From 96c302fbf199e91ae7837b90341cc288628ce296 Mon Sep 17 00:00:00 2001 From: Connor Fitzgerald Date: Thu, 20 Jul 2023 18:52:27 -0400 Subject: [PATCH] Support RefFromWasmAbi --- tests/expand/borrow.expanded.rs | 22 ++++++++++++- tests/expand/generic_enum.expanded.rs | 22 ++++++++++++- tests/expand/generic_struct.expanded.rs | 44 +++++++++++++++++++++++-- tsify-macros/src/wasm_bindgen.rs | 22 ++++++++++++- 4 files changed, 105 insertions(+), 5 deletions(-) diff --git a/tests/expand/borrow.expanded.rs b/tests/expand/borrow.expanded.rs index e370963..b406e74 100644 --- a/tests/expand/borrow.expanded.rs +++ b/tests/expand/borrow.expanded.rs @@ -10,7 +10,10 @@ const _: () = { extern crate serde as _serde; use tsify::Tsify; use wasm_bindgen::{ - convert::{FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi}, + convert::{ + FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi, + RefFromWasmAbi, + }, describe::WasmDescribe, prelude::*, }; #[wasm_bindgen] @@ -72,4 +75,21 @@ const _: () = { ::is_none(js) } } + pub struct SelfOwner(T); + impl ::core::ops::Deref for SelfOwner { + type Target = T; + fn deref(&self) -> &Self::Target { + &self.0 + } + } + impl<'a> RefFromWasmAbi for Borrow<'a> + where + Self: _serde::de::DeserializeOwned, + { + type Abi = ::Abi; + type Anchor = SelfOwner; + unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor { + SelfOwner(Self::from_abi(js)) + } + } }; diff --git a/tests/expand/generic_enum.expanded.rs b/tests/expand/generic_enum.expanded.rs index 5821d1d..9346411 100644 --- a/tests/expand/generic_enum.expanded.rs +++ b/tests/expand/generic_enum.expanded.rs @@ -11,7 +11,10 @@ const _: () = { extern crate serde as _serde; use tsify::Tsify; use wasm_bindgen::{ - convert::{FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi}, + convert::{ + FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi, + RefFromWasmAbi, + }, describe::WasmDescribe, prelude::*, }; #[wasm_bindgen] @@ -73,4 +76,21 @@ const _: () = { ::is_none(js) } } + pub struct SelfOwner(T); + impl ::core::ops::Deref for SelfOwner { + type Target = T; + fn deref(&self) -> &Self::Target { + &self.0 + } + } + impl RefFromWasmAbi for GenericEnum + where + Self: _serde::de::DeserializeOwned, + { + type Abi = ::Abi; + type Anchor = SelfOwner; + unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor { + SelfOwner(Self::from_abi(js)) + } + } }; diff --git a/tests/expand/generic_struct.expanded.rs b/tests/expand/generic_struct.expanded.rs index 935749f..2287516 100644 --- a/tests/expand/generic_struct.expanded.rs +++ b/tests/expand/generic_struct.expanded.rs @@ -8,7 +8,10 @@ const _: () = { extern crate serde as _serde; use tsify::Tsify; use wasm_bindgen::{ - convert::{FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi}, + convert::{ + FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi, + RefFromWasmAbi, + }, describe::WasmDescribe, prelude::*, }; #[wasm_bindgen] @@ -70,6 +73,23 @@ const _: () = { ::is_none(js) } } + pub struct SelfOwner(T); + impl ::core::ops::Deref for SelfOwner { + type Target = T; + fn deref(&self) -> &Self::Target { + &self.0 + } + } + impl RefFromWasmAbi for GenericStruct + where + Self: _serde::de::DeserializeOwned, + { + type Abi = ::Abi; + type Anchor = SelfOwner; + unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor { + SelfOwner(Self::from_abi(js)) + } + } }; #[tsify(into_wasm_abi, from_wasm_abi)] pub struct GenericNewtype(T); @@ -78,7 +98,10 @@ const _: () = { extern crate serde as _serde; use tsify::Tsify; use wasm_bindgen::{ - convert::{FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi}, + convert::{ + FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi, + RefFromWasmAbi, + }, describe::WasmDescribe, prelude::*, }; #[wasm_bindgen] @@ -140,4 +163,21 @@ const _: () = { ::is_none(js) } } + pub struct SelfOwner(T); + impl ::core::ops::Deref for SelfOwner { + type Target = T; + fn deref(&self) -> &Self::Target { + &self.0 + } + } + impl RefFromWasmAbi for GenericNewtype + where + Self: _serde::de::DeserializeOwned, + { + type Abi = ::Abi; + type Anchor = SelfOwner; + unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor { + SelfOwner(Self::from_abi(js)) + } + } }; diff --git a/tsify-macros/src/wasm_bindgen.rs b/tsify-macros/src/wasm_bindgen.rs index 9d796a4..fb4a95b 100644 --- a/tsify-macros/src/wasm_bindgen.rs +++ b/tsify-macros/src/wasm_bindgen.rs @@ -48,7 +48,7 @@ pub fn expand(cont: &Container, decl: Decl) -> TokenStream { #use_serde use tsify::Tsify; use wasm_bindgen::{ - convert::{FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi}, + convert::{FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi, RefFromWasmAbi}, describe::WasmDescribe, prelude::*, }; @@ -137,5 +137,25 @@ fn expand_from_wasm_abi(cont: &Container) -> TokenStream { ::is_none(js) } } + + pub struct SelfOwner(T); + + impl ::core::ops::Deref for SelfOwner { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.0 + } + } + + impl #impl_generics RefFromWasmAbi for #ident #ty_generics #where_clause { + type Abi = ::Abi; + + type Anchor = SelfOwner; + + unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor { + SelfOwner(Self::from_abi(js)) + } + } } }