Add `Insert` trait

This commit is contained in:
Hanno Braun 2022-11-09 20:39:16 +01:00
parent 9288a9bd36
commit 00618eb151
2 changed files with 54 additions and 0 deletions

View File

@ -0,0 +1,53 @@
//! Convenience trait to insert objects into their respective stores
//!
//! See [`Insert`].
use crate::{
objects::{
Curve, Cycle, Face, GlobalCurve, GlobalEdge, GlobalVertex, HalfEdge,
Objects, Shell, Sketch, Solid, Surface, SurfaceVertex, Vertex,
},
storage::Handle,
validate::Validate,
};
/// Convenience trait to insert objects into their respective stores
pub trait Insert: Sized + Validate {
// TASK: Make error more specific.
/// Insert the object into its respective store
fn insert(
self,
objects: &Objects,
) -> Result<Handle<Self>, <Self as Validate>::Error>;
}
macro_rules! impl_insert {
($($ty:ty, $store:ident;)*) => {
$(
impl Insert for $ty {
fn insert(
self,
objects: &Objects,
) -> Result<Handle<Self>, <Self as Validate>::Error> {
objects.$store.insert(self)
}
}
)*
};
}
impl_insert!(
Curve, curves;
Cycle, cycles;
Face, faces;
GlobalCurve, global_curves;
GlobalEdge, global_edges;
GlobalVertex, global_vertices;
HalfEdge, half_edges;
Shell, shells;
Sketch, sketches;
Solid, solids;
SurfaceVertex, surface_vertices;
Surface, surfaces;
Vertex, vertices;
);

View File

@ -89,6 +89,7 @@
pub mod algorithms; pub mod algorithms;
pub mod builder; pub mod builder;
pub mod insert;
pub mod iter; pub mod iter;
pub mod objects; pub mod objects;
pub mod partial; pub mod partial;