diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 10 | ||||
-rw-r--r-- | src/parse_trait_sig.rs | 17 | ||||
-rw-r--r-- | src/transform.rs | 37 |
3 files changed, 32 insertions, 32 deletions
@@ -44,7 +44,7 @@ use crate::parse_trait_sig::SignatureChanges; use crate::parse_trait_sig::TypeTransform; use crate::syn_utils::iter_path; use crate::syn_utils::trait_bounds; -use crate::transform::AssocTypeConversions; +use crate::transform::TypeConverter; mod parse_assoc_type; mod parse_trait_sig; @@ -66,7 +66,7 @@ pub fn dynamize(_attr: TokenStream, input: TokenStream) -> TokenStream { let mut objectifiable_methods: Vec<(Signature, SignatureChanges)> = Vec::new(); - let mut assoc_type_conversions = AssocTypeConversions::default(); + let mut type_converter = TypeConverter::default(); for item in &original_trait.items { if let TraitItem::Type(assoc_type) = item { @@ -82,7 +82,9 @@ pub fn dynamize(_attr: TokenStream, input: TokenStream) -> TokenStream { ) } Ok((ident, type_)) => { - assoc_type_conversions.0.insert(ident.clone(), type_); + type_converter + .assoc_type_conversions + .insert(ident.clone(), type_); } } } @@ -91,7 +93,7 @@ pub fn dynamize(_attr: TokenStream, input: TokenStream) -> TokenStream { for item in &original_trait.items { if let TraitItem::Method(method) = item { let mut signature = method.sig.clone(); - match parse_trait_signature(&mut signature, &assoc_type_conversions) { + match parse_trait_signature(&mut signature, &type_converter) { Err((_, MethodParseError::NonDispatchableMethod)) => continue, Err((span, MethodParseError::UnconvertibleAssocType)) => { return abort!( diff --git a/src/parse_trait_sig.rs b/src/parse_trait_sig.rs index 4c0b4fd..8f578f7 100644 --- a/src/parse_trait_sig.rs +++ b/src/parse_trait_sig.rs @@ -9,7 +9,7 @@ use syn::{Ident, Signature, TypeImplTrait}; use crate::match_assoc_type; use crate::parse_assoc_type::BoxType; use crate::syn_utils::{iter_type, trait_bounds, TypeOrPath}; -use crate::transform::{dynamize_function_bounds, AssocTypeConversions, TransformError}; +use crate::transform::{dynamize_function_bounds, TransformError, TypeConverter}; #[derive(Debug, Clone)] pub enum TypeTransform { @@ -49,7 +49,7 @@ pub struct SignatureChanges { pub fn parse_trait_signature( signature: &mut Signature, - assoc_type_conversions: &AssocTypeConversions, + type_converter: &TypeConverter, ) -> Result<SignatureChanges, (Span, MethodParseError)> { if is_non_dispatchable(signature) { return Err((signature.span(), MethodParseError::NonDispatchableMethod)); @@ -67,11 +67,10 @@ pub fn parse_trait_signature( } } - let type_param_transforms = - dynamize_function_bounds(&mut signature.generics, assoc_type_conversions)?; + let type_param_transforms = dynamize_function_bounds(&mut signature.generics, type_converter)?; let return_type = match &mut signature.output { - ReturnType::Type(_, og_type) => match assoc_type_conversions.parse_type_path(og_type) { + ReturnType::Type(_, og_type) => match type_converter.convert_type(og_type) { Ok(ret_type) => ret_type, Err(TransformError::UnconvertibleAssocType(span)) => { return Err((span, MethodParseError::UnconvertibleAssocType)); @@ -147,7 +146,7 @@ mod tests { parse_trait_sig::{ parse_trait_signature, MethodParseError, SignatureChanges, TypeTransform, }, - transform::AssocTypeConversions, + transform::TypeConverter, }; #[test] @@ -173,14 +172,14 @@ mod tests { }) .unwrap(); - let mut assoc_type_map = AssocTypeConversions::default(); + let mut type_converter = TypeConverter::default(); let ident = format_ident!("A"); let dest_inner = Type::Verbatim(quote! {Example}); let dest = DestType::Into(&dest_inner); - assoc_type_map.0.insert(ident, dest); + type_converter.assoc_type_conversions.insert(ident, dest); assert!(matches!( - parse_trait_signature(&mut type1.sig, &assoc_type_map), + parse_trait_signature(&mut type1.sig, &type_converter), Ok(SignatureChanges { return_type: TypeTransform::Into, .. diff --git a/src/transform.rs b/src/transform.rs index 4625f4c..47e27a6 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -15,7 +15,9 @@ use crate::{ }; #[derive(Default)] -pub struct AssocTypeConversions<'a>(pub HashMap<Ident, DestType<'a>>); +pub struct TypeConverter<'a> { + pub assoc_type_conversions: HashMap<Ident, DestType<'a>>, +} pub enum TransformError { UnconvertibleAssocType(Span), @@ -39,8 +41,8 @@ fn is_supported_collection_map(ident: &Ident) -> bool { ident == "HashMap" || ident == "BTreeMap" } -impl AssocTypeConversions<'_> { - pub fn parse_type_path(&self, type_: &mut Type) -> Result<TypeTransform, TransformError> { +impl TypeConverter<'_> { + pub fn convert_type(&self, type_: &mut Type) -> Result<TypeTransform, TransformError> { if !iter_type(type_).any(match_assoc_type) { return Ok(TypeTransform::NoOp); } @@ -69,8 +71,7 @@ impl AssocTypeConversions<'_> { if binding.ident == "Item" && iter_type(&binding.ty).any(match_assoc_type) { - let inner = - self.parse_type_path(&mut binding.ty)?; + let inner = self.convert_type(&mut binding.ty)?; let box_type = BoxType { inner: quote! {#elem}, placeholder_lifetime: true, @@ -96,7 +97,7 @@ impl AssocTypeConversions<'_> { if path.segments.len() == 2 { let ident = &path.segments.last().unwrap().ident; let dest_type = self - .0 + .assoc_type_conversions .get(ident) .ok_or_else(|| TransformError::UnconvertibleAssocType(ident.span()))?; *type_ = dest_type.get_dest(); @@ -113,13 +114,11 @@ impl AssocTypeConversions<'_> { if (last_seg.ident == "Option" && path_len == 1) || last_seg.ident == "Result" { - return Ok(TypeTransform::Map( - self.parse_type_path(arg)?.into(), - )); + return Ok(TypeTransform::Map(self.convert_type(arg)?.into())); } else if is_supported_collection(&last_seg.ident) && path_len == 1 { return Ok(TypeTransform::Collection( - self.parse_type_path(arg)?.into(), + self.convert_type(arg)?.into(), )); } } @@ -134,13 +133,13 @@ impl AssocTypeConversions<'_> { { if last_seg.ident == "Result" { return Ok(TypeTransform::Result( - self.parse_type_path(arg1)?.into(), - self.parse_type_path(arg2)?.into(), + self.convert_type(arg1)?.into(), + self.convert_type(arg2)?.into(), )); } else if is_supported_collection_map(&last_seg.ident) { return Ok(TypeTransform::CollectionMap( - self.parse_type_path(arg1)?.into(), - self.parse_type_path(arg2)?.into(), + self.convert_type(arg1)?.into(), + self.convert_type(arg2)?.into(), )); } } @@ -158,7 +157,7 @@ impl AssocTypeConversions<'_> { pub fn dynamize_function_bounds( generics: &mut Generics, - assoc_type_conversions: &AssocTypeConversions, + type_converter: &TypeConverter, ) -> Result<HashMap<Ident, Vec<TypeTransform>>, (Span, MethodParseError)> { let mut type_param_transforms = HashMap::new(); @@ -167,7 +166,7 @@ pub fn dynamize_function_bounds( if let TypeParamBound::Trait(bound) = bound { dynamize_trait_bound( bound, - assoc_type_conversions, + type_converter, &type_param.ident, &mut type_param_transforms, )?; @@ -184,7 +183,7 @@ pub fn dynamize_function_bounds( if let TypeParamBound::Trait(bound) = bound { dynamize_trait_bound( bound, - assoc_type_conversions, + type_converter, ident, &mut type_param_transforms, )?; @@ -226,7 +225,7 @@ pub fn dynamize_function_bounds( fn dynamize_trait_bound( bound: &mut TraitBound, - assoc_type_conversions: &AssocTypeConversions, + type_converter: &TypeConverter, type_ident: &Ident, type_param_transforms: &mut HashMap<Ident, Vec<TypeTransform>>, ) -> Result<(), (Span, MethodParseError)> { @@ -237,7 +236,7 @@ fn dynamize_trait_bound( 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) { + match type_converter.convert_type(input_type) { Ok(ret_type) => { transforms.push(ret_type); } |