1use crate::{declaration::AllocationDeclaration, key::StableKey, slot::AllocationSlotDescriptor};
2use std::sync::Arc;
3
4#[derive(Clone, Debug, Eq, PartialEq)]
19pub struct ValidatedAllocations {
20 inner: Arc<ValidatedState>,
21 _private: (),
22}
23
24#[derive(Clone, Debug, Eq, PartialEq)]
25struct ValidatedState {
26 base_generation: u64,
28 declarations: Vec<AllocationDeclaration>,
30 runtime_fingerprint: Option<String>,
32}
33
34impl ValidatedAllocations {
35 pub(crate) fn new(
36 base_generation: u64,
37 declarations: Vec<AllocationDeclaration>,
38 runtime_fingerprint: Option<String>,
39 ) -> Self {
40 Self {
41 inner: Arc::new(ValidatedState {
42 base_generation,
43 declarations,
44 runtime_fingerprint,
45 }),
46 _private: (),
47 }
48 }
49
50 #[must_use]
52 pub fn base_generation(&self) -> u64 {
53 self.inner.base_generation
54 }
55
56 #[must_use]
58 pub fn declarations(&self) -> &[AllocationDeclaration] {
59 &self.inner.declarations
60 }
61
62 #[must_use]
64 pub fn runtime_fingerprint(&self) -> Option<&str> {
65 self.inner.runtime_fingerprint.as_deref()
66 }
67
68 #[must_use]
70 pub fn slot_for(&self, key: &StableKey) -> Option<&AllocationSlotDescriptor> {
71 self.declarations()
72 .iter()
73 .find(|declaration| &declaration.stable_key == key)
74 .map(|declaration| &declaration.slot)
75 }
76
77 pub(crate) const fn confirm_persisted(self, generation: u64) -> CommittedAllocations {
78 CommittedAllocations {
79 validated: self,
80 generation,
81 _private: (),
82 }
83 }
84}
85
86#[derive(Clone, Debug, Eq, PartialEq)]
99pub struct CommittedAllocations {
100 validated: ValidatedAllocations,
101 generation: u64,
102 _private: (),
103}
104
105impl CommittedAllocations {
106 #[must_use]
108 pub const fn generation(&self) -> u64 {
109 self.generation
110 }
111
112 #[must_use]
114 pub fn declarations(&self) -> &[AllocationDeclaration] {
115 self.validated.declarations()
116 }
117
118 #[must_use]
120 pub fn runtime_fingerprint(&self) -> Option<&str> {
121 self.validated.runtime_fingerprint()
122 }
123
124 #[must_use]
126 pub fn slot_for(&self, key: &StableKey) -> Option<&AllocationSlotDescriptor> {
127 self.validated.slot_for(key)
128 }
129
130 pub(crate) fn without_stable_key_prefix(mut self, prefix: &str) -> Self {
131 let mut state = (*self.validated.inner).clone();
132 state
133 .declarations
134 .retain(|declaration| !declaration.stable_key.as_str().starts_with(prefix));
135 self.validated.inner = Arc::new(state);
136 self
137 }
138}