diff options
author | Martin Fischer <martin@push-f.com> | 2021-11-20 10:01:15 +0100 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2021-11-20 10:19:03 +0100 |
commit | 7d549f29d63df264884e80f1e95d881d7c0f7407 (patch) | |
tree | 1206e4fb69cbf1ba7ae1d5fd34979e230c6a667f | |
parent | 80225095fe224285215b83406950d53835e4dcf9 (diff) |
refactor: use syn::Generics::type_params iterators
-rw-r--r-- | src/lib.rs | 18 | ||||
-rw-r--r-- | src/transform.rs | 79 |
2 files changed, 45 insertions, 52 deletions
@@ -178,16 +178,14 @@ pub fn dynamize(_attr: TokenStream, input: TokenStream) -> TokenStream { let mut generic_map = HashMap::new(); - for generic in &dyn_trait.generics.params { - if let GenericParam::Type(type_param) = generic { - 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) { - return abort!( - assoc_type.span(), - "dynamize does not support associated types in trait generic bounds" - ); - } + 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) { + return abort!( + assoc_type.span(), + "dynamize does not support associated types in trait generic bounds" + ); } } } diff --git a/src/transform.rs b/src/transform.rs index 39b32cc..cbada94 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -1,9 +1,7 @@ use std::collections::HashMap; use proc_macro2::Span; -use syn::{ - spanned::Spanned, GenericParam, Generics, Ident, PathArguments, Type, TypeParamBound, TypePath, -}; +use syn::{spanned::Spanned, Generics, Ident, PathArguments, Type, TypeParamBound, TypePath}; use crate::{ parse_assoc_type::DestType, @@ -107,51 +105,48 @@ pub fn dynamize_function_bounds( ) -> Result<HashMap<Ident, Vec<TypeTransform>>, (Span, MethodParseError)> { let mut type_param_transforms = HashMap::new(); - for generic_param in &mut generics.params { - if let GenericParam::Type(type_param) = generic_param { - for bound in &mut type_param.bounds { - if let TypeParamBound::Trait(bound) = bound { - if bound.path.segments.len() == 1 { - let segment = bound.path.segments.first_mut().unwrap(); + for type_param in generics.type_params_mut() { + for bound in &mut type_param.bounds { + if let TypeParamBound::Trait(bound) = bound { + if bound.path.segments.len() == 1 { + let segment = bound.path.segments.first_mut().unwrap(); - if let PathArguments::Parenthesized(args) = &mut segment.arguments { - if segment.ident == "Fn" - || segment.ident == "FnOnce" - || segment.ident == "FnMut" - { - let mut transforms = Vec::new(); - for input_type in &mut args.inputs { - match assoc_type_conversions.parse_type_path(input_type) { - Ok(ret_type) => { - transforms.push(ret_type); - } - Err(TransformError::UnconvertibleAssocType(span)) => { - return Err(( - span, - MethodParseError::UnconvertibleAssocType, - )); - } - Err(TransformError::AssocTypeInUnsupportedType(span)) => { - return Err(( - span, - MethodParseError::UnconvertibleAssocTypeInFnInput, - )); - } + if let PathArguments::Parenthesized(args) = &mut segment.arguments { + if segment.ident == "Fn" + || segment.ident == "FnOnce" + || segment.ident == "FnMut" + { + let mut transforms = Vec::new(); + for input_type in &mut args.inputs { + match assoc_type_conversions.parse_type_path(input_type) { + Ok(ret_type) => { + transforms.push(ret_type); + } + Err(TransformError::UnconvertibleAssocType(span)) => { + return Err(( + span, + MethodParseError::UnconvertibleAssocType, + )); + } + Err(TransformError::AssocTypeInUnsupportedType(span)) => { + return Err(( + span, + MethodParseError::UnconvertibleAssocTypeInFnInput, + )); } } - if transforms.iter().any(|t| !matches!(t, TypeTransform::NoOp)) { - type_param_transforms - .insert(type_param.ident.clone(), transforms); - } + } + if transforms.iter().any(|t| !matches!(t, TypeTransform::NoOp)) { + type_param_transforms.insert(type_param.ident.clone(), transforms); } } } - if let Some(path) = find_in_path(&bound.path, &AssocTypeMatcher) { - return Err(( - path.span(), - MethodParseError::UnconvertibleAssocTypeInTraitBound, - )); - } + } + if let Some(path) = find_in_path(&bound.path, &AssocTypeMatcher) { + return Err(( + path.span(), + MethodParseError::UnconvertibleAssocTypeInTraitBound, + )); } } } |