async-nodes 0.1.0

An asynchronous, type-safe node engine implementing the Bulk Synchronous Parallel (BSP) model: control-flow loops, recovery, suspension, and serializable graphs, with dataflow-once pipelines as a profile.
Documentation
use std::sync::Arc;

use parking_lot::{MappedRwLockReadGuard, RwLock, RwLockReadGuard};
use typemap_rev::{TypeMap, TypeMapKey};

/// Shared execution context supporting dependency injection and state mapping across AST graphs.
#[derive(Clone, Default)]
pub struct BspContext {
    inner: Arc<RwLock<TypeMap>>,
}

impl BspContext {
    /// Instantiates a new empty execution context.
    #[must_use]
    pub fn new() -> Self {
        Self {
            inner: Arc::new(RwLock::new(TypeMap::new())),
        }
    }

    /// Injects a shared thread-safe dependency into the context store.
    pub fn insert<K: TypeMapKey>(&self, val: K::Value) {
        self.inner.write().insert::<K>(val);
    }

    /// Dynamically retrieves a reference-like read-guard to an injected dependency.
    #[must_use]
    pub fn get<K: TypeMapKey>(&self) -> Option<MappedRwLockReadGuard<'_, K::Value>> {
        let guard = self.inner.read();
        RwLockReadGuard::try_map(guard, typemap_rev::TypeMap::get::<K>).ok()
    }
}