Skip to content

&JsValue -> &T where T is an exported Rust struct #3943

Answered by Liamolucko
WilliamVenner asked this question in Q&A
Discussion options

You must be logged in to vote

No, there isn't at the moment.

A literal &JsValue -> &T transformation wouldn't work. The reason is that the way that borrowing Rust types works is that wasm-bindgen wraps them in an internal RefCell which it borrows every time a reference is passed to an exported Rust function, and then at the end of the function the Ref / RefMut is dropped to mark the RefCell as non-borrowed again. Otherwise JavaScript could easily pass the same T in for two different &mut T parameters of a function and cause undefined behaviour.

With a &T, there's no way to run some code when it's dropped; so, such an API would require creating some kind of Ref<T> or Handle<T> type which you can then turn &JsValue into.


Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@WilliamVenner
Comment options

Answer selected by WilliamVenner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants