CodingMaster239
CodingMaster239

Reputation: 1

How can I make a simple requestAnimationFrame loop in Rust with arguments?

I am trying to make a requestAnimationFrame loop in Rust with arguments.

I've looked and found how to make a requestAnimationFrame loop like so:

fn loop() {
    // do stuff here

    let closure = Closure::wrap(Box::new(move || {
        loop();
    }) as Box<dyn FnMut()>);
    
    web_sys::window().unwrap().request_animation_frame(closure.as_ref().unchecked_ref()).unwrap();
    closure.forget();
}

However, I need to pass arguments to the loop function. In JavaScript it would look something like this:

function loop(arg) {
    // do stuff with arg
    
    requestAnimationFrame(arg);
}

If I try adding an argument to the Rust function I get this error:

expected a closure that implements the `FnMut` trait, but this closure only implements `FnOnce`
required for the cast from `Box<{closure@src/main.rs:75:42: 75:49}>` to `Box<dyn FnMut()>

I can't use FnOnce because it doesn't implement the WasmClosure trait.

Upvotes: 0

Views: 123

Answers (0)

Related Questions