diff options
author | Martin Fischer <martin@push-f.com> | 2021-11-20 15:10:23 +0100 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2021-11-20 18:36:07 +0100 |
commit | 91ca29ade1ab6b90dda972c2a42a5dc529ddfb44 (patch) | |
tree | aec288a5858fe3e8a90f0357c69571e091cf85d6 /src/lib.rs | |
parent | e918eb0ab2cf6d84751f5f52b49414d382b7abbb (diff) |
refactor: traverse AST via iterators
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 35 |
1 files changed, 15 insertions, 20 deletions
@@ -35,7 +35,7 @@ use syn::TypeParam; use syn::TypeParamBound; use syn::TypePath; use syn::Visibility; -use syn_utils::TypeMatcher; +use syn_utils::TypeOrPath; use crate::parse_assoc_type::parse_assoc_type; use crate::parse_assoc_type::AssocTypeParseError; @@ -43,7 +43,7 @@ use crate::parse_trait_sig::parse_trait_signature; use crate::parse_trait_sig::MethodParseError; use crate::parse_trait_sig::SignatureChanges; use crate::parse_trait_sig::TypeTransform; -use crate::syn_utils::find_in_path; +use crate::syn_utils::iter_path; use crate::syn_utils::trait_bounds; use crate::transform::AssocTypeConversions; @@ -181,7 +181,8 @@ pub fn dynamize(_attr: TokenStream, input: TokenStream) -> TokenStream { for type_param in dyn_trait.generics.type_params() { generic_map.insert(type_param.ident.clone(), type_param.bounds.clone()); for trait_bound in trait_bounds(&type_param.bounds) { - if let Some(assoc_type) = find_in_path(&trait_bound.path, &AssocTypeMatcher) { + if let Some(assoc_type) = iter_path(&trait_bound.path).find_map(filter_map_assoc_paths) + { return abort!( assoc_type.span(), "dynamize does not support associated types in trait generic bounds" @@ -345,27 +346,21 @@ fn generate_blanket_impl( } } -struct AssocTypeMatcher; -impl TypeMatcher<Path> for AssocTypeMatcher { - fn match_path<'a>(&self, path: &'a Path) -> Option<&'a Path> { - if path.segments.first().unwrap().ident == "Self" { - return Some(path); - } - None - } +fn path_is_assoc_type(path: &Path) -> bool { + path.segments.first().unwrap().ident == "Self" } -impl<TypeMatch, PathMatch, T> TypeMatcher<T> for (TypeMatch, PathMatch) -where - TypeMatch: Fn(&Type) -> Option<&T>, - PathMatch: Fn(&Path) -> Option<&T>, -{ - fn match_type<'a>(&self, t: &'a Type) -> Option<&'a T> { - self.0(t) +fn match_assoc_type(item: TypeOrPath) -> bool { + if let TypeOrPath::Path(path) = item { + return path_is_assoc_type(path); } + false +} - fn match_path<'a>(&self, path: &'a Path) -> Option<&'a T> { - self.1(path) +fn filter_map_assoc_paths(item: TypeOrPath) -> Option<&Path> { + match item { + TypeOrPath::Path(p) if path_is_assoc_type(p) => Some(p), + _other => None, } } |