Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support RefFromWasmAbi #30

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}
}
}
}