aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorMartin Fischer <martin@push-f.com>2021-11-19 09:50:34 +0100
committerMartin Fischer <martin@push-f.com>2021-11-19 09:51:58 +0100
commiteb686879fdd7cde5d09837c202ebd25c9c9889a6 (patch)
treeec5388cd7e86513ed01dd5a89527fc44c4576777 /src/lib.rs
parent8e99d7bd6ebf9d1981667799f43cf45f682e1fff (diff)
refactor: forward type param transforms as HashMap
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs56
1 files changed, 32 insertions, 24 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 7f4e040..6e88136 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -78,7 +78,7 @@ pub fn dynamize(_attr: TokenStream, input: TokenStream) -> TokenStream {
)
}
Ok((ident, type_)) => {
- assoc_type_conversions.0.insert(ident, type_);
+ assoc_type_conversions.0.insert(ident.clone(), type_);
}
}
}
@@ -181,32 +181,40 @@ pub fn dynamize(_attr: TokenStream, input: TokenStream) -> TokenStream {
let fun_name = &new_method.sig.ident;
- let args = new_method
- .sig
- .inputs
- .iter()
- .enumerate()
- .map(|(idx, arg)| match arg {
- syn::FnArg::Receiver(_) => quote! {self},
- syn::FnArg::Typed(pat_type) => match pat_type.pat.as_ref() {
- syn::Pat::Ident(ident) => match &parsed_method.inputs[idx] {
- None => ident.ident.to_token_stream(),
- Some(transforms) => {
- let args = (0..transforms.len()).map(|i| format_ident!("a{}", i));
- let mut calls: Vec<_> =
- args.clone().map(|i| i.into_token_stream()).collect();
- for i in 0..calls.len() {
- transforms[i].append_conversion(&mut calls[i]);
+ let args = new_method.sig.inputs.iter().map(|arg| match arg {
+ syn::FnArg::Receiver(_) => quote! {self},
+ syn::FnArg::Typed(pat_type) => match pat_type.pat.as_ref() {
+ syn::Pat::Ident(ident) => {
+ // FUTURE: use try block
+ if let Type::Path(path) = &*pat_type.ty {
+ if let Some(type_ident) = path.path.get_ident() {
+ if let Some(transforms) =
+ parsed_method.type_param_transforms.get(type_ident)
+ {
+ let args = (0..transforms.len()).map(|i| format_ident!("a{}", i));
+ let mut calls: Vec<_> =
+ args.clone().map(|i| i.into_token_stream()).collect();
+ for i in 0..calls.len() {
+ transforms[i].append_conversion(&mut calls[i]);
+ }
+ let move_opt = new_method.sig.asyncness.map(|_| quote! {move});
+ quote!(#move_opt |#(#args),*| #ident(#(#calls),*))
+ } else {
+ ident.ident.to_token_stream()
}
- let move_opt = new_method.sig.asyncness.map(|_| quote! {move});
- quote!(#move_opt |#(#args),*| #ident(#(#calls),*))
+ } else {
+ ident.ident.to_token_stream()
}
- },
- _other => {
- panic!("unexpected");
+ } else {
+ ident.ident.to_token_stream()
}
- },
- });
+ }
+
+ _other => {
+ panic!("unexpected");
+ }
+ },
+ });
// in order for a trait to be object-safe its methods may not have
// generics so we convert method generics into trait generics