Skip to main content

ic_memory/slot/
descriptor.rs

1use serde::{Deserialize, Serialize};
2
3///
4/// AllocationSlot
5///
6/// Durable physical allocation identity interpreted by a storage substrate.
7///
8/// Stable keys are logical identities; allocation slots are the physical
9/// locations those keys are bound to in the ledger.
10#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
11pub enum AllocationSlot {
12    /// `ic-stable-structures::MemoryManager` virtual memory ID.
13    MemoryManagerId(u8),
14    /// Named substrate partition.
15    NamedPartition(String),
16    /// Forward-compatible custom slot descriptor.
17    Custom {
18        /// Substrate-defined slot kind.
19        kind: String,
20        /// Slot descriptor version.
21        version: u32,
22        /// Canonical substrate-defined value.
23        value: Vec<u8>,
24    },
25}
26
27///
28/// AllocationSlotDescriptor
29///
30/// Encoded allocation slot persisted in the ledger.
31///
32/// Constructors for built-in substrates validate their invariants before a
33/// descriptor can be used publicly. For `MemoryManager` slots, use
34/// [`AllocationSlotDescriptor::memory_manager`] so ID 255 is rejected.
35#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
36pub struct AllocationSlotDescriptor {
37    /// Durable allocation slot.
38    pub(crate) slot: AllocationSlot,
39    /// Substrate identifier that interprets the slot.
40    pub(crate) substrate: String,
41    /// Descriptor encoding version.
42    pub(crate) descriptor_version: u32,
43}
44
45impl AllocationSlotDescriptor {
46    /// Return the durable allocation slot value.
47    #[must_use]
48    pub const fn slot(&self) -> &AllocationSlot {
49        &self.slot
50    }
51
52    /// Return the substrate identifier that interprets this slot.
53    #[must_use]
54    pub fn substrate(&self) -> &str {
55        &self.substrate
56    }
57
58    /// Return the descriptor encoding version.
59    #[must_use]
60    pub const fn descriptor_version(&self) -> u32 {
61        self.descriptor_version
62    }
63}