Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove try_iter macro #2

Merged
merged 1 commit into from
Aug 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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