mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Removed vec allocation for component recipe iterators
This commit is contained in:
parent
a5a517916e
commit
fceb48c3ac
@ -668,14 +668,56 @@ impl ComponentRecipe {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn inputs(&self) -> impl ExactSizeIterator<Item = (&RecipeInput, u32)> {
|
pub fn inputs(&self) -> impl ExactSizeIterator<Item = (&RecipeInput, u32)> {
|
||||||
let material = core::iter::once(&self.material);
|
pub struct ComponentRecipeInputsIterator<'a> {
|
||||||
let modifier = self.modifier.iter();
|
material: bool,
|
||||||
let additional_inputs = self.additional_inputs.iter();
|
modifier: bool,
|
||||||
material.chain(modifier.chain(additional_inputs))
|
index: usize,
|
||||||
.map(|(item_def, amount)| (item_def, *amount))
|
recipe: &'a ComponentRecipe,
|
||||||
// Hack, not sure how to get exact size iterator from multiple chains.
|
}
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.into_iter()
|
impl<'a> Iterator for ComponentRecipeInputsIterator<'a> {
|
||||||
|
type Item = &'a (RecipeInput, u32);
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<&'a (RecipeInput, u32)> {
|
||||||
|
if !self.material {
|
||||||
|
self.material = true;
|
||||||
|
Some(&self.recipe.material)
|
||||||
|
} else if !self.modifier {
|
||||||
|
self.modifier = true;
|
||||||
|
if self.recipe.modifier.is_some() {
|
||||||
|
self.recipe.modifier.as_ref()
|
||||||
|
} else {
|
||||||
|
self.index += 1;
|
||||||
|
self.recipe.additional_inputs.get(self.index - 1)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
self.index += 1;
|
||||||
|
self.recipe.additional_inputs.get(self.index - 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> IntoIterator for &'a ComponentRecipe {
|
||||||
|
type IntoIter = ComponentRecipeInputsIterator<'a>;
|
||||||
|
type Item = &'a (RecipeInput, u32);
|
||||||
|
|
||||||
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
|
ComponentRecipeInputsIterator {
|
||||||
|
material: false,
|
||||||
|
modifier: false,
|
||||||
|
index: 0,
|
||||||
|
recipe: self,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> ExactSizeIterator for ComponentRecipeInputsIterator<'a> {
|
||||||
|
fn len(&self) -> usize {
|
||||||
|
1 + self.recipe.modifier.is_some() as usize + self.recipe.additional_inputs.len()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.into_iter().map(|(recipe, amount)| (recipe, *amount))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1335,9 +1335,7 @@ impl<'a> Widget for Crafting<'a> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
RecipeKind::Simple => {
|
RecipeKind::Simple => events.push(Event::CraftRecipe(recipe_name)),
|
||||||
events.push(Event::CraftRecipe(recipe_name))
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user