ulo 0.1.0-alpha.1

Fast and modular web framework for scalable applications
Documentation
use std::collections::HashMap;
use std::future::Future;
use std::sync::{Arc, Mutex, PoisonError};

use async_lock::OnceCell;

use crate::binding::Instance;
use crate::error::LookupError;
use crate::graph::BindingId;
use crate::transport::enhancer::ClosureId;

/// What one cell holds the instance of: a binding, or an enhancer declared by closure.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) enum Slot {
    Binding(BindingId),
    Closure(ClosureId),
}

/// One instance per slot, built on first read and shared after. Two readers of one slot
/// concurrently get one instance: both await one cell. A failed build is not cached.
///
/// An execution's cache holds its execution-scoped bindings and its per-execution enhancer
/// closures; the app's holds the enhancer closures built once. A cell holds the instance as the
/// recipe built it; `AppShared::obtain` widens a contribution to its collection's type on the way
/// out, as it does for a stored singleton.
#[derive(Default)]
pub(crate) struct OnceCells {
    cells: Mutex<HashMap<Slot, Arc<OnceCell<Instance>>>>,
}

impl OnceCells {
    pub(crate) async fn get_or_build<F, Fut>(&self, slot: Slot, build: F) -> Result<Instance, LookupError>
    where
        F: FnOnce() -> Fut,
        Fut: Future<Output = Result<Instance, LookupError>>,
    {
        // The map lock is released before the build is awaited: the build reads other dependencies,
        // which come back here for their own cells.
        let cell = {
            let mut cells = self.cells.lock().unwrap_or_else(PoisonError::into_inner);
            Arc::clone(cells.entry(slot).or_default())
        };
        let instance = cell.get_or_try_init(build).await?;
        Ok(Arc::clone(instance))
    }
}