Skip to content

TypedArray::from_slice() - #925

Merged
dherman merged 7 commits into
mainfrom
from_slice
Sep 23, 2022
Merged

TypedArray::from_slice()#925
dherman merged 7 commits into
mainfrom
from_slice

Conversation

@dherman

@dherman dherman commented Sep 16, 2022

Copy link
Copy Markdown
Collaborator

This PR follows on #909 with an additional TypedArray::from_slice() method, making it convenient and easy to copy from a Rust slice into a new typed array.

This is not only convenient, but also mitigates the ergonomic cost of deprecating JsArrayBuffer::external(), which seems like it'll be necessary due to recent changes in V8 to deprecate external typed arrays.

@dherman

dherman commented Sep 16, 2022

Copy link
Copy Markdown
Collaborator Author

I tried adding from_slice() to the TypedArray trait, but that trait doesn't extend Value and JsTypedArray<T> isn't declared to universally implement Value, which makes satisfying the type checker a challenge when trying to add a method that returns JsResult<Self>.

@dherman dherman changed the title Add JsArrayBuffer::from_slice() and JsTypedArray::from_slice() TypedArray::from_slice() Sep 16, 2022
@dherman dherman changed the title TypedArray::from_slice() TypedArray::from_slice() Sep 16, 2022
@dherman dherman changed the title TypedArray::from_slice() TypedArray::from_slice() Sep 16, 2022
@dherman

dherman commented Sep 16, 2022

Copy link
Copy Markdown
Collaborator Author

Update: I've moved the from_slice() method into TypedArray (thanks to @kjvalencik's help leveling up my understanding of the power of where clauses).

@dherman
dherman requested a review from kjvalencik September 19, 2022 17:23

@kjvalencik kjvalencik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It would be nice to have some docs added to JsTypedArray (and possibly a mirrored JsTypedArray::from_slice method) to help users find this constructor since it's not typical in Rust for a type's constructor to be on a trait.

Comment thread test/napi/src/js/typedarrays.rs
Comment thread crates/neon/src/types_impl/buffer/types.rs Outdated
Comment thread crates/neon/src/types_impl/buffer/mod.rs Outdated
dherman and others added 4 commits September 23, 2022 09:12
Co-authored-by: K.J. Valencik <kjvalencik@gmail.com>
- TypedArray doesn't need to explicit inherit Sealed since Value does
- Binary now inherits Copy
@dherman
dherman requested a review from kjvalencik September 23, 2022 16:47

@kjvalencik kjvalencik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Awesome! Love these improvements! :shipit:

@dherman
dherman merged commit 3c6f62d into main Sep 23, 2022
@dherman
dherman deleted the from_slice branch September 23, 2022 16:51
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.

2 participants