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

Make IsInserted more useful #1978

Merged
merged 4 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use crate::{
Curve, Cycle, Face, GlobalEdge, HalfEdge, Region, Shell, Sketch, Solid,
Surface, Vertex,
},
operations::{Polygon, TetrahedronShell},
services::Services,
storage::Handle,
};

use super::{Polygon, TetrahedronShell};
use super::{IsInsertedNo, IsInsertedYes};

/// Insert an object into its respective store
///
Expand Down Expand Up @@ -56,33 +57,6 @@ impl_insert!(
Vertex, vertices;
);

/// Indicate whether an object has been inserted
///
/// Intended to be used as a type parameter bound for structs that need to track
/// whether their contents have been inserted or not.
pub trait IsInserted {
/// The type of the object for which the insertion status is tracked
type T<T>;
}

/// Indicate that an object has been inserted
///
/// See [`IsInserted`].
pub struct IsInsertedYes;

impl IsInserted for IsInsertedYes {
type T<T> = Handle<T>;
}

/// Indicate that an object has not been inserted
///
/// See [`IsInserted`].
pub struct IsInsertedNo;

impl IsInserted for IsInsertedNo {
type T<T> = T;
}

impl<const D: usize> Insert for Polygon<D, IsInsertedNo> {
type Inserted = Polygon<D, IsInsertedYes>;

Expand Down
30 changes: 30 additions & 0 deletions crates/fj-core/src/operations/insert/is_inserted.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use std::borrow::Borrow;

use crate::storage::Handle;

/// Indicate whether an object has been inserted
///
/// Intended to be used as a type parameter bound for structs that need to track
/// whether their contents have been inserted or not.
pub trait IsInserted {
/// The type of the object for which the insertion status is tracked
type T<T>: Borrow<T>;
}

/// Indicate that an object has been inserted
///
/// See [`IsInserted`].
pub struct IsInsertedYes;

impl IsInserted for IsInsertedYes {
type T<T> = Handle<T>;
}

/// Indicate that an object has not been inserted
///
/// See [`IsInserted`].
pub struct IsInsertedNo;

impl IsInserted for IsInsertedNo {
type T<T> = T;
}
7 changes: 7 additions & 0 deletions crates/fj-core/src/operations/insert/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod insert_trait;
mod is_inserted;

pub use self::{
insert_trait::Insert,
is_inserted::{IsInserted, IsInsertedNo, IsInsertedYes},
};
10 changes: 9 additions & 1 deletion crates/fj-core/src/storage/handle.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::{any::type_name, cmp::Ordering, fmt, hash::Hash, ops::Deref};
use std::{
any::type_name, borrow::Borrow, cmp::Ordering, fmt, hash::Hash, ops::Deref,
};

use super::{blocks::Index, store::StoreInner};

Expand Down Expand Up @@ -82,6 +84,12 @@ impl<T> Deref for Handle<T> {
}
}

impl<T> Borrow<T> for Handle<T> {
fn borrow(&self) -> &T {
self.deref()
}
}

impl<T> Clone for Handle<T> {
fn clone(&self) -> Self {
Self {
Expand Down