Skip to content
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

Support converting js_sys typed arrays to Vec #811

Closed
aschampion opened this issue Sep 11, 2018 · 4 comments
Closed

Support converting js_sys typed arrays to Vec #811

aschampion opened this issue Sep 11, 2018 · 4 comments
Labels
js-sys Issues related to the `js-sys` crate

Comments

@aschampion
Copy link

Currently there's no way to convert TypedArrays to Vec<T>s in rust, e.g., js_sys::Uint8Array to Vec<u8>, even though one can pass (via an implicit copy) a Uint8Array across the wasm boundary to rust functions expecting Vec<u8> or &[u8]. See this comment for motivation.

@fitzgen fitzgen added the js-sys Issues related to the `js-sys` crate label Sep 12, 2018
@alexcrichton
Copy link
Contributor

One thing I've been pondering recently is if it's possible to, in a generic fashion, convert anything that implements IntoWasmAbi into a JsValue. This would fit the bill perfectly for this issue as well as solve a number of other issues I've wanted to handle in the past. We currently do this ad-hoc in two locations:

It'd be great to somehow unify the two of these and provide this for all types (like &[u8] which can be converted into a JsValue). I'll noodle on this a bit...

alexcrichton added a commit to alexcrichton/wasm-bindgen that referenced this issue Oct 2, 2018
This commit implements a generic function on `JsValue` which can convert
*any* Rust value that implements `IntoWasmAbi` into the corresponding
`JsValue`. This can be handy when binding APIs and the intermediate JS
value is needed is needed over a few function calls or is otherwise
needed when calling APIs that take `JsValue` but otherwise work with
Rust primitives.

One of the primary intended use cases for this will be interoperation
with the various `JsValue`-taking functions (or `Object`) in `web-sys`.

cc rustwasm#811
@alexcrichton
Copy link
Contributor

I believe I've implemented this in #918

alexcrichton added a commit to alexcrichton/wasm-bindgen that referenced this issue Jan 4, 2019
For all typed arrays, this commit adds:

* `TypedArray::view(src: &[Type])`
* `TypedArray::copy_to(&self, dst: &mut [Type])`

The `view` function is unsafe because it doesn't provide any guarantees
about lifetimes or mutability. The `copy_to` function is, however, safe.

Closes rustwasm#811
@alexcrichton
Copy link
Contributor

Ok I've made another attempt to solve this in #1147!

@dakom

This comment was marked as abuse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js-sys Issues related to the `js-sys` crate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants