zink/storage/
mod.rs

1//! Zink storage implementation.
2
3use crate::{ffi, 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/// Interface for the value of kv based storage
15pub trait StorageValue: Asm {
16    /// Load from storage
17    fn sload() -> Self;
18}
19
20/// Interface for the value of kv based transient storage
21pub trait TransientStorageValue: Asm {
22    /// Load from transient storage
23    fn tload() -> Self;
24}
25
26impl StorageValue for i32 {
27    fn sload() -> Self {
28        #[cfg(target_arch = "wasm32")]
29        unsafe {
30            ffi::asm::sload_i32()
31        }
32        #[cfg(not(target_arch = "wasm32"))]
33        ffi::asm::sload_i32()
34    }
35}
36
37impl StorageValue for u32 {
38    fn sload() -> Self {
39        #[cfg(target_arch = "wasm32")]
40        unsafe {
41            ffi::asm::sload_u32()
42        }
43        #[cfg(not(target_arch = "wasm32"))]
44        ffi::asm::sload_u32()
45    }
46}
47
48impl TransientStorageValue for i32 {
49    fn tload() -> Self {
50        #[cfg(target_arch = "wasm32")]
51        unsafe {
52            ffi::asm::tload_i32()
53        }
54        #[cfg(not(target_arch = "wasm32"))]
55        ffi::asm::tload_i32()
56    }
57}
58
59impl TransientStorageValue for u32 {
60    fn tload() -> Self {
61        #[cfg(target_arch = "wasm32")]
62        unsafe {
63            ffi::asm::tload_u32()
64        }
65        #[cfg(not(target_arch = "wasm32"))]
66        ffi::asm::tload_u32()
67    }
68}