Skip to main content

ic_memory/slot/
descriptor.rs

1use super::memory_manager::MemoryManagerSlotError;
2use serde::{Deserialize, Serialize};
3
4///
5/// AllocationSlot
6///
7/// Durable physical `ic-stable-structures::MemoryManager` allocation identity.
8///
9/// Stable keys are logical identities; allocation slots are the physical
10/// locations those keys are bound to in the ledger.
11#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
12#[serde(deny_unknown_fields)]
13pub enum AllocationSlot {
14    /// `ic-stable-structures::MemoryManager` virtual memory ID.
15    MemoryManagerId(u8),
16}
17
18///
19/// AllocationSlotDescriptor
20///
21/// Encoded allocation slot persisted in the ledger.
22///
23/// Use [`AllocationSlotDescriptor::memory_manager`] so ID 255 is rejected.
24///
25
26#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
27#[serde(deny_unknown_fields)]
28pub struct AllocationSlotDescriptor {
29    /// Durable allocation slot.
30    pub(crate) slot: AllocationSlot,
31}
32
33impl AllocationSlotDescriptor {
34    /// Return the durable allocation slot value.
35    #[must_use]
36    pub const fn slot(&self) -> &AllocationSlot {
37        &self.slot
38    }
39
40    /// Validate constructor invariants after decode.
41    pub fn validate(&self) -> Result<(), MemoryManagerSlotError> {
42        self.memory_manager_id().map(|_| ())
43    }
44}