unc_primitives/
sandbox.rs

1#[cfg(feature = "sandbox")]
2pub mod state_patch {
3    use crate::state_record::StateRecord;
4
5    /// Changes to the state to be applied via sandbox-only state patching
6    /// feature.
7    ///
8    /// As we only expose this functionality for sandbox, we make sure that the
9    /// object can be non-empty only if `sandbox` feature is enabled.  On
10    /// non-sandbox build, this struct is ZST and its methods are essentially
11    /// short-circuited by treating the type as always empty.
12    #[derive(Default)]
13    pub struct SandboxStatePatch {
14        records: Vec<StateRecord>,
15    }
16
17    impl SandboxStatePatch {
18        pub fn new(records: Vec<StateRecord>) -> SandboxStatePatch {
19            SandboxStatePatch { records }
20        }
21
22        pub fn is_empty(&self) -> bool {
23            self.records.is_empty()
24        }
25
26        pub fn clear(&mut self) {
27            self.records.clear();
28        }
29
30        pub fn take(&mut self) -> SandboxStatePatch {
31            Self { records: core::mem::take(&mut self.records) }
32        }
33
34        pub fn merge(&mut self, other: SandboxStatePatch) {
35            self.records.extend(other.records);
36        }
37    }
38
39    impl IntoIterator for SandboxStatePatch {
40        type Item = StateRecord;
41        type IntoIter = std::vec::IntoIter<StateRecord>;
42
43        fn into_iter(self) -> Self::IntoIter {
44            self.records.into_iter()
45        }
46    }
47}
48
49#[cfg(not(feature = "sandbox"))]
50pub mod state_patch {
51    use crate::state_record::StateRecord;
52
53    #[derive(Default)]
54    pub struct SandboxStatePatch;
55
56    impl SandboxStatePatch {
57        #[inline(always)]
58        pub fn is_empty(&self) -> bool {
59            true
60        }
61        #[inline(always)]
62        pub fn clear(&self) {}
63        #[inline(always)]
64        pub fn take(&mut self) -> Self {
65            Self
66        }
67        #[inline(always)]
68        pub fn merge(&self, _other: SandboxStatePatch) {}
69    }
70
71    impl IntoIterator for SandboxStatePatch {
72        type Item = StateRecord;
73        type IntoIter = std::iter::Empty<StateRecord>;
74
75        #[inline(always)]
76        fn into_iter(self) -> Self::IntoIter {
77            std::iter::empty()
78        }
79    }
80}