Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement operations::replace traits for bare objects #2092

Merged
merged 6 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Make ReplaceOutput more flexible
Make it possible to specify the types for the original and updated
objects separately. This will make it possible to implement the replace
traits for the bare object types too.
  • Loading branch information
hannobraun committed Nov 14, 2023
commit 10b87867571fdff13f90fce4a2e66df85197a9ed
16 changes: 8 additions & 8 deletions crates/fj-core/src/operations/replace/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub trait ReplaceCurve: Sized {
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
) -> ReplaceOutput<Self::BareObject>;
) -> ReplaceOutput<Self, Self::BareObject>;
}

impl ReplaceCurve for Handle<HalfEdge> {
Expand All @@ -34,7 +34,7 @@ impl ReplaceCurve for Handle<HalfEdge> {
original: &Handle<Curve>,
replacement: Handle<Curve>,
_: &mut Services,
) -> ReplaceOutput<Self::BareObject> {
) -> ReplaceOutput<Self, Self::BareObject> {
if original.id() == self.curve().id() {
ReplaceOutput::Updated(self.update_curve(|_| replacement))
} else {
Expand All @@ -51,7 +51,7 @@ impl ReplaceCurve for Handle<Cycle> {
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
) -> ReplaceOutput<Self::BareObject> {
) -> ReplaceOutput<Self, Self::BareObject> {
let mut replacement_happened = false;

let mut half_edges = Vec::new();
Expand Down Expand Up @@ -81,7 +81,7 @@ impl ReplaceCurve for Handle<Region> {
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
) -> ReplaceOutput<Self::BareObject> {
) -> ReplaceOutput<Self, Self::BareObject> {
let mut replacement_happened = false;

let exterior = self.exterior().replace_curve(
Expand Down Expand Up @@ -119,7 +119,7 @@ impl ReplaceCurve for Handle<Sketch> {
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
) -> ReplaceOutput<Self::BareObject> {
) -> ReplaceOutput<Self, Self::BareObject> {
let mut replacement_happened = false;

let mut regions = Vec::new();
Expand All @@ -146,7 +146,7 @@ impl ReplaceCurve for Handle<Face> {
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
) -> ReplaceOutput<Self::BareObject> {
) -> ReplaceOutput<Self, Self::BareObject> {
let region =
self.region().replace_curve(original, replacement, services);

Expand All @@ -169,7 +169,7 @@ impl ReplaceCurve for Handle<Shell> {
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
) -> ReplaceOutput<Self::BareObject> {
) -> ReplaceOutput<Self, Self::BareObject> {
let mut replacement_happened = false;

let mut faces = Vec::new();
Expand All @@ -196,7 +196,7 @@ impl ReplaceCurve for Handle<Solid> {
original: &Handle<Curve>,
replacement: Handle<Curve>,
services: &mut Services,
) -> ReplaceOutput<Self::BareObject> {
) -> ReplaceOutput<Self, Self::BareObject> {
let mut replacement_happened = false;

let mut shells = Vec::new();
Expand Down
14 changes: 7 additions & 7 deletions crates/fj-core/src/operations/replace/half_edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub trait ReplaceHalfEdge: Sized {
original: &Handle<HalfEdge>,
replacements: [Handle<HalfEdge>; N],
services: &mut Services,
) -> ReplaceOutput<Self::BareObject>;
) -> ReplaceOutput<Self, Self::BareObject>;
}

impl ReplaceHalfEdge for Handle<Cycle> {
Expand All @@ -33,7 +33,7 @@ impl ReplaceHalfEdge for Handle<Cycle> {
original: &Handle<HalfEdge>,
replacements: [Handle<HalfEdge>; N],
_: &mut Services,
) -> ReplaceOutput<Self::BareObject> {
) -> ReplaceOutput<Self, Self::BareObject> {
if let Some(half_edges) =
self.half_edges().replace(original, replacements)
{
Expand All @@ -52,7 +52,7 @@ impl ReplaceHalfEdge for Handle<Region> {
original: &Handle<HalfEdge>,
replacements: [Handle<HalfEdge>; N],
services: &mut Services,
) -> ReplaceOutput<Self::BareObject> {
) -> ReplaceOutput<Self, Self::BareObject> {
let mut replacement_happened = false;

let exterior = self.exterior().replace_half_edge(
Expand Down Expand Up @@ -93,7 +93,7 @@ impl ReplaceHalfEdge for Handle<Sketch> {
original: &Handle<HalfEdge>,
replacements: [Handle<HalfEdge>; N],
services: &mut Services,
) -> ReplaceOutput<Self::BareObject> {
) -> ReplaceOutput<Self, Self::BareObject> {
let mut replacement_happened = false;

let mut regions = Vec::new();
Expand Down Expand Up @@ -123,7 +123,7 @@ impl ReplaceHalfEdge for Handle<Face> {
original: &Handle<HalfEdge>,
replacements: [Handle<HalfEdge>; N],
services: &mut Services,
) -> ReplaceOutput<Self::BareObject> {
) -> ReplaceOutput<Self, Self::BareObject> {
let region =
self.region()
.replace_half_edge(original, replacements, services);
Expand All @@ -147,7 +147,7 @@ impl ReplaceHalfEdge for Handle<Shell> {
original: &Handle<HalfEdge>,
replacements: [Handle<HalfEdge>; N],
services: &mut Services,
) -> ReplaceOutput<Self::BareObject> {
) -> ReplaceOutput<Self, Self::BareObject> {
let mut replacement_happened = false;

let mut faces = Vec::new();
Expand Down Expand Up @@ -177,7 +177,7 @@ impl ReplaceHalfEdge for Handle<Solid> {
original: &Handle<HalfEdge>,
replacements: [Handle<HalfEdge>; N],
services: &mut Services,
) -> ReplaceOutput<Self::BareObject> {
) -> ReplaceOutput<Self, Self::BareObject> {
let mut replacement_happened = false;

let mut shells = Vec::new();
Expand Down
10 changes: 6 additions & 4 deletions crates/fj-core/src/operations/replace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ use super::insert::Insert;
/// See [module documentation] for more information.
///
/// [module documentation]: self
pub enum ReplaceOutput<T> {
pub enum ReplaceOutput<Original, Updated> {
/// The original object that the replace operation was called on
///
/// If this variant is returned, the object to be replaced was not
/// referenced, and no replacement happened.
Original(Handle<T>),
Original(Original),

/// The updated version of the object that the operation was called on
///
Expand All @@ -125,15 +125,17 @@ pub enum ReplaceOutput<T> {
/// modeling process. The validation infrastructure currently provides no
/// good ways to deal with invalid intermediate results, even if the end
/// result ends up valid.
Updated(T),
Updated(Updated),
}

impl<T> ReplaceOutput<T> {
impl<Original, Updated> ReplaceOutput<Original, Updated> {
/// Indicate whether the original object was updated
pub fn was_updated(&self) -> bool {
matches!(self, ReplaceOutput::Updated(_))
}
}

impl<T> ReplaceOutput<Handle<T>, T> {
/// Return the original object, or insert the updated on and return handle
pub fn into_inner(self, services: &mut Services) -> Handle<T>
where
Expand Down
16 changes: 8 additions & 8 deletions crates/fj-core/src/operations/replace/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub trait ReplaceVertex: Sized {
original: &Handle<Vertex>,
replacement: Handle<Vertex>,
services: &mut Services,
) -> ReplaceOutput<Self::BareObject>;
) -> ReplaceOutput<Self, Self::BareObject>;
}

impl ReplaceVertex for Handle<HalfEdge> {
Expand All @@ -34,7 +34,7 @@ impl ReplaceVertex for Handle<HalfEdge> {
original: &Handle<Vertex>,
replacement: Handle<Vertex>,
_: &mut Services,
) -> ReplaceOutput<Self::BareObject> {
) -> ReplaceOutput<Self, Self::BareObject> {
if original.id() == self.start_vertex().id() {
ReplaceOutput::Updated(self.update_start_vertex(|_| replacement))
} else {
Expand All @@ -51,7 +51,7 @@ impl ReplaceVertex for Handle<Cycle> {
original: &Handle<Vertex>,
replacement: Handle<Vertex>,
services: &mut Services,
) -> ReplaceOutput<Self::BareObject> {
) -> ReplaceOutput<Self, Self::BareObject> {
let mut replacement_happened = false;

let mut half_edges = Vec::new();
Expand Down Expand Up @@ -81,7 +81,7 @@ impl ReplaceVertex for Handle<Region> {
original: &Handle<Vertex>,
replacement: Handle<Vertex>,
services: &mut Services,
) -> ReplaceOutput<Self::BareObject> {
) -> ReplaceOutput<Self, Self::BareObject> {
let mut replacement_happened = false;

let exterior = self.exterior().replace_vertex(
Expand Down Expand Up @@ -119,7 +119,7 @@ impl ReplaceVertex for Handle<Sketch> {
original: &Handle<Vertex>,
replacement: Handle<Vertex>,
services: &mut Services,
) -> ReplaceOutput<Self::BareObject> {
) -> ReplaceOutput<Self, Self::BareObject> {
let mut replacement_happened = false;

let mut regions = Vec::new();
Expand All @@ -146,7 +146,7 @@ impl ReplaceVertex for Handle<Face> {
original: &Handle<Vertex>,
replacement: Handle<Vertex>,
services: &mut Services,
) -> ReplaceOutput<Self::BareObject> {
) -> ReplaceOutput<Self, Self::BareObject> {
let region =
self.region()
.replace_vertex(original, replacement, services);
Expand All @@ -170,7 +170,7 @@ impl ReplaceVertex for Handle<Shell> {
original: &Handle<Vertex>,
replacement: Handle<Vertex>,
services: &mut Services,
) -> ReplaceOutput<Self::BareObject> {
) -> ReplaceOutput<Self, Self::BareObject> {
let mut replacement_happened = false;

let mut faces = Vec::new();
Expand All @@ -197,7 +197,7 @@ impl ReplaceVertex for Handle<Solid> {
original: &Handle<Vertex>,
replacement: Handle<Vertex>,
services: &mut Services,
) -> ReplaceOutput<Self::BareObject> {
) -> ReplaceOutput<Self, Self::BareObject> {
let mut replacement_happened = false;

let mut shells = Vec::new();
Expand Down