Support for automatically clone()
-ing objects to allow passing by value
#4511
Labels
clone()
-ing objects to allow passing by value
#4511
Uh oh!
There was an error while loading. Please reload this page.
Motivation
#1754 added support for
#[wasm_bindgen]
onasync
functions, which is great, because it generates a method on the Javascript side with a strong return type ofPromise<Foo>
.However, as noted in that PR, it doesn't work correctly for methods that take
&self
, because that's not safe. [Aside: it seemed to compile fine for me, but it's certainly not safe and I ended up shooting my foot off. But I digress.]The suggested workaround in #1754 is to use a wrapper type which contains an
Rc<RefCell<InnerCore>>
and pass that by value. This is fine, but you end up having to clone the wrapper on each call from Javascript. Something like this:This works fine, but requires the caller to remember to call
clone()
, otherwise they later get mysterious errors about "null pointer passed to rust", which is error-prone at best. It also means I need a shim forclone
which I otherwise wouldn't need.Proposed Solution
It would be really nice to be able to annotate the method to indicate that wasm-bindgen should generate glue code which clones the arguments. For example, maybe I could write something like the following to indicate that both arguments need cloning:
Alternatives
Additional Context
The text was updated successfully, but these errors were encountered: