-
-
Notifications
You must be signed in to change notification settings - Fork 32
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
Serialize Rust Vector to JS typed array (Float32Array, Uint32Array, Uint16Array ...) #2
Comments
web-sys already has this functionality built-in: (And similarly for the other typed arrays) Keep in mind this will make a |
Hi @Pauan , thanks for reaching out and for the precious tip. I followed your advice and flicking through docs, issues and reddit threads, I learned a little bit more regarding Rust/Wasm interaction and I ended up successfully using I got your point regarding Thanks a lot! |
Yeah, it's probably pretty fast.
No, in order to maintain memory safety it has to make a copy. There is a In particular, as long as the typed array is alive you must not do any Rust allocations whatsoever. That means no So if you're passing the typed array to JS, and you don't call any Rust functions until after it's been discarded in JS, then yeah you can use it. But I recommend using the safe
That code is unsafe. It is using the raw WebAssembly memory, which can be invalidated at any time. It is the same as the But as I said above, if you use It's always possible to switch from |
Hi @Pauan , I got your concern regarding unsafe memory guarantees. I took the time to check source code and see how the impl<'a> From<&'a [$ty]> for $name {
#[inline]
fn from(slice: &'a [$ty]) -> $name {
// This is safe because the `new` function makes a copy if its argument is a TypedArray
unsafe { $name::new(&$name::view(slice)) }
}
} I'll definitely follow your advice! Thanks again! 🙏 |
Hi,
I spent a few time with this great lib although I would like to know if there's a workaround in order to pass back to javascript typed arrays (
Float32Array
,Uint32Array
,Uint16Array
...)?Something like what is feasible with https://github.com/serde-rs/bytes for Rust
Vec<u8>
.Thanks!
The text was updated successfully, but these errors were encountered: