1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use crate::{
    objects::{
        Cycle, Face, GlobalEdge, HalfEdge, Objects, Shell, Sketch, Solid,
        Surface, Vertex,
    },
    services::{Operation, Service},
    storage::Handle,
};

/// Insert an object into its respective store
///
/// This is the only primitive operation that is directly understood by
/// `Service<Objects>`. All other operations are built on top of it.
pub trait Insert: Sized {
    /// Insert the object into its respective store
    fn insert(self, objects: &mut Service<Objects>) -> Handle<Self>;
}

macro_rules! impl_insert {
    ($($ty:ty, $store:ident;)*) => {
        $(
            impl Insert for $ty {
                fn insert(self, objects: &mut Service<Objects>) -> Handle<Self>
                {
                    let handle = objects.$store.reserve();
                    let object = (handle.clone(), self).into();
                    objects.execute(Operation::InsertObject { object });
                    handle
                }
            }
        )*
    };
}

impl_insert!(
    Cycle, cycles;
    Face, faces;
    GlobalEdge, global_edges;
    HalfEdge, half_edges;
    Shell, shells;
    Sketch, sketches;
    Solid, solids;
    Surface, surfaces;
    Vertex, vertices;
);