diff options
author | Martin Fischer <martin@push-f.com> | 2021-11-22 12:05:22 +0100 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2021-11-22 12:05:22 +0100 |
commit | e8581b5034c52636f4f76a8e91b57e7752323d04 (patch) | |
tree | a3dae3ef0289a2f7a28f001df6f9c7939bbe6df5 | |
parent | 7384c11750e9971770683861f7cd5f719c487ce9 (diff) |
make convert_type skip non-type generics
-rw-r--r-- | src/syn_utils.rs | 9 | ||||
-rw-r--r-- | src/transform.rs | 65 |
2 files changed, 39 insertions, 35 deletions
diff --git a/src/syn_utils.rs b/src/syn_utils.rs index ea4b5ea..00efee0 100644 --- a/src/syn_utils.rs +++ b/src/syn_utils.rs @@ -23,6 +23,15 @@ pub fn lifetime_bounds<T>( }) } +pub fn type_arguments_mut<P>( + args: &mut Punctuated<GenericArgument, P>, +) -> impl Iterator<Item = &mut Type> { + args.iter_mut().filter_map(|a| match a { + GenericArgument::Type(t) => Some(t), + _other => None, + }) +} + pub enum TypeOrPath<'a> { Type(&'a Type), Path(&'a Path), diff --git a/src/transform.rs b/src/transform.rs index 4649037..60ac6e8 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -11,7 +11,7 @@ use crate::{ filter_map_assoc_paths, match_assoc_type, parse_assoc_type::{BoxType, DestType}, parse_trait_sig::{MethodError, TypeTransform}, - syn_utils::{iter_path, iter_type}, + syn_utils::{iter_path, iter_type, type_arguments_mut}, }; #[derive(Default)] @@ -111,43 +111,38 @@ impl TypeConverter<'_> { let last_seg = path.segments.last_mut().unwrap(); if let PathArguments::AngleBracketed(args) = &mut last_seg.arguments { - if args.args.len() == 1 { - if let GenericArgument::Type(arg) = &mut args.args[0] { - if iter_type(arg).any(match_assoc_type) { - if (last_seg.ident == "Option" && path_len == 1) - || last_seg.ident == "Result" - { - return Ok(TypeTransform::Map(self.convert_type(arg)?.into())); - } else if self.known_as_collection_with_one_type(&last_seg.ident) - && path_len == 1 - { - return Ok(TypeTransform::IntoIterMapCollect(vec![ - self.convert_type(arg)? - ])); - } - } - } - } else if args.args.len() == 2 && path_len == 1 { - let mut args_iter = args.args.iter_mut(); - if let (GenericArgument::Type(arg1), GenericArgument::Type(arg2)) = - (args_iter.next().unwrap(), args_iter.next().unwrap()) - { - if iter_type(arg1).any(match_assoc_type) - || iter_type(arg2).any(match_assoc_type) + let mut args: Vec<_> = type_arguments_mut(&mut args.args).collect(); + + if args.len() == 1 { + if iter_type(args[0]).any(match_assoc_type) { + if (last_seg.ident == "Option" && path_len == 1) + || last_seg.ident == "Result" { - if last_seg.ident == "Result" { - return Ok(TypeTransform::Result( - self.convert_type(arg1)?.into(), - self.convert_type(arg2)?.into(), - )); - } else if self.known_as_collection_with_two_types(&last_seg.ident) { - return Ok(TypeTransform::IntoIterMapCollect(vec![ - self.convert_type(arg1)?, - self.convert_type(arg2)?, - ])); - } + return Ok(TypeTransform::Map(self.convert_type(args[0])?.into())); + } else if self.known_as_collection_with_one_type(&last_seg.ident) + && path_len == 1 + { + return Ok(TypeTransform::IntoIterMapCollect(vec![ + self.convert_type(args[0])? + ])); } } + } else if args.len() == 2 + && path_len == 1 + && (iter_type(args[0]).any(match_assoc_type) + || iter_type(args[1]).any(match_assoc_type)) + { + if last_seg.ident == "Result" { + return Ok(TypeTransform::Result( + self.convert_type(args[0])?.into(), + self.convert_type(args[1])?.into(), + )); + } else if self.known_as_collection_with_two_types(&last_seg.ident) { + return Ok(TypeTransform::IntoIterMapCollect(vec![ + self.convert_type(args[0])?, + self.convert_type(args[1])?, + ])); + } } } } |