Wasm closures - value borrowed here after move

I am trying to render a scene using babylyonjs.
Part of making that work is setting the renderloop on the engine.

I have a wasm_bindgen (not going to share the entire thing) for that function.

#[wasm_bindgen(method, js_name = runRenderLoop, js_class = Engine)]
pub fn run_render_loop(this: &Engine, value: &Closure<dyn FnMut()>);

In rust I have a babylon structure that includes the engine and the scene and all that works fine.
Where I am getting stuck is setting it up so that rust delegate will called.

the code looks like this

let babylon: Babylon = Babylon::new(&canvas);

... a bunch of scene code that executes js (works fine as per chrome debugger)

let cb = Closure::wrap(Box::new(move || {
}) as Box<dyn FnMut()>);


This is where the wheels fall off for me and I don't know how to get past this.

Error: Compiling your crate to WebAssembly failed
33 | let cb = Closure::wrap(Box::new(move || {
| ------- value moved into closure here
34 | babylon.scene.render();
| ------------- variable moved due to use in closure
37 | babylon.engine.run_render_loop(&cb);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ value borrowed here after move

Any assistance on getting past this would be greatly appreciated

The issue is that the entire Babylon instance gets moved into the closure, when you only want babylon.scene to be moved into it. Try destructuring the top level object first:


let Babylon { scene, engine } = babylon;
let cb = Closure::wrap(Box::new(move || {
}) as Box<dyn FnMut()>);


Thank you for the help, much appreciated.
Everything is up and running now, yay!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.