cranpose-core 0.0.60

Core runtime for a Jetpack Compose inspired UI framework in Rust
Documentation
use super::debug::SlotTableDiagnostics;
use super::{
    AnchorRegistry, DeferredDrop, GroupRecord, NodeRecord, PayloadAnchorRegistry, PayloadRecord,
    ScopeIndex, SlotLifecycleCoordinator, SlotWriteSessionState,
};
use std::sync::atomic::{AtomicUsize, Ordering};

mod metadata;
mod mutation;
mod values;

static NEXT_SLOT_STORAGE_ID: AtomicUsize = AtomicUsize::new(1);

pub(crate) struct SlotWriteSession<'a> {
    pub(super) table: &'a mut SlotTable,
    pub(in crate::slot) lifecycle: &'a mut SlotLifecycleCoordinator,
    pub(in crate::slot) state: &'a mut SlotWriteSessionState,
}

pub struct SlotTable {
    storage_id: usize,
    pub(super) groups: Vec<GroupRecord>,
    pub(super) payloads: Vec<PayloadRecord>,
    pub(super) nodes: Vec<NodeRecord>,
    pub(super) anchors: AnchorRegistry,
    pub(super) payload_anchors: PayloadAnchorRegistry,
    pub(super) scope_index: ScopeIndex,
    pub(super) diagnostics: SlotTableDiagnostics,
    next_group_generation: u32,
}

impl SlotTable {
    pub fn new() -> Self {
        Self {
            storage_id: NEXT_SLOT_STORAGE_ID.fetch_add(1, Ordering::Relaxed),
            groups: Vec::new(),
            payloads: Vec::new(),
            nodes: Vec::new(),
            anchors: AnchorRegistry::new(),
            payload_anchors: PayloadAnchorRegistry::new(),
            scope_index: ScopeIndex::new(),
            diagnostics: SlotTableDiagnostics::default(),
            next_group_generation: 1,
        }
    }

    pub(crate) fn write_session<'a>(
        &'a mut self,
        lifecycle: &'a mut SlotLifecycleCoordinator,
        state: &'a mut SlotWriteSessionState,
    ) -> SlotWriteSession<'a> {
        SlotWriteSession {
            table: self,
            lifecycle,
            state,
        }
    }

    pub(crate) fn storage_id(&self) -> usize {
        self.storage_id
    }

    pub(crate) fn compact_storage(&mut self) {
        self.groups.shrink_to_fit();
        self.payloads.shrink_to_fit();
        self.nodes.shrink_to_fit();
        self.anchors.shrink_to_fit();
        self.payload_anchors.shrink_to_fit();
        self.scope_index.shrink_to_fit();
    }

    pub(crate) fn take_all_drops(&mut self) -> Vec<DeferredDrop> {
        let payload_count = self.payloads.len();
        let mut drops = Vec::with_capacity(payload_count);
        for payload in self.payloads.drain(..).rev() {
            drops.push(payload.into_deferred_drop());
        }
        self.groups.clear();
        self.nodes.clear();
        self.anchors.clear();
        self.payload_anchors.clear();
        self.scope_index.clear();
        drops
    }
}

impl Default for SlotTable {
    fn default() -> Self {
        Self::new()
    }
}