use crate::{
GenericAllocationPolicy, MemoryAllocationPool, MemoryAuthority, MemoryManagerConfig,
MemoryRequest, MemoryRuntime, RuntimeMemory, SchemaMetadata, SealedDeclarationSnapshot,
ic_stable_structures::{Cell, Memory},
};
pub struct FixtureCell<M: Memory> {
_runtime: MemoryRuntime<M>,
cell: Cell<Vec<u8>, RuntimeMemory<M>>,
}
impl<M: Memory> FixtureCell<M> {
pub fn new(
memory: M,
owner: &str,
namespace: &str,
stable_key: &str,
config: MemoryManagerConfig,
) -> Self {
let request = MemoryRequest::new(owner, stable_key, SchemaMetadata::default())
.expect("fixture memory request");
let pool = MemoryAllocationPool::new(
vec![MemoryAuthority::new(owner, namespace).expect("fixture namespace grant")],
vec![],
)
.expect("fixture allocation pool");
let declarations =
SealedDeclarationSnapshot::new(vec![request]).expect("fixture declarations");
let mut runtime = MemoryRuntime::new_with_config(memory, config).expect("fixture runtime");
runtime
.bootstrap(&declarations, &pool, &GenericAllocationPolicy)
.expect("fixture bootstrap");
let memory = runtime
.open_memory(stable_key)
.expect("fixture memory grant");
let cell = Cell::init(memory, Vec::new());
Self {
_runtime: runtime,
cell,
}
}
#[must_use]
pub fn bytes(&self) -> &[u8] {
self.cell.get()
}
pub fn set(&mut self, bytes: Vec<u8>) {
self.cell.set(bytes);
}
}
#[cfg(test)]
mod tests;