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

Add conversions between typed arrays and Rust #1147

Merged
merged 1 commit into from
Jan 7, 2019

Conversation

alexcrichton
Copy link
Contributor

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 #811

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
/// Copy the contents of this JS typed array into the destination
/// Rust slice.
///
/// This function will efficiently copy the memory from a typed
Copy link
Contributor

@chinedufn chinedufn Jan 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious, what does "efficiently" mean here? What is the less efficient alternative?

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah this just means that it uses the set API which eventually translates to something like a raw memcpy. That's the fastest way (even in Rust) to copy chunks of memory.

The slow way would be to copy bytes one-by-one in wasm which misses on on things like vectorization and eliding bounds checks

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gains knowledge

Cool - thanks!!

macro_rules! arrays {
($($name:ident: $ty:ident,)*) => ($(
impl $name {
/// Creates a JS typed array which is a few into wasm's linear
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor thing that I noticed -> few -> view

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I'll be sure to handle before landing!

Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@alexcrichton alexcrichton merged commit 9dced1d into rustwasm:master Jan 7, 2019
@alexcrichton alexcrichton deleted the more-slicing branch January 7, 2019 18:59
@grovesNL
Copy link
Contributor

grovesNL commented Feb 1, 2019

This looks great, thanks! Is there a way to return a single element from the typed array as well? There doesn’t seem to be an indexing getter for typed arrays in js-sys, but this is supported in JavaScript.

@Pauan
Copy link
Contributor

Pauan commented Feb 1, 2019

@grovesNL Currently you would use Reflect.get, like this:

let value = js_sys::Reflect::get(typed_array.as_ref(), &JsValue::from_f64(0.0)).unwrap();

Reflect.get works on pretty much everything: objects, arrays, TypedArray, etc.

It's equivalent to the foo[bar] syntax in JS.


@alexcrichton @fitzgen Any thoughts on adding a convenience method which basically does the above?

I think it'd be especially nice if the convenience method could avoid the JsValue::from_f64 boxing (since f64 can be passed natively to/from JS).

If you're okay with it, I can try sending a PR.

@grovesNL
Copy link
Contributor

grovesNL commented Feb 1, 2019

Oh right, using Reflect.get makes sense, thanks! I thought there might be a more direct way to index into these arrays. It could be useful to make a note of it from the typed array documentation somewhere if this is the recommended approach going forward (I'd be happy to send a PR).

It feels slightly confusing now because of the availability of set there with no corresponding get. I even looked through all the traits expecting to see something like IndexingGetter<u8> :)

@alexcrichton
Copy link
Contributor Author

Adding a convenience or a method to just get one item sounds like a good idea to me! @Pauan I'd be down for such a PR

@Pauan
Copy link
Contributor

Pauan commented Feb 4, 2019

PR made at #1225

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants