Skip to content

Commit

Permalink
Merge pull request #2084 from hannobraun/handles
Browse files Browse the repository at this point in the history
Merge `Handles::replace_with_multiple` into `Handles::replace`
  • Loading branch information
hannobraun authored Nov 8, 2023
2 parents 37bb598 + bf66e1e commit bc2ee5d
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 35 deletions.
34 changes: 9 additions & 25 deletions crates/fj-core/src/objects/handles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,32 +143,10 @@ impl<T> Handles<T> {
///
/// Panics, if the update results in a duplicate item.
#[must_use]
pub fn replace(
pub fn replace<const N: usize>(
&self,
original: &Handle<T>,
replacement: Handle<T>,
) -> Option<Self>
where
T: Debug + Ord,
{
self.replace_with_multiple(original, [replacement])
}

/// Create a new instance in which the provided item has been replaced
///
/// Returns `None`, if the provided item is not present.
///
/// This is a more general version of [`Handles::replace`] which can replace
/// a single item with multiple others.
///
/// # Panics
///
/// Panics, if the replacement results in a duplicate item.
#[must_use]
pub fn replace_with_multiple<const N: usize>(
&self,
original: &Handle<T>,
replacement: [Handle<T>; N],
replacements: [Handle<T>; N],
) -> Option<Self>
where
T: Debug + Ord,
Expand Down Expand Up @@ -198,7 +176,13 @@ impl<T> Handles<T> {
// Let's make that a bit more explicit by renaming the variable.
let after = iter;

Some(before.into_iter().chain(replacement).chain(after).collect())
Some(
before
.into_iter()
.chain(replacements)
.chain(after)
.collect(),
)
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/fj-core/src/operations/update/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl UpdateCycle for Cycle {
) -> Self {
let edges = self
.half_edges()
.replace(handle, update(handle))
.replace(handle, [update(handle)])
.expect("Half-edge not found");
Cycle::new(edges)
}
Expand All @@ -72,7 +72,7 @@ impl UpdateCycle for Cycle {
) -> Self {
let edges = self
.half_edges()
.replace_with_multiple(handle, replace(handle))
.replace(handle, replace(handle))
.expect("Half-edge not found");
Cycle::new(edges)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/fj-core/src/operations/update/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl UpdateRegion for Region {
) -> Self {
let interiors = self
.interiors()
.replace(handle, update(handle))
.replace(handle, [update(handle)])
.expect("Cycle not found");
Region::new(self.exterior().clone(), interiors, self.color())
}
Expand All @@ -87,7 +87,7 @@ impl UpdateRegion for Region {
) -> Self {
let interiors = self
.interiors()
.replace_with_multiple(handle, replace(handle))
.replace(handle, replace(handle))
.expect("Cycle not found");
Region::new(self.exterior().clone(), interiors, self.color())
}
Expand Down
4 changes: 2 additions & 2 deletions crates/fj-core/src/operations/update/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl UpdateShell for Shell {
) -> Self {
let faces = self
.faces()
.replace(handle, update(handle))
.replace(handle, [update(handle)])
.expect("Face not found");
Shell::new(faces)
}
Expand All @@ -70,7 +70,7 @@ impl UpdateShell for Shell {
) -> Self {
let faces = self
.faces()
.replace_with_multiple(handle, replace(handle))
.replace(handle, replace(handle))
.expect("Face not found");
Shell::new(faces)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/fj-core/src/operations/update/sketch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl UpdateSketch for Sketch {
) -> Self {
let regions = self
.regions()
.replace(handle, update(handle))
.replace(handle, [update(handle)])
.expect("Region not found");
Sketch::new(regions)
}
Expand All @@ -65,7 +65,7 @@ impl UpdateSketch for Sketch {
) -> Self {
let regions = self
.regions()
.replace_with_multiple(handle, replace(handle))
.replace(handle, replace(handle))
.expect("Region not found");
Sketch::new(regions)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/fj-core/src/operations/update/solid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl UpdateSolid for Solid {
) -> Self {
let shells = self
.shells()
.replace(handle, update(handle))
.replace(handle, [update(handle)])
.expect("Shell not found");
Solid::new(shells)
}
Expand All @@ -72,7 +72,7 @@ impl UpdateSolid for Solid {
) -> Self {
let shells = self
.shells()
.replace_with_multiple(handle, replace(handle))
.replace(handle, replace(handle))
.expect("Shell not found");
Solid::new(shells)
}
Expand Down

0 comments on commit bc2ee5d

Please sign in to comment.