From 774864c85efdcacec141ca803dbd8364b5126080 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Fri, 26 Nov 2021 10:39:52 +0100 Subject: support dynamized supertraits with generics --- src/lib.rs | 20 +++++++++++--------- tests/tests.rs | 4 ++++ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 73ae723..3f1640a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -84,7 +84,9 @@ pub fn dynamize(_attr: TokenStream, input: TokenStream) -> TokenStream { }; for dyn_supertrait in &method_attrs.dynamized_supertraits { - if !trait_bounds(&original_trait.supertraits).any(|t| t.path.is_ident(dyn_supertrait)) { + if !trait_bounds(&original_trait.supertraits) + .any(|t| t.path.segments[0].ident == *dyn_supertrait) + { return abort!( dyn_supertrait.span(), "this trait definition has no such supertrait" @@ -206,14 +208,14 @@ pub fn dynamize(_attr: TokenStream, input: TokenStream) -> TokenStream { .iter() .filter_map(|t| { if let TypeParamBound::Trait(trait_bound) = t { - if let Some(ident) = trait_bound.path.get_ident() { - if ident == "Sized" { - return None; - } else if method_attrs.dynamized_supertraits.contains(ident) { - let mut bound = trait_bound.clone(); - bound.path.segments[0].ident = format_ident!("Dyn{}", ident); - return Some(TypeParamBound::Trait(bound)); - } + if trait_bound.path.is_ident("Sized") { + return None; + } + let ident = &trait_bound.path.segments[0].ident; + if method_attrs.dynamized_supertraits.contains(ident) { + let mut bound = trait_bound.clone(); + bound.path.segments[0].ident = format_ident!("Dyn{}", ident); + return Some(TypeParamBound::Trait(bound)); } } Some(t.clone()) diff --git a/tests/tests.rs b/tests/tests.rs index 11bf10d..aa5e996 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -298,6 +298,10 @@ trait OneTrait: SomeTrait { fn test(&self) -> Self::X; } +#[dynamize::dynamize] +#[dynamized(Foo)] +trait OtherTrait: Foo {} + struct SuperError; trait Cache {} #[dynamize::dynamize] -- cgit v1.2.3