-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow Serde to bind Uint8Array to Vec<u8> #2017
Comments
Thanks for the report! This is due to the fact that This may be fixable by updating the implementation of the intrinsics to special case typed arrays, but I'm not sure if that's the best change to make per se or how it would best be done otherwise. |
@zer0x64 For passing serialized data between Rust and JS, I recommend using serde-wasm-bindgen, it should handle this situation correctly. @alexcrichton Since Or maybe we can change |
Came here to mention serde-wasm-bindgen, but @Pauan beat me to it :)
That was the plan on the original PR. The problem is, this is a potential breaking change, so until wasm-bindgen bumps significant version number, it's tracked like other breaking changes under a corresponding label in #1258. |
I'd be fine deprecating the |
@alexcrichton To clarify, you mean just a soft deprecation warning for now, right? |
Indeed yeah, either removing them or eventually implementing in terms of the serde-wasm-bindgen crate I think is the way to go, so we want to discourage usage of them as-is. |
Not sure if this should be considered as a feature or a bug...
Motivation
Currently,
wasm-bindgen
bindVec<u8>
return type toUint8Array
on the javascript type. However, when usinginto_serde()
to pass the data to the Rust side, the method returns an error when parsing aUint8Array
into aVec<u8>
.Although the solution in a simple case like this is to simply take a
Vec<u8>
directly as an input, this doesn't work for more complex type. A hack around it wouild be to convert the Uint8Array into an Array on the rust side:My specific type context was to receive a
Vec<Vec<u8>>
, which would be of the typeArray<Uint8Array>
from the Javascript side. I'll be dumping the code snippet of the hack here in case someone else need it:This does work, but is pretty ugly. From a user perspective, it makes sense that deserializing a
Uint8Array
into aVec<u8>
should work without this boilerplate.Alternatives
For the moment, I'm using the hack described above.
The text was updated successfully, but these errors were encountered: