如何将本地转换closure
为js_sys::Funtion
?
我想做这样的事情:
let canvas = document.get_element_by_id("canvas").unwrap();
let e: web_sys::HtmlElement = canvas.dyn_into().unwrap();
let f = || {};
e.set_onresize(Some(&f.into()));
如何将本地转换closure
为js_sys::Funtion
?
我想做这样的事情:
let canvas = document.get_element_by_id("canvas").unwrap();
let e: web_sys::HtmlElement = canvas.dyn_into().unwrap();
let f = || {};
e.set_onresize(Some(&f.into()));
我找到了这个。
https://rustwasm.github.io/wasm-bindgen/reference/passing-rust-closures-to-js.html
它像是:
let f = Closure::wrap(Box::new(move || { /* whatever */}) as Box<dyn FnMut()>);
e.set_onresize(Some(f.as_ref().unchecked_ref()));
f.forget(); // It is not good practice, just for simplification!