#Implementing clone for Closure, where Closure is a wasm-bindgen struct

3 messages · Page 1 of 1 (latest)

heavy bison
#

The closure struct in wasm-bindgen which looks like this

pub struct Closure<T: ?Sized> {
    js: ManuallyDrop<JsValue>,
    data: ManuallyDrop<Box<T>>,
}

And I am trying to implement a clone trait for a custom struct that uses the Closure struct

pub(crate) struct Listener {
    // handler: fn(),
    pub(crate) handler: Closure<dyn FnMut()>,
}

since the trait Clone is not implemented for Closure<dyn FnMut()>

How do I implement it?

Thanks

glacial bison
#

You, uh, don't. You might be able to use a workaround though.

#

There are ways to clone trait objects, but they rely on being able to return Box<dyn Trait>, and here the box is hidden from you