From 90fb74d1245ceb7d60d0e1c9892cfc7d04553fc2 Mon Sep 17 00:00:00 2001 From: Pantamis Date: Thu, 11 Apr 2024 19:07:04 +0200 Subject: [PATCH] feat: impl From for JsValue where T: IntoWasmAbi (#22) --- tests/expand/borrow.expanded.rs | 6 ++++++ tests/expand/generic_enum.expanded.rs | 6 ++++++ tests/expand/generic_struct.expanded.rs | 12 ++++++++++++ tsify-next-macros/src/wasm_bindgen.rs | 7 +++++++ 4 files changed, 31 insertions(+) diff --git a/tests/expand/borrow.expanded.rs b/tests/expand/borrow.expanded.rs index 4204c04..d984be0 100644 --- a/tests/expand/borrow.expanded.rs +++ b/tests/expand/borrow.expanded.rs @@ -204,6 +204,12 @@ const _: () = { ::none() } } + impl<'a> From> for JsValue { + #[inline] + fn from(value: Borrow<'a>) -> Self { + value.into_js().unwrap_throw().into() + } + } impl<'a> FromWasmAbi for Borrow<'a> where Self: _serde::de::DeserializeOwned, diff --git a/tests/expand/generic_enum.expanded.rs b/tests/expand/generic_enum.expanded.rs index 10a82ba..3d7db09 100644 --- a/tests/expand/generic_enum.expanded.rs +++ b/tests/expand/generic_enum.expanded.rs @@ -210,6 +210,12 @@ const _: () = { ::none() } } + impl From> for JsValue { + #[inline] + fn from(value: GenericEnum) -> Self { + value.into_js().unwrap_throw().into() + } + } impl FromWasmAbi for GenericEnum where Self: _serde::de::DeserializeOwned, diff --git a/tests/expand/generic_struct.expanded.rs b/tests/expand/generic_struct.expanded.rs index f0afc35..493e6a1 100644 --- a/tests/expand/generic_struct.expanded.rs +++ b/tests/expand/generic_struct.expanded.rs @@ -209,6 +209,12 @@ const _: () = { ::none() } } + impl From> for JsValue { + #[inline] + fn from(value: GenericStruct) -> Self { + value.into_js().unwrap_throw().into() + } + } impl FromWasmAbi for GenericStruct where Self: _serde::de::DeserializeOwned, @@ -459,6 +465,12 @@ const _: () = { ::none() } } + impl From> for JsValue { + #[inline] + fn from(value: GenericNewtype) -> Self { + value.into_js().unwrap_throw().into() + } + } impl FromWasmAbi for GenericNewtype where Self: _serde::de::DeserializeOwned, diff --git a/tsify-next-macros/src/wasm_bindgen.rs b/tsify-next-macros/src/wasm_bindgen.rs index b268079..0b0baab 100644 --- a/tsify-next-macros/src/wasm_bindgen.rs +++ b/tsify-next-macros/src/wasm_bindgen.rs @@ -110,6 +110,13 @@ fn expand_into_wasm_abi(cont: &Container) -> TokenStream { ::none() } } + + impl #impl_generics From<#ident #ty_generics> for JsValue { + #[inline] + fn from(value: #ident #ty_generics) -> Self { + value.into_js().unwrap_throw().into() + } + } } }