From 5000c9872c579d411fd6f18ee20c278af496dc54 Mon Sep 17 00:00:00 2001 From: Christof Petig Date: Mon, 23 Nov 2020 23:28:21 +0100 Subject: [PATCH] use iter once.chain(once).flatten trick to deduplicate SiteInfo generation --- world/src/lib.rs | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/world/src/lib.rs b/world/src/lib.rs index c1fe4091d9..cc7bcfaf1f 100644 --- a/world/src/lib.rs +++ b/world/src/lib.rs @@ -118,20 +118,16 @@ impl World { self.civs() .caves .iter() - .map(|(_, info)| world_msg::SiteInfo { - name: Some(info.name.clone()), + .map(|(_, info)| { + // separate the two locations, combine with name + std::iter::once((info.name.clone(), info.location.0)) + .chain(std::iter::once((info.name.clone(), info.location.1))) + }) + .flatten() // unwrap inner iteration + .map(|(name, pos)| world_msg::SiteInfo { + name: Some(name), kind: world_msg::SiteKind::Cave, - wpos: info.location.0, - }), - ) - .chain( - self.civs() - .caves - .iter() - .map(|(_, info)| world_msg::SiteInfo { - name: Some(info.name.clone()), - kind: world_msg::SiteKind::Cave, - wpos: info.location.1, + wpos: pos, }), ) .collect(),