Skip to main content

ic_storage/
memory.rs

1//! Upstream memory contract and Platform-backed canister storage.
2pub use ic_stable_structures::{Memory, VectorMemory};
3
4/// Native default backing for fixture and host integration.
5#[cfg(not(target_arch = "wasm32"))]
6pub type DefaultMemoryImpl = VectorMemory;
7
8/// Canister stable memory through Platform's system-call boundary.
9#[cfg(target_arch = "wasm32")]
10#[derive(Clone, Copy, Default)]
11pub struct DefaultMemoryImpl {
12    _private: (),
13}
14#[cfg(target_arch = "wasm32")]
15impl Memory for DefaultMemoryImpl {
16    fn size(&self) -> u64 {
17        ic_platform_runtime::raw::stable64_size()
18    }
19    fn grow(&self, pages: u64) -> i64 {
20        ic_platform_runtime::raw::stable64_grow(pages).map_or(-1, |previous| {
21            i64::try_from(previous).expect("IC page count")
22        })
23    }
24    fn read(&self, offset: u64, bytes: &mut [u8]) {
25        ic_platform_runtime::raw::stable64_read(offset, bytes);
26    }
27    fn write(&self, offset: u64, bytes: &[u8]) {
28        ic_platform_runtime::raw::stable64_write(offset, bytes);
29    }
30}