mod.rs

benches/minima/src/root/mod.rs

mod.rs118 lines← Benchmarks
1//! Only attachments, provider setup and transient service wiring live here.2#[cdk_rsx::root(state = App, init = install, restore = reopen)]3pub mod memory;4use crate::{5    sketches::{self, Sketches},6    tables::{self, Tables},7};8use cdk_rsx_engine_minima::Arena;9use cdk_rsx_store::{Actor, Error, Result, Store};10use memory::current as Mem;11use std::{cell::RefCell, rc::Rc};1213#[cfg(target_family = "wasm")]14pub(crate) type Physical = cdk_rsx_platform::StableMemory;15#[cfg(not(target_family = "wasm"))]16pub(crate) type Physical = cdk_rsx_platform::VecMemory;17pub(crate) type Provider = Rc<RefCell<Arena<Physical>>>;18const HYLIX_CONTRACT: [u8; 32] = [0x42; 32];19#[cdk_rsx::service(actor = actor)]20pub(crate) struct App {21    actor: Actor<Mem::Memory>,22    #[service]23    tables: Tables,24    #[service]25    sketches: Sketches,26}27impl App {28    fn install(store: Store, arena: Arena<Physical>) -> Result<Self> {29        let store = store.with_hylix(30            cdk_rsx_engine_hylix::Arena::format(arena.hylix_memory(), HYLIX_CONTRACT)31                .map_err(|_| Error::Hylix)?,32        )?;33        let provider = Rc::new(RefCell::new(arena));34        let actor = store.install(&Mem::KEY, |scope| {35            let root = scope.create(&Mem::KEY, ())?;36            let child = tables::memory::new(scope, &mut *provider.borrow_mut())?;37            root.reference()38                .children(scope, &Mem::KEY)?39                .tables40                .attach(scope, child)?;41            let sketches = sketches::memory::new(scope, &mut *provider.borrow_mut())?;42            root.reference()43                .children(scope, &Mem::KEY)?44                .sketches45                .attach(scope, sketches)?;46            Ok(root)47        })?;48        Self::bind(actor, provider)49    }50    fn bind(actor: Actor<Mem::Memory>, provider: Provider) -> Result<Self> {51        let tables = actor.read(|scope, root| {52            let reference = root53                .children(scope, &Mem::KEY)?54                .tables55                .reference(scope)?56                .ok_or(Error::NotInstalled)?;57            Tables::open(scope, reference, provider.clone())58        })?;59        let sketches = actor.read(|scope, root| {60            let reference = root61                .children(scope, &Mem::KEY)?62                .sketches63                .reference(scope)?64                .ok_or(Error::NotInstalled)?;65            Ok(Sketches::bind(reference, provider.clone()))66        })?;67        Ok(Self::new(actor, tables, sketches))68    }69    #[cfg(target_family = "wasm")]70    fn open(store: Store) -> Result<Self> {71        let preliminary = store.open(&Mem::KEY)?;72        let descriptor = preliminary.read(|scope, root| {73            let reference = root74                .children(scope, &Mem::KEY)?75                .tables76                .reference(scope)?77                .ok_or(Error::NotInstalled)?;78            tables::descriptor(scope, reference)79        })?;80        // SAFETY: this root acquires the sole physical stable capability.81        let memory = unsafe { Physical::acquire() };82        let arena = Arena::open(memory, descriptor).map_err(|_| Error::Arena)?;83        let store = store.with_hylix(84            cdk_rsx_engine_hylix::Arena::open(arena.hylix_memory(), HYLIX_CONTRACT)85                .map_err(|_| Error::Hylix)?,86        )?;87        Self::bind(store.open(&Mem::KEY)?, Rc::new(RefCell::new(arena)))88    }89}90fn install((): ()) -> Result<()> {91    #[cfg(target_family = "wasm")]92    let store = Store::initialize()?;93    #[cfg(not(target_family = "wasm"))]94    let store = Store::default();95    #[cfg(target_family = "wasm")]96    let memory = unsafe { Physical::acquire() };97    #[cfg(not(target_family = "wasm"))]98    let memory = Physical::new();99    let arena = Arena::format(memory).map_err(|_| Error::Arena)?;100    save(App::install(store, arena)?);101    Ok(())102}103fn reopen() -> Result<()> {104    #[cfg(target_family = "wasm")]105    {106        let store = unsafe { Store::reopen() }?;107        save(App::open(store)?);108        Ok(())109    }110    #[cfg(not(target_family = "wasm"))]111    {112        Err(Error::NotInstalled)113    }114}115116#[cfg(test)]117#[path = "operations_tests.rs"]118mod operations_tests;