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

TypedArray::from_slice() #925

Merged
merged 7 commits into from
Sep 23, 2022
Merged

TypedArray::from_slice() #925

merged 7 commits into from
Sep 23, 2022

Conversation

dherman
Copy link
Collaborator

@dherman dherman commented Sep 16, 2022

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
Copy link
Collaborator Author

dherman commented Sep 16, 2022

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
Copy link
Collaborator Author

dherman commented Sep 16, 2022

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).

Copy link
Member

@kjvalencik kjvalencik left a comment

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.

test/napi/src/js/typedarrays.rs Outdated Show resolved Hide resolved
crates/neon/src/types_impl/buffer/types.rs Outdated Show resolved Hide resolved
crates/neon/src/types_impl/buffer/mod.rs Outdated Show resolved Hide resolved
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
Copy link
Member

@kjvalencik kjvalencik left a comment

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