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

Fixed ToVariant/FromVariant derive for generic types with bounds #961

Merged
merged 1 commit into from
Oct 16, 2022
Merged
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
20 changes: 19 additions & 1 deletion gdnative-derive/src/variant/bounds.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use syn::punctuated::Punctuated;
use syn::visit::Visit;
use syn::Generics;
use syn::{GenericParam, Generics};

use crate::extend_bounds::{with_visitor, BoundsVisitor};

Expand Down Expand Up @@ -50,3 +51,20 @@ pub(crate) fn extend_bounds(
}
})
}

pub(crate) fn remove_bounds(mut generics: Generics) -> Generics {
for param in generics.params.iter_mut() {
match param {
GenericParam::Type(ty) => {
ty.colon_token = None;
ty.bounds = Punctuated::new();
}
GenericParam::Lifetime(lt) => {
lt.colon_token = None;
lt.bounds = Punctuated::new();
}
GenericParam::Const(_) => {}
}
}
generics
}
4 changes: 3 additions & 1 deletion gdnative-derive/src/variant/from.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use proc_macro2::{Literal, Span, TokenStream as TokenStream2};

use crate::variant::bounds;
use syn::Ident;

use super::repr::Repr;
Expand Down Expand Up @@ -106,11 +107,12 @@ pub(crate) fn expand_from_variant(derive_data: DeriveData) -> Result<TokenStream
}
};

let generics_no_bounds = bounds::remove_bounds(generics.clone());
let where_clause = &generics.where_clause;

let result = quote! {
#derived
impl #generics ::gdnative::core_types::FromVariant for #ident #generics #where_clause {
impl #generics ::gdnative::core_types::FromVariant for #ident #generics_no_bounds #where_clause {
fn from_variant(
#input_ident: &::gdnative::core_types::Variant
) -> ::std::result::Result<Self, ::gdnative::core_types::FromVariantError> {
Expand Down
4 changes: 3 additions & 1 deletion gdnative-derive/src/variant/to.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::variant::bounds;
use proc_macro2::{Literal, TokenStream as TokenStream2};

use super::repr::Repr;
Expand Down Expand Up @@ -69,11 +70,12 @@ pub(crate) fn expand_to_variant(
}
};

let generics_no_bounds = bounds::remove_bounds(generics.clone());
let where_clause = &generics.where_clause;

let result = quote! {
#derived
impl #generics #trait_path for #ident #generics #where_clause {
impl #generics #trait_path for #ident #generics_no_bounds #where_clause {
fn #to_variant_fn(#to_variant_receiver) -> ::gdnative::core_types::Variant {
use #trait_path;
use ::gdnative::core_types::FromVariant;
Expand Down
10 changes: 6 additions & 4 deletions test/src/test_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ pub(crate) fn register(handle: InitHandle) {

crate::godot_itest! { test_derive_to_variant {
#[derive(Clone, Eq, PartialEq, Debug, ToVariant, FromVariant)]
struct ToVar<T, R>
struct ToVar<T: Associated, R>
where
T: Associated,
R: Default,
{
foo: T::A,
Expand All @@ -55,7 +54,7 @@ crate::godot_itest! { test_derive_to_variant {
}

#[derive(Clone, Eq, PartialEq, Debug, ToVariant, FromVariant)]
enum ToVarEnum<T> {
enum ToVarEnum<T: Bound> {
Foo(T),
Bar,
Baz { baz: u8 },
Expand All @@ -67,9 +66,12 @@ crate::godot_itest! { test_derive_to_variant {
T: Associated,
R: Default;

trait Bound {}
impl Bound for bool {}

trait Associated {
type A;
type B;
type B : Bound;
}

impl Associated for f64 {
Expand Down