1use crate::{
2 declaration::AllocationDeclaration, key::StableKey, slot::AllocationSlotDescriptor,
3 substrate::StorageSubstrate,
4};
5use serde::{Deserialize, Serialize};
6
7#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
17pub struct ValidatedAllocations {
18 generation: u64,
20 declarations: Vec<AllocationDeclaration>,
22 runtime_fingerprint: Option<String>,
24}
25
26impl ValidatedAllocations {
27 pub(crate) const fn new(
28 generation: u64,
29 declarations: Vec<AllocationDeclaration>,
30 runtime_fingerprint: Option<String>,
31 ) -> Self {
32 Self {
33 generation,
34 declarations,
35 runtime_fingerprint,
36 }
37 }
38
39 pub(crate) const fn with_generation(mut self, generation: u64) -> Self {
40 self.generation = generation;
41 self
42 }
43
44 #[must_use]
46 pub const fn generation(&self) -> u64 {
47 self.generation
48 }
49
50 #[must_use]
52 pub fn declarations(&self) -> &[AllocationDeclaration] {
53 &self.declarations
54 }
55
56 #[must_use]
58 pub fn runtime_fingerprint(&self) -> Option<&str> {
59 self.runtime_fingerprint.as_deref()
60 }
61
62 #[must_use]
64 pub fn slot_for(&self, key: &StableKey) -> Option<&AllocationSlotDescriptor> {
65 self.declarations
66 .iter()
67 .find(|declaration| &declaration.stable_key == key)
68 .map(|declaration| &declaration.slot)
69 }
70}
71
72pub struct AllocationSession<S: StorageSubstrate> {
82 substrate: S,
83 validated: ValidatedAllocations,
84}
85
86impl<S: StorageSubstrate> AllocationSession<S> {
87 #[must_use]
89 pub const fn new(substrate: S, validated: ValidatedAllocations) -> Self {
90 Self {
91 substrate,
92 validated,
93 }
94 }
95
96 #[must_use]
98 pub const fn validated(&self) -> &ValidatedAllocations {
99 &self.validated
100 }
101
102 pub fn open(
104 &self,
105 key: &StableKey,
106 ) -> Result<S::MemoryHandle, AllocationSessionError<S::Error>> {
107 let slot = self
108 .validated
109 .slot_for(key)
110 .ok_or_else(|| AllocationSessionError::UnknownStableKey(key.clone()))?;
111 self.substrate
112 .open_slot(slot)
113 .map_err(AllocationSessionError::Substrate)
114 }
115}
116
117#[derive(Clone, Debug, Eq, thiserror::Error, PartialEq)]
122pub enum AllocationSessionError<E> {
123 #[error("stable key '{0}' was not validated for this allocation session")]
125 UnknownStableKey(StableKey),
126 #[error("storage substrate failed to open allocation slot")]
128 Substrate(E),
129}