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

feat: impl LongRefFromWasmAbi for T where T: Tsify + FromWasmAbi #35

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
21 changes: 20 additions & 1 deletion tests/expand/borrow.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const _: () = {
use wasm_bindgen::{
convert::{
FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi,
RefFromWasmAbi,
RefFromWasmAbi, LongRefFromWasmAbi,
},
describe::WasmDescribe, prelude::*,
};
Expand Down Expand Up @@ -243,6 +243,11 @@ const _: () = {
&self.0
}
}
impl<T> ::core::borrow::Borrow<T> for SelfOwner<T> {
fn borrow(&self) -> &T {
&self.0
}
}
impl<'a> RefFromWasmAbi for Borrow<'a>
where
Self: _serde::de::DeserializeOwned,
Expand All @@ -257,4 +262,18 @@ const _: () = {
SelfOwner(result.unwrap_throw())
}
}
impl<'a> LongRefFromWasmAbi for Borrow<'a>
where
Self: _serde::de::DeserializeOwned,
{
type Abi = <JsType as LongRefFromWasmAbi>::Abi;
type Anchor = SelfOwner<Self>;
unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor {
let result = Self::from_js(&JsType::from_abi(js));
if let Err(err) = result {
wasm_bindgen::throw_str(err.to_string().as_ref());
}
SelfOwner(result.unwrap_throw())
}
}
};
21 changes: 20 additions & 1 deletion tests/expand/generic_enum.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const _: () = {
use wasm_bindgen::{
convert::{
FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi,
RefFromWasmAbi,
RefFromWasmAbi, LongRefFromWasmAbi,
},
describe::WasmDescribe, prelude::*,
};
Expand Down Expand Up @@ -249,6 +249,11 @@ const _: () = {
&self.0
}
}
impl<T> ::core::borrow::Borrow<T> for SelfOwner<T> {
fn borrow(&self) -> &T {
&self.0
}
}
impl<T, U> RefFromWasmAbi for GenericEnum<T, U>
where
Self: _serde::de::DeserializeOwned,
Expand All @@ -263,4 +268,18 @@ const _: () = {
SelfOwner(result.unwrap_throw())
}
}
impl<T, U> LongRefFromWasmAbi for GenericEnum<T, U>
where
Self: _serde::de::DeserializeOwned,
{
type Abi = <JsType as LongRefFromWasmAbi>::Abi;
type Anchor = SelfOwner<Self>;
unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor {
let result = Self::from_js(&JsType::from_abi(js));
if let Err(err) = result {
wasm_bindgen::throw_str(err.to_string().as_ref());
}
SelfOwner(result.unwrap_throw())
}
}
};
42 changes: 40 additions & 2 deletions tests/expand/generic_struct.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const _: () = {
use wasm_bindgen::{
convert::{
FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi,
RefFromWasmAbi,
RefFromWasmAbi, LongRefFromWasmAbi,
},
describe::WasmDescribe, prelude::*,
};
Expand Down Expand Up @@ -248,6 +248,11 @@ const _: () = {
&self.0
}
}
impl<T> ::core::borrow::Borrow<T> for SelfOwner<T> {
fn borrow(&self) -> &T {
&self.0
}
}
impl<T> RefFromWasmAbi for GenericStruct<T>
where
Self: _serde::de::DeserializeOwned,
Expand All @@ -262,6 +267,20 @@ const _: () = {
SelfOwner(result.unwrap_throw())
}
}
impl<T> LongRefFromWasmAbi for GenericStruct<T>
where
Self: _serde::de::DeserializeOwned,
{
type Abi = <JsType as LongRefFromWasmAbi>::Abi;
type Anchor = SelfOwner<Self>;
unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor {
let result = Self::from_js(&JsType::from_abi(js));
if let Err(err) = result {
wasm_bindgen::throw_str(err.to_string().as_ref());
}
SelfOwner(result.unwrap_throw())
}
}
};
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct GenericNewtype<T>(T);
Expand All @@ -272,7 +291,7 @@ const _: () = {
use wasm_bindgen::{
convert::{
FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi,
RefFromWasmAbi,
RefFromWasmAbi, LongRefFromWasmAbi,
},
describe::WasmDescribe, prelude::*,
};
Expand Down Expand Up @@ -511,6 +530,11 @@ const _: () = {
&self.0
}
}
impl<T> ::core::borrow::Borrow<T> for SelfOwner<T> {
fn borrow(&self) -> &T {
&self.0
}
}
impl<T> RefFromWasmAbi for GenericNewtype<T>
where
Self: _serde::de::DeserializeOwned,
Expand All @@ -525,4 +549,18 @@ const _: () = {
SelfOwner(result.unwrap_throw())
}
}
impl<T> LongRefFromWasmAbi for GenericNewtype<T>
where
Self: _serde::de::DeserializeOwned,
{
type Abi = <JsType as LongRefFromWasmAbi>::Abi;
type Anchor = SelfOwner<Self>;
unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor {
let result = Self::from_js(&JsType::from_abi(js));
if let Err(err) = result {
wasm_bindgen::throw_str(err.to_string().as_ref());
}
SelfOwner(result.unwrap_throw())
}
}
};
22 changes: 21 additions & 1 deletion tsify-next-macros/src/wasm_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub fn expand(cont: &Container, decl: Decl) -> TokenStream {
#use_serde
use tsify_next::Tsify;
use wasm_bindgen::{
convert::{FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi, RefFromWasmAbi},
convert::{FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi, RefFromWasmAbi, LongRefFromWasmAbi},
describe::WasmDescribe,
prelude::*,
};
Expand Down Expand Up @@ -165,6 +165,12 @@ fn expand_from_wasm_abi(cont: &Container) -> TokenStream {
}
}

impl<T> ::core::borrow::Borrow<T> for SelfOwner<T> {
fn borrow(&self) -> &T {
&self.0
}
}

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

Expand All @@ -178,5 +184,19 @@ fn expand_from_wasm_abi(cont: &Container) -> TokenStream {
SelfOwner(result.unwrap_throw())
}
}

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

type Anchor = SelfOwner<Self>;

unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor {
let result = Self::from_js(&JsType::from_abi(js));
if let Err(err) = result {
wasm_bindgen::throw_str(err.to_string().as_ref());
}
SelfOwner(result.unwrap_throw())
}
}
}
}
Loading