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
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/convert/slices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ cfg_if! {
}

if_std! {
use std::convert::TryFrom;

impl<T> IntoWasmAbi for Vec<T> where Box<[T]>: IntoWasmAbi<Abi = WasmSlice> {
type Abi = <Box<[T]> as IntoWasmAbi>::Abi;

Expand Down Expand Up @@ -174,6 +176,38 @@ if_std! {
fn is_none(abi: &WasmSlice) -> bool { abi.ptr == 0 }
}

impl<T, const N: usize> IntoWasmAbi for [T; N] where Box<[T]>: IntoWasmAbi<Abi = WasmSlice> {
type Abi = <Box<[T]> as IntoWasmAbi>::Abi;

#[inline]
fn into_abi(self) -> Self::Abi {
<Box<[T]>>::from(self).into_abi()
}
}

impl<T, const N: usize> OptionIntoWasmAbi for [T; N] where Box<[T]>: IntoWasmAbi<Abi = WasmSlice> {
#[inline]
fn none() -> WasmSlice { null_slice() }
}

impl<T, const N: usize> FromWasmAbi for [T; N] where Box<[T]>: FromWasmAbi<Abi = WasmSlice> {
type Abi = <Box<[T]> as FromWasmAbi>::Abi;

#[inline]
unsafe fn from_abi(js: Self::Abi) -> Self {
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

}
}
}

impl<T, const N: usize> OptionFromWasmAbi for [T; N] where Box<[T]>: FromWasmAbi<Abi = WasmSlice> {
#[inline]
fn is_none(abi: &WasmSlice) -> bool { abi.ptr == 0 }
}

impl IntoWasmAbi for String {
type Abi = <Vec<u8> as IntoWasmAbi>::Abi;

Expand Down
6 changes: 6 additions & 0 deletions src/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ if_std! {
<Box<[T]>>::describe();
}
}

impl<T, const N: usize> WasmDescribe for [T; N] where Box<[T]>: WasmDescribe {
fn describe() {
<Box<[T]>>::describe();
}
}
}

impl<T: WasmDescribe> WasmDescribe for Option<T> {
Expand Down