Skip to content

Conversation

@kjvalencik
Copy link
Member

@kjvalencik kjvalencik commented Jan 22, 2025

This removes the TryIntoJs implementations for Arc and Box to free them up for implicitly Boxed implementations.

In order to maintain the ability to return borrowed buffers (e.g., Arc<Vec<u8>>), I've added extractors for typed arrays. It requires slightly more type noise (e.g., Uint8Array<Arc<Vec<u8>> instead of Arc<Vec<u8>>), but it is more powerful because it will work across all types that are AsRef<[T]>.

The implementation in this PR opts for bespoke struct extractors for each of the typed array types (e.g., Uint8Array, Int8Array). The benefits of this approach are the simplicity of implementation and the ability to directly use them as destructor patterns and constructors. The most significant downside is that there's no trait to unify them for generic buffer functions.

If we wanted to include a trait for handling these in the future, we could introduce a TypedArray extractor and accept that this implementation is distinct from the one in this PR. No breaking change would be necessary.

Example Usage

use bytes::BytesMut;
use neon::types::extract::Uint8Array;

#[neon::export]
pub fn concat_bytes(
    Uint8Array(mut buf): Uint8Array<BytesMut>,
    data: Vec<u8>,
) -> Uint8Array<BytesMut> {
    buf.put_slice(&data);

    Uint8Array(buf)
}

Copy link
Collaborator

@dherman dherman left a comment

Choose a reason for hiding this comment

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

I love this design, it's a huge ergonomics win. Let's add just a couple unit tests to have some examples in repo.

@kjvalencik kjvalencik merged commit 065ba2d into main Jan 25, 2025
9 checks passed
@kjvalencik kjvalencik deleted the kv/buf-extractor branch January 25, 2025 16:50
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.

3 participants