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

Implement To/FromWasmAbi for arrays #2649

Closed

Conversation

Jules-Bertholet
Copy link
Contributor

Allows using Rust arrays as arguments and return values for wasm-bindgen functions.

let result = <[T; N]>::try_from(<Vec<T>>::from(<Box<[T]>>::from_abi(js)));
return match result {
Ok(arr) => arr,
Err(_) => panic!("JS array length does not match Rust array")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is not ideal. I see a few alternatives here:

  1. Accept the panic
  2. Add a T: Default bound and fill missing values with default()
  3. Add a T: JsCast bound and fill missing values with JsValue::UNDEFINED

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've gone ahead and implemented alternative 2

@alexcrichton
Copy link
Contributor

Thanks for the PR! I'd prefer to not implement this via this strategy though becuase this can be a hidden performance trap relative to allocations and transferring/copying memory. I don't necessarily know of an alternative way to implement this that doesn't suffer from such an issue, but I would recomend consumers instead work with Vec<T> and then convert to [T; N] themselves (e.g. with the array-init crate you found) instead of baking that directly into wasm-bindgen

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