ic_memory/slot/
memory_manager.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
17#[serde(try_from = "u8", into = "u8")]
18pub struct MemoryManagerSlot(u8);
19
20impl MemoryManagerSlot {
21 pub const fn new(id: u8) -> Result<Self, MemoryManagerSlotError> {
23 match validate_memory_manager_id(id) {
24 Ok(()) => Ok(Self(id)),
25 Err(error) => Err(error),
26 }
27 }
28
29 #[must_use]
31 pub const fn id(&self) -> u8 {
32 self.0
33 }
34}
35
36impl From<MemoryManagerSlot> for u8 {
37 fn from(slot: MemoryManagerSlot) -> Self {
38 slot.id()
39 }
40}
41
42impl TryFrom<u8> for MemoryManagerSlot {
43 type Error = MemoryManagerSlotError;
44
45 fn try_from(id: u8) -> Result<Self, Self::Error> {
46 Self::new(id)
47 }
48}
49
50pub const MEMORY_MANAGER_MIN_ID: u8 = 0;
52
53pub const MEMORY_MANAGER_MAX_ID: u8 = 254;
55
56pub const MEMORY_MANAGER_INVALID_ID: u8 = u8::MAX;
58
59pub const IC_MEMORY_STABLE_KEY_PREFIX: &str = "ic_memory.";
61
62pub const IC_MEMORY_AUTHORITY_OWNER: &str = "ic-memory";
64
65pub const IC_MEMORY_LEDGER_STABLE_KEY: &str = "ic_memory.ledger.v1";
67
68pub const MEMORY_MANAGER_LEDGER_ID: u8 = MEMORY_MANAGER_MIN_ID;
70
71pub const MEMORY_MANAGER_GOVERNANCE_MAX_ID: u8 = 9;
73
74#[must_use]
76pub fn is_ic_memory_stable_key(stable_key: &str) -> bool {
77 stable_key.starts_with(IC_MEMORY_STABLE_KEY_PREFIX)
78}
79
80#[non_exhaustive]
87#[derive(Clone, Debug, Eq, thiserror::Error, PartialEq)]
88pub enum MemoryManagerSlotError {
89 #[error("MemoryManager ID {id} is not a usable allocation slot")]
91 InvalidMemoryManagerId {
92 id: u8,
94 },
95}
96
97pub const fn validate_memory_manager_id(id: u8) -> Result<(), MemoryManagerSlotError> {
99 if id == MEMORY_MANAGER_INVALID_ID {
100 return Err(MemoryManagerSlotError::InvalidMemoryManagerId { id });
101 }
102 Ok(())
103}