Skip to content

Commit

Permalink
Refactor to prepare for follow-on change
Browse files Browse the repository at this point in the history
  • Loading branch information
hannobraun committed Sep 27, 2023
1 parent d9bc6fb commit a7c3b62
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions crates/fj-core/src/objects/handles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,31 @@ impl<T> Handles<T> {
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()
}
}

Expand Down

0 comments on commit a7c3b62

Please sign in to comment.