diff --git a/lib/c-api/src/wasm_c_api/macros.rs b/lib/c-api/src/wasm_c_api/macros.rs index 92dd1590206..07a99b01545 100644 --- a/lib/c-api/src/wasm_c_api/macros.rs +++ b/lib/c-api/src/wasm_c_api/macros.rs @@ -42,7 +42,7 @@ int main() { macro_rules! wasm_declare_vec { ($name:ident) => { paste::paste! { - #[doc = "Represents of a vector of `wasm_" $name "_t`. + #[doc = "Represents a vector of `wasm_" $name "_t`. Read the documentation of [`wasm_" $name "_t`] to see more concrete examples. @@ -107,6 +107,7 @@ int main() { .into_boxed_slice(); let data = copied_data.as_mut_ptr(); ::std::mem::forget(copied_data); + Self { size, data, @@ -183,7 +184,9 @@ int main() { macro_rules! wasm_declare_boxed_vec { ($name:ident) => { paste::paste! { - #[doc = "Represents of a vector of [`wasm_" $name "_t`]."] + #[doc = "Represents a vector of `wasm_" $name "_t`. + +Read the documentation of [`wasm_" $name "_t`] to see more concrete examples."] #[derive(Debug)] #[repr(C)] pub struct [] { @@ -206,6 +209,27 @@ macro_rules! wasm_declare_boxed_vec { } } + impl<'a, T: Into<[]> + Clone> From<&'a [T]> for [] { + fn from(other: &'a [T]) -> Self { + let size = other.len(); + let mut copied_data = other + .iter() + .cloned() + .map(Into::into) + .map(Box::new) + .map(Box::into_raw) + .collect::]>>() + .into_boxed_slice(); + let data = copied_data.as_mut_ptr(); + ::std::mem::forget(copied_data); + + Self { + size, + data, + } + } + } + // TODO: do this properly impl [] { pub unsafe fn into_slice(&self) -> Option<&[Box<[]>]>{