Skip to content

Commit 20d8604

Browse files
bors[bot]Bromeon
andauthored
Merge #961
961: Fixed ToVariant/FromVariant derive for generic types with bounds r=Bromeon a=Bromeon Fixes part of #957; but there's a separate problem with `where` clauses (see description). Co-authored-by: Jan Haller <[email protected]>
2 parents 3731a64 + ea43b63 commit 20d8604

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

gdnative-derive/src/variant/bounds.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
use syn::punctuated::Punctuated;
12
use syn::visit::Visit;
2-
use syn::Generics;
3+
use syn::{GenericParam, Generics};
34

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

@@ -50,3 +51,20 @@ pub(crate) fn extend_bounds(
5051
}
5152
})
5253
}
54+
55+
pub(crate) fn remove_bounds(mut generics: Generics) -> Generics {
56+
for param in generics.params.iter_mut() {
57+
match param {
58+
GenericParam::Type(ty) => {
59+
ty.colon_token = None;
60+
ty.bounds = Punctuated::new();
61+
}
62+
GenericParam::Lifetime(lt) => {
63+
lt.colon_token = None;
64+
lt.bounds = Punctuated::new();
65+
}
66+
GenericParam::Const(_) => {}
67+
}
68+
}
69+
generics
70+
}

gdnative-derive/src/variant/from.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use proc_macro2::{Literal, Span, TokenStream as TokenStream2};
22

3+
use crate::variant::bounds;
34
use syn::Ident;
45

56
use super::repr::Repr;
@@ -106,11 +107,12 @@ pub(crate) fn expand_from_variant(derive_data: DeriveData) -> Result<TokenStream
106107
}
107108
};
108109

110+
let generics_no_bounds = bounds::remove_bounds(generics.clone());
109111
let where_clause = &generics.where_clause;
110112

111113
let result = quote! {
112114
#derived
113-
impl #generics ::gdnative::core_types::FromVariant for #ident #generics #where_clause {
115+
impl #generics ::gdnative::core_types::FromVariant for #ident #generics_no_bounds #where_clause {
114116
fn from_variant(
115117
#input_ident: &::gdnative::core_types::Variant
116118
) -> ::std::result::Result<Self, ::gdnative::core_types::FromVariantError> {

gdnative-derive/src/variant/to.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::variant::bounds;
12
use proc_macro2::{Literal, TokenStream as TokenStream2};
23

34
use super::repr::Repr;
@@ -69,11 +70,12 @@ pub(crate) fn expand_to_variant(
6970
}
7071
};
7172

73+
let generics_no_bounds = bounds::remove_bounds(generics.clone());
7274
let where_clause = &generics.where_clause;
7375

7476
let result = quote! {
7577
#derived
76-
impl #generics #trait_path for #ident #generics #where_clause {
78+
impl #generics #trait_path for #ident #generics_no_bounds #where_clause {
7779
fn #to_variant_fn(#to_variant_receiver) -> ::gdnative::core_types::Variant {
7880
use #trait_path;
7981
use ::gdnative::core_types::FromVariant;

test/src/test_derive.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ pub(crate) fn register(handle: InitHandle) {
4040

4141
crate::godot_itest! { test_derive_to_variant {
4242
#[derive(Clone, Eq, PartialEq, Debug, ToVariant, FromVariant)]
43-
struct ToVar<T, R>
43+
struct ToVar<T: Associated, R>
4444
where
45-
T: Associated,
4645
R: Default,
4746
{
4847
foo: T::A,
@@ -55,7 +54,7 @@ crate::godot_itest! { test_derive_to_variant {
5554
}
5655

5756
#[derive(Clone, Eq, PartialEq, Debug, ToVariant, FromVariant)]
58-
enum ToVarEnum<T> {
57+
enum ToVarEnum<T: Bound> {
5958
Foo(T),
6059
Bar,
6160
Baz { baz: u8 },
@@ -67,9 +66,12 @@ crate::godot_itest! { test_derive_to_variant {
6766
T: Associated,
6867
R: Default;
6968

69+
trait Bound {}
70+
impl Bound for bool {}
71+
7072
trait Associated {
7173
type A;
72-
type B;
74+
type B : Bound;
7375
}
7476

7577
impl Associated for f64 {

0 commit comments

Comments
 (0)