zink/storage/
mod.rs

1//! Zink storage implementation.
2
3use crate::asm;
4pub use {
5    dkmapping::{DoubleKeyMapping, DoubleKeyTransientMapping},
6    mapping::{Mapping, TransientMapping},
7    value::{Storage, TransientStorage},
8};
9
10pub mod dkmapping;
11pub mod mapping;
12mod value;
13
14/// Trait for the value used in assembly code
15pub trait Value {
16    /// Load from storage
17    fn sload() -> Self;
18
19    /// Load from transient storage
20    fn tload() -> Self;
21
22    /// Push self on the stack.
23    fn push(self);
24
25    /// Convert to bytes32
26    #[cfg(not(target_family = "wasm"))]
27    fn bytes32(&self) -> [u8; 32];
28}
29
30macro_rules! impl_value {
31    ($($ty:ident),+) => {
32        $(
33            paste::paste! {
34                impl Value for $ty {
35                    fn sload() -> Self {
36                        unsafe { asm::ext::[<sload_ $ty>]() }
37                    }
38
39                    fn tload() -> Self {
40                        unsafe { asm::ext::[<tload_ $ty>]() }
41                    }
42
43                    fn push(self) {
44                        unsafe { asm::ext::[<push_ $ty>](self); }
45                    }
46
47                    #[cfg(not(target_family = "wasm"))]
48                    fn bytes32(&self) -> [u8; 32] {
49                            crate::to_bytes32(&self.to_le_bytes())
50                    }
51               }
52            }
53        )+
54    };
55}
56
57impl_value!(i8, u8, i16, u16, i32, u32, i64, u64);