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}
30
31impl AllocationSlotDescriptor {
32    /// Return the durable allocation slot value.
33    #[must_use]
34    pub const fn slot(&self) -> &AllocationSlot {
35        &self.slot
36    }
37}
38
39impl Validate for AllocationSlotDescriptor {
40    type Error = super::memory_manager::MemoryManagerSlotError;
41
42    fn validate(&self) -> Result<(), Self::Error> {
43        self.memory_manager_id().map(|_| ())
44    }
45}