diff options
author | Martin Fischer <martin@push-f.com> | 2021-11-25 16:56:44 +0100 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2021-11-26 11:45:53 +0100 |
commit | 74bcf35fc83c57fd7e9a31639d7dae31d4d9a050 (patch) | |
tree | 0084ecfa9f464505bbb3bfaa7b3899c11587dd2d /src/lib.rs | |
parent | 8046190e8f537407210fb87acbfe96d4084d58da (diff) |
support dynamized supertraits via attribute
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 26 |
1 files changed, 22 insertions, 4 deletions
@@ -83,6 +83,15 @@ pub fn dynamize(_attr: TokenStream, input: TokenStream) -> TokenStream { Err(err) => return err.to_compile_error().into(), }; + for dyn_supertrait in &method_attrs.dynamized_supertraits { + if !trait_bounds(&original_trait.supertraits).any(|t| t.path.is_ident(dyn_supertrait)) { + return abort!( + dyn_supertrait.span(), + "this trait definition has no such supertrait" + ); + } + } + let mut type_converter = TypeConverter { collections: method_attrs.collections, assoc_type_conversions: HashMap::new(), @@ -185,11 +194,20 @@ pub fn dynamize(_attr: TokenStream, input: TokenStream) -> TokenStream { supertraits: original_trait .supertraits .iter() - .filter(|t| match t { - TypeParamBound::Trait(t) => !t.path.is_ident("Sized"), - TypeParamBound::Lifetime(_) => true, + .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)); + } + } + } + Some(t.clone()) }) - .cloned() .collect(), brace_token: Brace::default(), items: Vec::new(), |