use crate::{
GenericAllocationPolicy, Memory, MemoryAllocationPool, MemoryAuthority, MemoryManagerConfig,
MemoryRequest, MemoryRuntime, SealedDeclarationSnapshot,
};
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum FixtureRuntimeError {
#[error(transparent)]
Declaration(#[from] crate::MemoryDeclarationError),
#[error(transparent)]
AllocationPool(#[from] crate::MemoryAllocationPoolError),
#[error(transparent)]
Construction(#[from] crate::RuntimeConstructionError),
#[error(transparent)]
Bootstrap(Box<crate::RuntimeBootstrapError<std::convert::Infallible>>),
}
pub fn fixture_runtime<M: Memory>(
backing: M,
owner: &str,
namespace: &str,
stable_keys: &[&str],
config: MemoryManagerConfig,
) -> Result<MemoryRuntime<M>, FixtureRuntimeError> {
let requests = stable_keys
.iter()
.map(|key| MemoryRequest::new(owner, key))
.collect::<Result<Vec<_>, _>>()?;
let pool = MemoryAllocationPool::new(vec![MemoryAuthority::new(owner, namespace)?], vec![])?;
let declarations = SealedDeclarationSnapshot::new(requests)?;
let mut runtime = MemoryRuntime::new_with_config(backing, config)?;
runtime
.bootstrap(&declarations, &pool, &GenericAllocationPolicy)
.map_err(|error| FixtureRuntimeError::Bootstrap(Box::new(error)))?;
Ok(runtime)
}