diff --git a/src/impl_as_ref.rs b/src/impl_as_ref.rs index 6eb4051..80adae1 100644 --- a/src/impl_as_ref.rs +++ b/src/impl_as_ref.rs @@ -11,42 +11,40 @@ use model::*; pub fn build(component: &AutoImpl, ref_ty: Trait) -> Result { let component_ident = &component.ident; - let impl_methods = try_iter!( - component.methods.iter() - .map(|method| { - let valid_receiver = match method.arg_self { - Some(ref arg_self) => match *arg_self { - SelfArg::Ref(_, syn::Mutability::Immutable) => true, - _ => false - }, - None => false - }; - - if !valid_receiver { - Err(format!("auto impl for `{}` is only supported for methods with a `&self` reciever", ref_ty))? - } - - method.build_impl_item(|method| { - let fn_ident = &method.ident; - let fn_args = &method.arg_pats; - - quote!({ - self.as_ref().#fn_ident( #(#fn_args),* ) - }) + let impl_methods = component.methods.iter() + .map(|method| { + let valid_receiver = match method.arg_self { + Some(ref arg_self) => match *arg_self { + SelfArg::Ref(_, syn::Mutability::Immutable) => true, + _ => false + }, + None => false + }; + + if !valid_receiver { + Err(format!("auto impl for `{}` is only supported for methods with a `&self` reciever", ref_ty))? + } + + method.build_impl_item(|method| { + let fn_ident = &method.ident; + let fn_args = &method.arg_pats; + + quote!({ + self.as_ref().#fn_ident( #(#fn_args),* ) }) }) - ); + }) + .collect::, _>>()?; - let impl_associated_types = try_iter!( - component.associated_types.iter() - .map(|associated_type| { - associated_type.build_impl_item(|associated_type| { - let ty_ident = &associated_type.ident; + let impl_associated_types = component.associated_types.iter() + .map(|associated_type| { + associated_type.build_impl_item(|associated_type| { + let ty_ident = &associated_type.ident; - quote!(TAutoImpl :: #ty_ident) - }) + quote!(TAutoImpl :: #ty_ident) }) - ); + }) + .collect::, _>>()?; let (trait_tys, impl_lifetimes, impl_tys, where_clauses) = component.split_generics(); diff --git a/src/lib.rs b/src/lib.rs index 3f50989..3eb6b6d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,25 +8,6 @@ include!("lib.proc_macro.rs"); extern crate quote; extern crate syn; -macro_rules! try_iter { - ($vec_expr:expr) => ({ - let v: Vec<_> = $vec_expr.collect(); - - if let Some(err) = v.iter().find(|res| res.is_err()) { - match *err { - Err(ref err) => Err(err.clone())?, - _ => unreachable!() - } - } - - let v: Vec<_> = v.into_iter() - .filter_map(Result::ok) - .collect(); - - v - }) -} - mod model; mod impl_as_ref; mod impl_fn; @@ -80,9 +61,7 @@ fn parse_impl_types(tokens: Tokens) -> Result, String> { _ => Err(IMPL_FOR_TRAIT_ERR)? }; - let idents = try_iter!(idents.into_iter()); - - Ok(idents) + idents.into_iter().collect() } fn auto_impl_expand(impl_for_traits: &[ImplForTrait], tokens: Tokens) -> Result {