aboutsummaryrefslogtreecommitdiff
path: root/src/transform.rs
diff options
context:
space:
mode:
authorMartin Fischer <martin@push-f.com>2021-11-25 10:30:54 +0100
committerMartin Fischer <martin@push-f.com>2021-11-26 11:45:53 +0100
commit7489a3c2246e7ea2483446dd2ed3fdbfaf462c1a (patch)
treefc2670f56f7cc16567eb10d88f9439abb9098298 /src/transform.rs
parent43950edc4f26a07055fb917fa8bbb262276e2a08 (diff)
introduce #[convert] attribute
Diffstat (limited to 'src/transform.rs')
-rw-r--r--src/transform.rs16
1 files changed, 11 insertions, 5 deletions
diff --git a/src/transform.rs b/src/transform.rs
index 75138c9..ae345c8 100644
--- a/src/transform.rs
+++ b/src/transform.rs
@@ -1,4 +1,4 @@
-use std::collections::HashMap;
+use std::{collections::HashMap, rc::Rc};
use proc_macro2::Span;
use quote::quote;
@@ -10,14 +10,16 @@ use syn::{
use crate::{
filter_map_assoc_paths, match_assoc_type,
parse_assoc_type::{BoxType, DestType},
+ parse_attrs::Convert,
syn_utils::{iter_path, iter_type, type_arguments_mut},
trait_sig::{MethodError, TypeTransform},
};
-pub struct TypeConverter<'a> {
- pub assoc_type_conversions: HashMap<Ident, DestType<'a>>,
+pub struct TypeConverter {
+ pub assoc_type_conversions: HashMap<Ident, DestType>,
pub collections: HashMap<Ident, usize>,
pub trait_ident: Ident,
+ pub type_conversions: HashMap<Type, Rc<Convert>>,
}
#[derive(Debug)]
@@ -30,7 +32,7 @@ pub enum TransformError {
SelfQualifiedAsOtherTrait,
}
-impl TypeConverter<'_> {
+impl TypeConverter {
/// A return type of Some(1) means that the type implements
/// IntoIterator<Item=T1> and FromIterator<T1>
/// with T1 being its first generic type parameter.
@@ -59,6 +61,10 @@ impl TypeConverter<'_> {
}
pub fn convert_type(&self, type_: &mut Type) -> Result<TypeTransform, (Span, TransformError)> {
+ if let Some(conv) = self.type_conversions.get(type_) {
+ *type_ = conv.dest_type.clone();
+ return Ok(TypeTransform::Verbatim(conv.clone()));
+ }
if !iter_type(type_).any(match_assoc_type) {
return Ok(TypeTransform::NoOp);
}
@@ -139,7 +145,7 @@ impl TypeConverter<'_> {
return Ok(dest_type.type_transformation());
}
- return Err((path.span(), TransformError::SelfQualifiedAsOtherTrait));
+ return Err((type_.span(), TransformError::SelfQualifiedAsOtherTrait));
}
}
} else if let Type::Path(TypePath { path, qself: None }) = type_ {