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)]
100pub struct CommittedAllocations {
101 validated: ValidatedAllocations,
102 generation: u64,
103 _private: (),
104}
105
106impl CommittedAllocations {
107 #[must_use]
109 pub const fn generation(&self) -> u64 {
110 self.generation
111 }
112
113 #[must_use]
115 pub fn declarations(&self) -> &[AllocationDeclaration] {
116 self.validated.declarations()
117 }
118
119 #[must_use]
121 pub fn runtime_fingerprint(&self) -> Option<&str> {
122 self.validated.runtime_fingerprint()
123 }
124
125 #[must_use]
127 pub fn slot_for(&self, key: &StableKey) -> Option<&AllocationSlotDescriptor> {
128 self.validated.slot_for(key)
129 }
130
131 pub(crate) fn without_stable_key_prefix(mut self, prefix: &str) -> Self {
132 let mut state = (*self.validated.inner).clone();
133 state
134 .declarations
135 .retain(|declaration| !declaration.stable_key.as_str().starts_with(prefix));
136 self.validated.inner = Arc::new(state);
137 self
138 }
139}