Skip to main content

ic_storage/memory/
mod.rs

1//! Upstream memory contract and Platform-backed canister storage.
2cfg_select! {
3    target_arch = "wasm32" => {
4        mod wasm;
5        pub use wasm::DefaultMemoryImpl;
6    }
7    _ => {
8        /// Native default backing for fixture and host integration.
9        pub type DefaultMemoryImpl = VectorMemory;
10    }
11}
12
13pub use crate::substrate::{Memory, VectorMemory};
14use std::{rc::Rc, sync::Arc};
15
16///
17/// BackingIdentity
18/// Identity of a physical backing allocation, shared by all of its wrappers.
19/// This is host custody metadata, not a virtual memory ID or durable record.
20///
21
22#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
23pub struct BackingIdentity(usize);
24
25impl BackingIdentity {
26    /// Identify an Rc-owned backing. Its allocation must remain alive through
27    /// every runtime/handle using this identity. Identify the underlying backing,
28    /// not a newly allocated wrapper or an address inside a growable byte buffer.
29    #[must_use]
30    pub fn of_rc<T: ?Sized>(backing: &Rc<T>) -> Self {
31        Self(Rc::as_ptr(backing).cast::<()>() as usize)
32    }
33
34    /// Identify an Arc-owned backing under the same contract as [`Self::of_rc`].
35    #[must_use]
36    pub fn of_arc<T: ?Sized>(backing: &Arc<T>) -> Self {
37        Self(Arc::as_ptr(backing).cast::<()>() as usize)
38    }
39}