From a7c3b625fe55e913315eae6eca46dc5677477cff Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 27 Sep 2023 11:09:26 +0200 Subject: [PATCH] Refactor to prepare for follow-on change --- crates/fj-core/src/objects/handles.rs | 30 +++++++++++++++++---------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/crates/fj-core/src/objects/handles.rs b/crates/fj-core/src/objects/handles.rs index 3c490d1f9..51c910ce4 100644 --- a/crates/fj-core/src/objects/handles.rs +++ b/crates/fj-core/src/objects/handles.rs @@ -114,23 +114,31 @@ impl Handles { where T: Debug + Ord, { - let mut updated = Some(update(handle)); + let mut iter = self.iter().cloned().peekable(); + + // Collect all items before the item we want to update. + let mut before = Vec::new(); + loop { + let h = match iter.peek() { + Some(h) => h, + None => panic!("Item not found"), + }; - let items = self.iter().map(|h| { if h.id() == handle.id() { - updated - .take() - .expect("`Handles` should not contain same item twice") - } else { - h.clone() + // Found the item we want to update. Remove it from the + // iterator, then move on. + iter.next(); + break; } - }); - let handles = items.collect(); + let next = iter.next().expect("Peek just returned `Some`"); + before.push(next.clone()); + } - assert!(updated.is_none(), "Item not found"); + let updated = update(handle); + let after = iter; - handles + before.into_iter().chain([updated]).chain(after).collect() } }