Skip to content

Commit

Permalink
Merge pull request #2 from KodrAus/chore/remove-try-iter
Browse files Browse the repository at this point in the history
Remove `try_iter` macro
  • Loading branch information
KodrAus committed Aug 1, 2017
2 parents d62b6fc + 6616700 commit eff358d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 53 deletions.
60 changes: 29 additions & 31 deletions src/impl_as_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,40 @@ use model::*;
pub fn build(component: &AutoImpl, ref_ty: Trait) -> Result<Tokens, String> {
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::<Result<Vec<_>, _>>()?;

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::<Result<Vec<_>, _>>()?;

let (trait_tys, impl_lifetimes, impl_tys, where_clauses) = component.split_generics();

Expand Down
23 changes: 1 addition & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,9 +61,7 @@ fn parse_impl_types(tokens: Tokens) -> Result<Vec<ImplForTrait>, 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<Tokens, String> {
Expand Down

0 comments on commit eff358d

Please sign in to comment.