Skip to content

Commit

Permalink
Merge pull request #1978 from hannobraun/insert
Browse files Browse the repository at this point in the history
Make `IsInserted` more useful
  • Loading branch information
hannobraun authored Aug 2, 2023
2 parents aff3e5d + e82d79b commit c930a55
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 29 deletions.
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

0 comments on commit c930a55

Please sign in to comment.