dope-core 0.77.0

Owned native I/O and completion driver for dope
Documentation
use o3::{
    alloc,
    depot::{self, capacity},
};
use table::{borrows, entries::occupied};

use crate::driver::{route, route::indices, route::table};

#[repr(transparent)]
pub struct Slab<T, Tag: route::Tag> {
    inner: table::Storage<T, Tag>,
}

impl<T, Tag: route::Tag> Slab<T, Tag> {
    /// Reserves the complete typed target table transactionally.
    pub fn try_new(cap: table::Capacity) -> Result<Self, alloc::Error> {
        Ok(Self {
            inner: table::Storage::try_new(cap.0)?,
        })
    }

    /// Returns the immutable extent admitted when this route table was allocated.
    pub fn capacity(&self) -> table::Capacity {
        table::Capacity(self.inner.capacity())
    }

    pub fn len(&self) -> capacity::Count {
        self.inner.len()
    }

    /// Returns capacity minus the number of initialized values.
    ///
    /// Forgotten reservations are included, so this is an upper bound on
    /// reservable slots. Reservation still reports `Full` or `IdentityExhausted`.
    pub fn available(&self) -> capacity::Count {
        let total = self.inner.capacity();
        let occupied = self.inner.len();
        capacity::Count::new(total.raw() - occupied.raw())
    }

    pub fn is_empty(&self) -> bool {
        self.inner.len() == capacity::Count::EMPTY
    }

    pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
        self.inner.iter_mut()
    }

    pub fn next_epoch(&self) -> Option<route::Epoch> {
        self.inner.next()
    }

    pub fn get(&self, parts: table::Parts<Tag>) -> Option<&T> {
        self.inner.get(parts.external())
    }

    pub fn contains(&self, parts: table::Parts<Tag>) -> bool {
        self.get(parts).is_some()
    }

    pub fn key_at(&self, slot: indices::SlotIndex) -> Option<table::Key<Tag>> {
        self.inner.key_at(slot.raw()).map(table::Key::new)
    }

    pub fn current(
        physical: depot::raw::Proof<(&Self, indices::SlotIndex), capacity::Physical>,
    ) -> Option<table::Key<Tag>> {
        let (slab, slot) = physical.into_inner();
        // SAFETY: the index and the original fixed slab are unchanged.
        Option::from(unsafe {
            depot::raw::Proof::<_, capacity::Physical>::new((&slab.inner, slot.raw()))
        })
        .map(table::Key::new)
    }

    pub fn access(&mut self) -> borrows::Access<'_, T, Tag> {
        borrows::Access(&mut self.inner)
    }
}

// SAFETY: the initialized proof retains the exclusive loan of this row and slab.
unsafe impl<'a, T, Tag: route::Tag> depot::raw::Project<indices::SlotIndex>
    for &'a mut Slab<T, Tag>
{
    type Output = occupied::Entry<'a, T, Tag>;

    unsafe fn project(self, slot: indices::SlotIndex) -> Self::Output {
        // SAFETY: the same owner and bounded zero-based position select the proven row.
        occupied::Entry(unsafe {
            depot::raw::Project::<_, ()>::project(&mut self.inner, slot.index().into())
        })
    }
}