Skip to content

Commit

Permalink
Add Handles::replace
Browse files Browse the repository at this point in the history
  • Loading branch information
hannobraun committed Sep 27, 2023
1 parent a7c3b62 commit 7990e85
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions crates/fj-core/src/objects/handles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,27 @@ impl<T> Handles<T> {
handle: &Handle<T>,
update: impl FnOnce(&Handle<T>) -> Handle<T>,
) -> 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<const N: usize>(
&self,
handle: &Handle<T>,
replace: impl FnOnce(&Handle<T>) -> [Handle<T>; N],
) -> Self
where
T: Debug + Ord,
{
Expand All @@ -135,10 +156,10 @@ impl<T> Handles<T> {
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()
}
}

Expand Down

0 comments on commit 7990e85

Please sign in to comment.