fj_core/operations/update/
solid.rs

1use crate::{
2    objects::{Shell, Solid},
3    operations::{derive::DeriveFrom, insert::Insert},
4    storage::Handle,
5    Core,
6};
7
8/// Update a [`Solid`]
9pub trait UpdateSolid {
10    /// Add a shell to the solid
11    #[must_use]
12    fn add_shells<T>(
13        &self,
14        shells: impl IntoIterator<Item = T>,
15        core: &mut Core,
16    ) -> Self
17    where
18        T: Insert<Inserted = Handle<Shell>>;
19
20    /// Update a shell of the solid
21    ///
22    /// # Panics
23    ///
24    /// Panics, if the object can't be found.
25    ///
26    /// Panics, if the update results in multiple handles referencing the same object.
27    #[must_use]
28    fn update_shell<T, R>(
29        &self,
30        handle: &Handle<Shell>,
31        update: impl FnOnce(&Handle<Shell>, &mut Core) -> R,
32        core: &mut Core,
33    ) -> Self
34    where
35        T: Insert<Inserted = Handle<Shell>>,
36        R: IntoIterator<Item = T>;
37}
38
39impl UpdateSolid for Solid {
40    fn add_shells<T>(
41        &self,
42        shells: impl IntoIterator<Item = T>,
43        core: &mut Core,
44    ) -> Self
45    where
46        T: Insert<Inserted = Handle<Shell>>,
47    {
48        let shells = shells.into_iter().map(|shell| shell.insert(core));
49        let shells = self.shells().iter().cloned().chain(shells);
50        Solid::new(shells)
51    }
52
53    fn update_shell<T, R>(
54        &self,
55        handle: &Handle<Shell>,
56        update: impl FnOnce(&Handle<Shell>, &mut Core) -> R,
57        core: &mut Core,
58    ) -> Self
59    where
60        T: Insert<Inserted = Handle<Shell>>,
61        R: IntoIterator<Item = T>,
62    {
63        let shells = self
64            .shells()
65            .replace(
66                handle,
67                update(handle, core).into_iter().map(|object| {
68                    object.insert(core).derive_from(handle, core)
69                }),
70            )
71            .expect("Shell not found");
72        Solid::new(shells)
73    }
74}