From 7990e85865237fbd266868a7499d48a0fe3e1c8e Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 27 Sep 2023 11:11:16 +0200 Subject: [PATCH] Add `Handles::replace` --- crates/fj-core/src/objects/handles.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/crates/fj-core/src/objects/handles.rs b/crates/fj-core/src/objects/handles.rs index 51c910ce4..d9ee8b6a5 100644 --- a/crates/fj-core/src/objects/handles.rs +++ b/crates/fj-core/src/objects/handles.rs @@ -111,6 +111,27 @@ impl Handles { handle: &Handle, update: impl FnOnce(&Handle) -> Handle, ) -> Self + where + T: Debug + Ord, + { + self.replace(handle, |handle| [update(handle)]) + } + + /// Create a new instance in which the provided item has been replaced + /// + /// This is a more general version of [`Handles::update`] which can replace + /// a single item with multiple others. + /// + /// # Panics + /// + /// Panics, if the provided item is not present. + /// Panics, if the update results in a duplicate item. + #[must_use] + pub fn replace( + &self, + handle: &Handle, + replace: impl FnOnce(&Handle) -> [Handle; N], + ) -> Self where T: Debug + Ord, { @@ -135,10 +156,10 @@ impl Handles { before.push(next.clone()); } - let updated = update(handle); + let replaced = replace(handle); let after = iter; - before.into_iter().chain([updated]).chain(after).collect() + before.into_iter().chain(replaced).chain(after).collect() } }