I previously got help with this unboxed closure issue by creating several implementations:
With the Stabilization of GATs, is there any way to combine these 3 implementations? Thanks!
Working code...
use wasm_bindgen::closure::Closure;
/* POC: Use traits instead of nested calls
* i.e.
* let x = myFn.set_closure_fnmut();
* instead of
* let x = Closure::wrap(Box::new(myFn) as Box<dyn FnMut(Event)>);
*/
pub trait SetClosureFnOnce<Dyn: ?Sized> {
fn set_closure_fnonce(self) -> Closure<Dyn>;
}
impl<F> SetClosureFnOnce<dyn FnMut()> for F
where
F: FnOnce() + 'static,
{
fn set_closure_fnonce(self) -> Closure<dyn FnMut()> {
let cb_boxed = Box::new(self) as Box<dyn FnOnce()>;
Closure::once(cb_boxed)
}
}
pub trait SetClosureFnMut<Dyn: ?Sized> {
fn set_closure_fnmut(self) -> Closure<Dyn>;
}
impl<F> SetClosureFnMut<dyn FnMut()> for F
where
F: FnMut() + 'static,
{
fn set_closure_fnmut(self) -> Closure<dyn FnMut()> {
let cb_boxed = Box::new(self) as Box<dyn FnMut()>;
Closure::wrap(cb_boxed)
}
}
impl<F, A, R> SetClosureFnMut<dyn FnMut(A) -> R> for F
where
F: FnMut(A) -> R + 'static,
A: wasm_bindgen::convert::FromWasmAbi + 'static,
R: wasm_bindgen::convert::IntoWasmAbi + 'static,
{
fn set_closure_fnmut(self) -> Closure<dyn FnMut(A) -> R> {
let cb_boxed = Box::new(self) as Box<dyn FnMut(A) -> R>;
Closure::wrap(cb_boxed)
}
}