truthlinked_sdk/
storage.rs1use crate::codec::Codec32;
17use crate::env;
18use crate::error::Result;
19use crate::hashing;
20
21#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
23pub struct Slot(pub [u8; 32]);
24
25impl Slot {
26 pub const fn new(slot: [u8; 32]) -> Self {
28 Self(slot)
29 }
30
31 pub fn from_label(label: &str) -> Self {
33 Self(hashing::derive_slot(b"trth:sdk:slot", &[label.as_bytes()]))
34 }
35
36 pub fn derived(namespace: &[u8; 32], parts: &[&[u8]]) -> Self {
38 Self(hashing::derive_slot(namespace, parts))
39 }
40
41 pub fn read(self) -> Result<[u8; 32]> {
43 env::storage_read_32(&self.0)
44 }
45
46 pub fn write(self, value: &[u8; 32]) -> Result<()> {
48 env::storage_write_32(&self.0, value)
49 }
50
51 pub fn write_typed<T: Codec32>(self, value: &T) -> Result<()> {
53 let encoded = value.encode_32();
54 self.write(&encoded)
55 }
56
57 pub fn read_typed<T: Codec32>(self) -> Result<T> {
59 let raw = self.read()?;
60 T::decode_32(&raw)
61 }
62
63 pub fn write_u64(self, value: u64) -> Result<()> {
65 let mut bytes = [0u8; 32];
66 bytes[..8].copy_from_slice(&value.to_le_bytes());
67 self.write(&bytes)
68 }
69
70 pub fn read_u64(self) -> Result<u64> {
72 let bytes = self.read()?;
73 let mut out = [0u8; 8];
74 out.copy_from_slice(&bytes[..8]);
75 Ok(u64::from_le_bytes(out))
76 }
77
78 pub fn write_u128(self, value: u128) -> Result<()> {
80 let mut bytes = [0u8; 32];
81 bytes[..16].copy_from_slice(&value.to_le_bytes());
82 self.write(&bytes)
83 }
84
85 pub fn read_u128(self) -> Result<u128> {
87 let bytes = self.read()?;
88 let mut out = [0u8; 16];
89 out.copy_from_slice(&bytes[..16]);
90 Ok(u128::from_le_bytes(out))
91 }
92}
93
94pub fn namespace(label: &str) -> [u8; 32] {
96 hashing::hash32(label.as_bytes())
97}
98
99pub fn slot_for(namespace: &[u8; 32], key: &[u8]) -> Slot {
101 Slot::derived(namespace, &[key])
102}
103
104pub fn slot_for_parts(namespace: &[u8; 32], parts: &[&[u8]]) -> Slot {
106 Slot::derived(namespace, parts)
107}
108
109#[macro_export]
111macro_rules! slot {
112 ($bytes:expr) => {{
113 $crate::storage::Slot::new($bytes)
114 }};
115}