diff --git a/Cargo.toml b/Cargo.toml index 828dd99b466..df0fe064e11 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] @@ -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' } diff --git a/src/convert/slices.rs b/src/convert/slices.rs index 9d0970f4e6a..a0c08602755 100644 --- a/src/convert/slices.rs +++ b/src/convert/slices.rs @@ -146,6 +146,8 @@ cfg_if! { } if_std! { + use std::{hint, iter}; + impl IntoWasmAbi for Vec where Box<[T]>: IntoWasmAbi { type Abi = as IntoWasmAbi>::Abi; @@ -174,6 +176,39 @@ if_std! { fn is_none(abi: &WasmSlice) -> bool { abi.ptr == 0 } } + impl IntoWasmAbi for [T; N] where Box<[T]>: IntoWasmAbi { + type Abi = as IntoWasmAbi>::Abi; + + #[inline] + fn into_abi(self) -> Self::Abi { + >::from(self).into_abi() + } + } + + impl OptionIntoWasmAbi for [T; N] where Box<[T]>: IntoWasmAbi { + #[inline] + fn none() -> WasmSlice { null_slice() } + } + + impl FromWasmAbi for [T; N] where Box<[T]>: FromWasmAbi { + type Abi = as FromWasmAbi>::Abi; + + #[inline] + unsafe fn from_abi(js: Self::Abi) -> Self { + let vec = >::from(>::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 OptionFromWasmAbi for [T; N] where Box<[T]>: FromWasmAbi { + #[inline] + fn is_none(abi: &WasmSlice) -> bool { abi.ptr == 0 } + } + impl IntoWasmAbi for String { type Abi = as IntoWasmAbi>::Abi; diff --git a/src/describe.rs b/src/describe.rs index 2d424f8f3f7..29c4a475f16 100644 --- a/src/describe.rs +++ b/src/describe.rs @@ -155,6 +155,12 @@ if_std! { >::describe(); } } + + impl WasmDescribe for [T; N] where Box<[T]>: WasmDescribe { + fn describe() { + >::describe(); + } + } } impl WasmDescribe for Option {