Skip to content

Commit

Permalink
Support RefFromWasmAbi
Browse files Browse the repository at this point in the history
  • Loading branch information
cwfitzgerald committed Jul 25, 2023
1 parent 66cddfe commit 96c302f
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 5 deletions.
22 changes: 21 additions & 1 deletion tests/expand/borrow.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -72,4 +75,21 @@ const _: () = {
<JsType as OptionFromWasmAbi>::is_none(js)
}
}
pub struct SelfOwner<T>(T);
impl<T> ::core::ops::Deref for SelfOwner<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'a> RefFromWasmAbi for Borrow<'a>
where
Self: _serde::de::DeserializeOwned,
{
type Abi = <JsType as RefFromWasmAbi>::Abi;
type Anchor = SelfOwner<Self>;
unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor {
SelfOwner(Self::from_abi(js))
}
}
};
22 changes: 21 additions & 1 deletion tests/expand/generic_enum.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -73,4 +76,21 @@ const _: () = {
<JsType as OptionFromWasmAbi>::is_none(js)
}
}
pub struct SelfOwner<T>(T);
impl<T> ::core::ops::Deref for SelfOwner<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T, U> RefFromWasmAbi for GenericEnum<T, U>
where
Self: _serde::de::DeserializeOwned,
{
type Abi = <JsType as RefFromWasmAbi>::Abi;
type Anchor = SelfOwner<Self>;
unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor {
SelfOwner(Self::from_abi(js))
}
}
};
44 changes: 42 additions & 2 deletions tests/expand/generic_struct.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -70,6 +73,23 @@ const _: () = {
<JsType as OptionFromWasmAbi>::is_none(js)
}
}
pub struct SelfOwner<T>(T);
impl<T> ::core::ops::Deref for SelfOwner<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> RefFromWasmAbi for GenericStruct<T>
where
Self: _serde::de::DeserializeOwned,
{
type Abi = <JsType as RefFromWasmAbi>::Abi;
type Anchor = SelfOwner<Self>;
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>(T);
Expand All @@ -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]
Expand Down Expand Up @@ -140,4 +163,21 @@ const _: () = {
<JsType as OptionFromWasmAbi>::is_none(js)
}
}
pub struct SelfOwner<T>(T);
impl<T> ::core::ops::Deref for SelfOwner<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> RefFromWasmAbi for GenericNewtype<T>
where
Self: _serde::de::DeserializeOwned,
{
type Abi = <JsType as RefFromWasmAbi>::Abi;
type Anchor = SelfOwner<Self>;
unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor {
SelfOwner(Self::from_abi(js))
}
}
};
22 changes: 21 additions & 1 deletion tsify-macros/src/wasm_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*,
};
Expand Down Expand Up @@ -137,5 +137,25 @@ fn expand_from_wasm_abi(cont: &Container) -> TokenStream {
<JsType as OptionFromWasmAbi>::is_none(js)
}
}

pub struct SelfOwner<T>(T);

impl<T> ::core::ops::Deref for SelfOwner<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl #impl_generics RefFromWasmAbi for #ident #ty_generics #where_clause {
type Abi = <JsType as RefFromWasmAbi>::Abi;

type Anchor = SelfOwner<Self>;

unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor {
SelfOwner(Self::from_abi(js))
}
}
}
}

0 comments on commit 96c302f

Please sign in to comment.