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 all commits
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test = false
[features]
default = ["spans", "std"]
spans = ["wasm-bindgen-macro/spans"]
std = []
std = ["array-init"]
serde-serialize = ["serde", "serde_json", "std"]
nightly = []
enable-interning = ["std"]
Expand All @@ -40,6 +40,7 @@ wasm-bindgen-macro = { path = "crates/macro", version = "=0.2.75" }
serde = { version = "1.0", optional = true }
serde_json = { version = "1.0", optional = true }
cfg-if = "1.0.0"
array-init = { version = "2.0.0", optional = true }

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
js-sys = { path = 'crates/js-sys', version = '0.3.52' }
Expand Down
35 changes: 35 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::{hint, iter};

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,39 @@ 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: Default, 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 vec = <Vec<T>>::from(<Box<[T]>>::from_abi(js));
let iter = vec.into_iter().chain(iter::repeat_with(T::default));
match array_init::from_iter(iter) {
Some(arr) => arr,
None => hint::unreachable_unchecked(),
}
}
}

impl<T: Default, 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