Skip to content

Commit

Permalink
fix: include trait bounds in where clause of From<JsValue> (#31)
Browse files Browse the repository at this point in the history
* fix: include trait bounds in where clause of From<JsValue>
  • Loading branch information
Pantamis committed Apr 16, 2024
1 parent 7cd5f26 commit cfc19b8
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
6 changes: 5 additions & 1 deletion tests-e2e/test3/entry_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ use wasm_bindgen::prelude::*;

#[derive(Serialize, Tsify)]
#[tsify(into_wasm_abi)]
pub struct Identified<Id, Value> {
pub struct Identified<Id, Value>
where
Id: Sync,
Value: 'static,
{
pub id: Id,
pub value: Value,
}
4 changes: 2 additions & 2 deletions tests/expand/borrow.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ const _: () = {
}
impl<'a> IntoWasmAbi for Borrow<'a>
where
Self: _serde::Serialize,
Borrow<'a>: _serde::Serialize,
{
type Abi = <JsType as IntoWasmAbi>::Abi;
#[inline]
Expand All @@ -197,7 +197,7 @@ const _: () = {
}
impl<'a> OptionIntoWasmAbi for Borrow<'a>
where
Self: _serde::Serialize,
Borrow<'a>: _serde::Serialize,
{
#[inline]
fn none() -> Self::Abi {
Expand Down
4 changes: 2 additions & 2 deletions tests/expand/generic_enum.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ const _: () = {
}
impl<T, U> IntoWasmAbi for GenericEnum<T, U>
where
Self: _serde::Serialize,
GenericEnum<T, U>: _serde::Serialize,
{
type Abi = <JsType as IntoWasmAbi>::Abi;
#[inline]
Expand All @@ -203,7 +203,7 @@ const _: () = {
}
impl<T, U> OptionIntoWasmAbi for GenericEnum<T, U>
where
Self: _serde::Serialize,
GenericEnum<T, U>: _serde::Serialize,
{
#[inline]
fn none() -> Self::Abi {
Expand Down
8 changes: 4 additions & 4 deletions tests/expand/generic_struct.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ const _: () = {
}
impl<T> IntoWasmAbi for GenericStruct<T>
where
Self: _serde::Serialize,
GenericStruct<T>: _serde::Serialize,
{
type Abi = <JsType as IntoWasmAbi>::Abi;
#[inline]
Expand All @@ -202,7 +202,7 @@ const _: () = {
}
impl<T> OptionIntoWasmAbi for GenericStruct<T>
where
Self: _serde::Serialize,
GenericStruct<T>: _serde::Serialize,
{
#[inline]
fn none() -> Self::Abi {
Expand Down Expand Up @@ -451,7 +451,7 @@ const _: () = {
}
impl<T> IntoWasmAbi for GenericNewtype<T>
where
Self: _serde::Serialize,
GenericNewtype<T>: _serde::Serialize,
{
type Abi = <JsType as IntoWasmAbi>::Abi;
#[inline]
Expand All @@ -461,7 +461,7 @@ const _: () = {
}
impl<T> OptionIntoWasmAbi for GenericNewtype<T>
where
Self: _serde::Serialize,
GenericNewtype<T>: _serde::Serialize,
{
#[inline]
fn none() -> Self::Abi {
Expand Down
12 changes: 4 additions & 8 deletions tsify-next-macros/src/wasm_bindgen.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use proc_macro2::TokenStream;
use quote::quote;
use syn::{parse_quote, WhereClause};
use syn::parse_quote;

use crate::{container::Container, decl::Decl};

Expand Down Expand Up @@ -86,19 +86,15 @@ fn expand_into_wasm_abi(cont: &Container) -> TokenStream {
let ident = cont.ident();
let serde_path = cont.serde_container.attrs.serde_path();

let borrowed_generics = cont.generics();
let mut generics = cont.generics().clone();
generics
.make_where_clause()
.predicates
.push(parse_quote!(Self: #serde_path::Serialize));
.push(parse_quote!(#ident #borrowed_generics: #serde_path::Serialize));

let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

let serde_where_clause = WhereClause {
where_token: parse_quote!(where),
predicates: parse_quote!(#ident #ty_generics: #serde_path::Serialize),
};

quote! {
impl #impl_generics IntoWasmAbi for #ident #ty_generics #where_clause {
type Abi = <JsType as IntoWasmAbi>::Abi;
Expand All @@ -116,7 +112,7 @@ fn expand_into_wasm_abi(cont: &Container) -> TokenStream {
}
}

impl #impl_generics From<#ident #ty_generics> for JsValue #serde_where_clause {
impl #impl_generics From<#ident #ty_generics> for JsValue #where_clause {
#[inline]
fn from(value: #ident #ty_generics) -> Self {
value.into_js().unwrap_throw().into()
Expand Down

0 comments on commit cfc19b8

Please sign in to comment.