Skip to main content

ic_memory/slot/
descriptor.rs

1use crate::validation::Validate;
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#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
25#[serde(deny_unknown_fields)]
26pub struct AllocationSlotDescriptor {
27    /// Durable allocation slot.
28    pub(crate) slot: AllocationSlot,
29    /// Fixed substrate marker for the current `MemoryManager` slot protocol.
30    pub(crate) substrate: String,
31    /// Descriptor encoding version.
32    pub(crate) descriptor_version: u32,
33}
34
35impl AllocationSlotDescriptor {
36    /// Return the durable allocation slot value.
37    #[must_use]
38    pub const fn slot(&self) -> &AllocationSlot {
39        &self.slot
40    }
41
42    /// Return the fixed substrate marker for this slot protocol.
43    #[must_use]
44    pub fn substrate(&self) -> &str {
45        &self.substrate
46    }
47
48    /// Return the descriptor encoding version.
49    #[must_use]
50    pub const fn descriptor_version(&self) -> u32 {
51        self.descriptor_version
52    }
53}
54
55impl Validate for AllocationSlotDescriptor {
56    type Error = AllocationSlotDescriptorError;
57
58    fn validate(&self) -> Result<(), Self::Error> {
59        self.memory_manager_id()
60            .map(|_| ())
61            .map_err(AllocationSlotDescriptorError::MemoryManager)
62    }
63}
64
65///
66/// AllocationSlotDescriptorError
67///
68/// Allocation slot descriptor validation failure.
69#[derive(Clone, Debug, Eq, thiserror::Error, PartialEq)]
70pub enum AllocationSlotDescriptorError {
71    /// `MemoryManager` descriptor invariants failed.
72    #[error(transparent)]
73    MemoryManager(super::memory_manager::MemoryManagerSlotError),
74}