Simplify iterator usage

This commit is contained in:
Jonas Schievink 2017-09-22 18:10:00 +02:00
parent b1777ebd68
commit 70e243694d

View file

@ -160,20 +160,15 @@ fn init(app: &App, main: &mut Vec<Tokens>, root: &mut Vec<Tokens>) {
let mut ret = None; let mut ret = None;
let mut mod_items = vec![]; let mut mod_items = vec![];
// Write resources usable by `init`, if any let (init_resources, late_resources): (Vec<_>, Vec<_>) = app.resources.iter()
.partition(|&(_, res)| res.expr.is_some());
if !init_resources.is_empty() {
// Are there any resources that have an initializer? Those can be used by `init`.
let has_initialized_resources = app.resources.iter()
.find(|&(_, res)| res.expr.is_some()).is_some();
if has_initialized_resources {
let mut fields = vec![]; let mut fields = vec![];
let mut lifetime = None; let mut lifetime = None;
let mut rexprs = vec![]; let mut rexprs = vec![];
for (name, resource) in app.resources.iter() for (name, resource) in init_resources {
.filter(|&(_, res)| res.expr.is_some()) {
let _name = Ident::new(format!("_{}", name.as_ref())); let _name = Ident::new(format!("_{}", name.as_ref()));
lifetime = Some(quote!('a)); lifetime = Some(quote!('a));
@ -213,17 +208,15 @@ fn init(app: &App, main: &mut Vec<Tokens>, root: &mut Vec<Tokens>) {
exprs.push(quote!(init::Resources::new())); exprs.push(quote!(init::Resources::new()));
} }
let mut late_resources = vec![]; // Initialization statements for late resources
let has_late_resources = app.resources.iter() let mut late_resource_init = vec![];
.find(|&(_, res)| res.expr.is_none()).is_some();
if has_late_resources { if !late_resources.is_empty() {
// `init` must initialize and return resources // `init` must initialize and return resources
let mut fields = vec![]; let mut fields = vec![];
for (name, resource) in app.resources.iter() for (name, resource) in late_resources {
.filter(|&(_, res)| res.expr.is_none()) {
let _name = Ident::new(format!("_{}", name.as_ref())); let _name = Ident::new(format!("_{}", name.as_ref()));
let ty = &resource.ty; let ty = &resource.ty;
@ -232,7 +225,7 @@ fn init(app: &App, main: &mut Vec<Tokens>, root: &mut Vec<Tokens>) {
pub #name: #ty, pub #name: #ty,
}); });
late_resources.push(quote! { late_resource_init.push(quote! {
#_name = #krate::UntaggedOption { some: _late_resources.#name }; #_name = #krate::UntaggedOption { some: _late_resources.#name };
}); });
} }
@ -316,7 +309,7 @@ fn init(app: &App, main: &mut Vec<Tokens>, root: &mut Vec<Tokens>) {
#krate::atomic(unsafe { &mut #krate::Threshold::new(0) }, |_t| unsafe { #krate::atomic(unsafe { &mut #krate::Threshold::new(0) }, |_t| unsafe {
let _late_resources = init(#(#exprs,)*); let _late_resources = init(#(#exprs,)*);
#(#late_resources)* #(#late_resource_init)*
#(#exceptions)* #(#exceptions)*
#(#interrupts)* #(#interrupts)*