Skip to content

Commit

Permalink
Merge pull request #1810 from hannobraun/object
Browse files Browse the repository at this point in the history
Derive `Eq`/`Ord` for `Object`
  • Loading branch information
hannobraun authored May 2, 2023
2 parents a8acb9e + c895a1c commit 9ed81d0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
14 changes: 14 additions & 0 deletions crates/fj-kernel/src/objects/full/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ impl HalfEdge {
///
/// See [`HalfEdge`]'s documentation for more information on the relationship
/// between [`HalfEdge`] and `GlobalEdge`.
///
/// # Equality
///
/// `GlobalEdge` contains no data and exists purely to be used within a
/// `Handle`, where `Handle::id` can be used to compare different instances of
/// `GlobalEdge`.
///
/// If `GlobalEdge` had `Eq`/`PartialEq` implementations, it containing no data
/// would mean that all instances of `GlobalEdge` would be considered equal.
/// This would be very error-prone.
///
/// If you need to reference a `GlobalEdge` from a struct that needs to derive
/// `Eq`/`Ord`/..., you can use `HandleWrapper<GlobalEdge>` to do that. It will
/// use `Handle::id` to provide those `Eq`/`Ord`/... implementations.
#[derive(Clone, Debug, Default, Hash)]
pub struct GlobalEdge {}

Expand Down
13 changes: 13 additions & 0 deletions crates/fj-kernel/src/objects/full/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@
/// between distinct vertices can be configured using the respective field in
/// [`ValidationConfig`].
///
/// # Equality
///
/// `Vertex` contains no data and exists purely to be used within a `Handle`,
/// where `Handle::id` can be used to compare different instances of `Vertex`.
///
/// If `Vertex` had `Eq`/`PartialEq` implementations, it containing no data
/// would mean that all instances of `Vertex` would be considered equal. This
/// would be very error-prone.
///
/// If you need to reference a `Vertex` from a struct that needs to derive
/// `Eq`/`Ord`/..., you can use `HandleWrapper<Vertex>` to do that. It will
/// use `Handle::id` to provide those `Eq`/`Ord`/... implementations.
///
/// [`ValidationConfig`]: crate::validate::ValidationConfig
#[derive(Clone, Copy, Debug, Default, Hash)]
pub struct Vertex {}
Expand Down
26 changes: 14 additions & 12 deletions crates/fj-kernel/src/objects/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
Cycle, Face, GlobalEdge, HalfEdge, Objects, Shell, Sketch, Solid,
Surface, Vertex,
},
storage::{Handle, ObjectId},
storage::{Handle, HandleWrapper, ObjectId},
validate::{Validate, ValidationError},
};

Expand All @@ -14,7 +14,7 @@ macro_rules! object {
/// This enum is generic over the form that the object takes. An
/// `Object<Bare>` contains bare objects, like `Curve`. An
/// `Object<BehindHandle>` contains handles, like `Handle<Curve>`.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum Object<F: Form> {
$(
#[doc = concat!("A ", $name)]
Expand All @@ -39,8 +39,10 @@ macro_rules! object {
match self {
$(
Self::$ty((handle, object)) => {
objects.$store.insert(handle.clone(), object);
handle.into()
objects.$store.insert(
handle.clone().into(), object
);
handle.0.into()
}
)*
}
Expand All @@ -60,7 +62,7 @@ macro_rules! object {
fn from(object: Object<WithHandle>) -> Self {
match object {
$(
Object::$ty((handle, _)) => Self::$ty(handle),
Object::$ty((handle, _)) => Self::$ty(handle.into()),
)*
}
}
Expand All @@ -75,13 +77,13 @@ macro_rules! object {

impl From<Handle<$ty>> for Object<BehindHandle> {
fn from(object: Handle<$ty>) -> Self {
Self::$ty(object)
Self::$ty(object.into())
}
}

impl From<(Handle<$ty>, $ty)> for Object<WithHandle> {
fn from((handle, object): (Handle<$ty>, $ty)) -> Self {
Self::$ty((handle, object))
Self::$ty((handle.into(), object))
}
}
)*
Expand Down Expand Up @@ -110,25 +112,25 @@ pub trait Form {
}

/// Implementation of [`Form`] for bare objects
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct Bare;

impl Form for Bare {
type Form<T> = T;
}

/// Implementation of [`Form`] for objects behind a handle
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct BehindHandle;

impl Form for BehindHandle {
type Form<T> = Handle<T>;
type Form<T> = HandleWrapper<T>;
}

/// Implementation of [`Form`] for objects that are paired with their handle
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct WithHandle;

impl Form for WithHandle {
type Form<T> = (Handle<T>, T);
type Form<T> = (HandleWrapper<T>, T);
}

0 comments on commit 9ed81d0

Please sign in to comment.