returning a vec creates much more readable code

This commit is contained in:
Christof Petig 2022-07-13 23:03:58 +02:00
parent 3bdeb77268
commit 197f84a360

View File

@ -316,25 +316,26 @@ lazy_static! {
// expand this loot specification towards individual item descriptions // expand this loot specification towards individual item descriptions
// partial duplicate of random_weapon_primary_component // partial duplicate of random_weapon_primary_component
// returning an Iterator is difficult due to the branch and it is always used as
// a vec afterwards
pub fn expand_primary_component( pub fn expand_primary_component(
tool: ToolKind, tool: ToolKind,
material: Material, material: Material,
hand_restriction: Option<inventory::item::Hands>, hand_restriction: Option<inventory::item::Hands>,
) -> Option<impl Iterator<Item = ItemDefinitionIdOwned>> { ) -> Vec<ItemDefinitionIdOwned> {
if let Some(material_id) = material.asset_identifier() { if let Some(material_id) = material.asset_identifier() {
Some( PRIMARY_COMPONENT_POOL
PRIMARY_COMPONENT_POOL .get(&(tool, material_id.to_owned()))
.get(&(tool, material_id.to_owned())) .into_iter()
.into_iter() .flatten()
.flatten() .filter(move |(_comp, hand)| match (hand_restriction, *hand) {
.filter(move |(_comp, hand)| match (hand_restriction, *hand) { (Some(restriction), Some(hand)) => restriction == hand,
(Some(restriction), Some(hand)) => restriction == hand, (None, _) | (_, None) => true,
(None, _) | (_, None) => true, })
}) .map(|e| e.0.clone())
.map(|e| e.0.clone()), .collect()
)
} else { } else {
None Vec::new()
} }
} }
@ -389,8 +390,7 @@ impl From<Vec<(f32, LootSpec<String>)>> for ProbabilityFile {
material, material,
hands, hands,
} => { } => {
let mut primary = expand_primary_component(tool, material, hands) let mut primary = expand_primary_component(tool, material, hands);
.map_or(Vec::new(), |it| it.collect());
let secondary: Vec<ItemDefinitionIdOwned> = let secondary: Vec<ItemDefinitionIdOwned> =
expand_secondary_component(tool, material, hands).collect(); expand_secondary_component(tool, material, hands).collect();
let freq = if primary.is_empty() || secondary.is_empty() { let freq = if primary.is_empty() || secondary.is_empty() {
@ -422,8 +422,7 @@ impl From<Vec<(f32, LootSpec<String>)>> for ProbabilityFile {
material, material,
hands, hands,
} => { } => {
let mut res = expand_primary_component(tool, material, hands) let mut res = expand_primary_component(tool, material, hands);
.map_or(Vec::new(), |it| it.collect());
let freq = if res.is_empty() { let freq = if res.is_empty() {
0.0 0.0
} else { } else {