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/// Identity of a physical backing allocation, shared by all of its wrappers.
17/// This is host custody metadata, not a virtual memory ID or durable record.
18#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
19pub struct BackingIdentity(usize);
20
21impl BackingIdentity {
22 /// Identify an Rc-owned backing. Its allocation must remain alive through
23 /// every runtime/handle using this identity. Identify the underlying backing,
24 /// not a newly allocated wrapper or an address inside a growable byte buffer.
25 #[must_use]
26 pub fn of_rc<T: ?Sized>(backing: &Rc<T>) -> Self {
27 Self(Rc::as_ptr(backing).cast::<()>() as usize)
28 }
29
30 /// Identify an Arc-owned backing under the same contract as [`Self::of_rc`].
31 #[must_use]
32 pub fn of_arc<T: ?Sized>(backing: &Arc<T>) -> Self {
33 Self(Arc::as_ptr(backing).cast::<()>() as usize)
34 }
35}