o3 0.62.0

shared-nothing primitives
Documentation
//! Fixed-capacity, ownership-preserving protocol group queues.

use std::{mem, ops, ptr};

pub use batch::Batch;

use crate::{
    alloc,
    depot::{
        arena::slab::key,
        array::Index,
        capacity::Shortage,
        queue::{
            indexed::{slab, slab::phase},
            links, tagged,
        },
    },
    mem::{Lease, Return, lease, slot},
};

pub mod capacity;
pub mod drain;
mod map;
pub mod raw;

#[derive(Clone, Copy, PartialEq, Eq)]
enum EntryTag {}

/// Exact capacity rejection without a partially completed operation.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Error<const LANES: usize> {
    Entries(Shortage),
    Owners {
        lane: Index<LANES>,
        capacity: capacity::Count<LANES>,
        key: u64,
    },
    Branches {
        lane: Index<LANES>,
        capacity: capacity::Count<LANES>,
        key: u64,
    },
}

impl<const LANES: usize> Error<LANES> {
    fn owner(lane: Index<LANES>, key: u64, cause: map::Error<LANES>) -> Self {
        match cause {
            map::Error::Owners(capacity) => Self::Owners {
                lane,
                capacity,
                key,
            },
            map::Error::Branches(capacity) => Self::Branches {
                lane,
                capacity,
                key,
            },
        }
    }
}

/// Rejection retains the unqueued value and its exact cause.
pub struct Rejected<V, const LANES: usize> {
    pub error: Error<LANES>,
    pub lane: Index<LANES>,
    pub owner: u64,
    pub value: V,
}

/// A selected record still linked in its ledger.
#[must_use]
pub struct Front<'a, V, const LANES: usize> {
    ledger: &'a mut Ledger<V, LANES>,
    entry: map::Node<EntryTag>,
    lane: Index<LANES>,
}

impl<'a, V, const LANES: usize> Front<'a, V, LANES> {
    pub fn get(&self) -> &V {
        &self.entry.bind(&self.ledger.entries).value
    }

    pub fn unlink(self) -> Detached<'a, V, LANES> {
        let mut slot = self.entry.bind(&mut self.ledger.entries).detach();
        let entry = slot.as_mut();
        let next = entry.owner_next.take();
        self.ledger.map.detach(&entry.owner, self.lane, next);
        *self.lane.get_mut(&mut self.ledger.lane_lengths) -= 1;
        Detached(Lease::open(slot, ()))
    }

    pub fn remove(self) -> V {
        self.unlink().remove()
    }

    fn pop(self) -> (u64, V) {
        let entry = self.entry.bind(&self.ledger.entries);
        let owner = self.ledger.map.get(&entry.owner).key;
        (owner, self.remove())
    }

    pub fn take(self) -> (V, Vacancy<'a, V, LANES>) {
        let ledger = self.ledger;
        let (entry, slot) = self.entry.bind(&mut ledger.entries).take(self.lane);
        let (_, index) = slot.into_owner();
        ledger
            .map
            .get_mut(&entry.owner)
            .detach(self.lane, entry.owner_next);
        *self.lane.get_mut(&mut ledger.lane_lengths) -= 1;
        (
            entry.value,
            Vacancy {
                ledger,
                index,
                owner: entry.owner,
                lane: self.lane,
            },
        )
    }
}

/// Owns the unlinked payload and its physical slot until disposal.
#[must_use]
pub struct Detached<'a, V, const LANES: usize>(Lease<Record<'a, V, LANES>, lease::Open>);

impl<V, const LANES: usize> ops::Deref for Detached<'_, V, LANES> {
    type Target = V;

    fn deref(&self) -> &V {
        &self.0.sink().as_ref().value
    }
}

impl<V, const LANES: usize> Detached<'_, V, LANES> {
    pub fn get_mut(&mut self) -> &mut V {
        &mut self.0.parts_mut().0.as_mut().value
    }

    pub fn remove(self) -> V {
        self.0.into_parts().0.read().value
    }

    /// Releases the slot without running the payload's destructor.
    pub fn forget(self) {
        self.0.into_parts().0.forget();
    }
}

impl<V, const LANES: usize> Return for Record<'_, V, LANES> {
    type Item = ();

    fn return_item(self, (): ()) {
        self.discard();
    }
}

struct Entry<V> {
    owner: map::Node<map::Tag>,
    value: V,
    owner_next: Option<map::Node<EntryTag>>,
}

impl<V> Entry<V> {
    fn take<'a, const LANES: usize>(slot: Record<'a, V, LANES>) -> slot::Occupied<'a, V> {
        // SAFETY: the detached slot retains this exact initialized payload field.
        let value = unsafe { ptr::addr_of_mut!((*slot.value().cast::<Self>()).value) };
        slot.forget();
        // SAFETY: metadata has retired, and the original exclusive slot loan
        // prevents reuse until this final payload's owner is consumed.
        unsafe { slot::raw::Vacant::new(&mut *value.cast::<mem::MaybeUninit<V>>()).assume_init() }
    }
}

/// An extracted value's slot and detached owner affiliation, retained together.
#[must_use]
pub struct Vacancy<'a, V, const LANES: usize> {
    ledger: &'a mut Ledger<V, LANES>,
    index: links::Index,
    owner: map::Node<map::Tag>,
    lane: Index<LANES>,
}

impl<V, const LANES: usize> Drop for Vacancy<'_, V, LANES> {
    fn drop(&mut self) {
        drop(Vacant {
            core: &mut self.ledger.entries,
            index: self.index,
            phase: phase::Restore(self.lane),
        });
        self.ledger.map.prune(&self.owner);
    }
}

impl<V, const LANES: usize> Vacancy<'_, V, LANES> {
    pub fn restore(self, value: V) {
        let slot = Vacant {
            core: &mut self.ledger.entries,
            index: self.index,
            phase: phase::Restore(self.lane),
        };
        let owner = map::Node::new(self.owner.index());
        let (owner, next) = self.ledger.map.restore(owner, self.lane, slot.handle());
        *self.lane.get_mut(&mut self.ledger.lane_lengths) += 1;
        slot.restore(Entry {
            owner,
            value,
            owner_next: next,
        });
        mem::forget(self);
    }
}

impl<V, const LANES: usize> Ledger<V, LANES> {
    pub fn available(&self) -> usize {
        self.entries.available()
    }
    pub fn is_full(&self) -> bool {
        self.entries.is_full()
    }
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
    pub fn lane_len(&self, lane: Index<LANES>) -> usize {
        *lane.get(&self.lane_lengths) as usize
    }

    pub fn front(&self, lane: Index<LANES>) -> Option<&V> {
        Some(&self.entries.front_pair(lane)?.1.value)
    }

    pub fn get(&self, owner: u64, lane: Index<LANES>) -> Option<&V> {
        let owner = self.map.find(owner)?;
        let key = lane.get(&owner.heads).as_ref()?;
        Some(&key.bind(&self.entries).value)
    }

    pub fn iter_owner(&self, owner: u64, lane: Index<LANES>) -> Iter<'_, V, LANES> {
        Iter {
            entries: &self.entries,
            next: self
                .map
                .find(owner)
                .and_then(|owner| lane.get(&owner.heads).as_ref()),
        }
    }
}

/// Borrows one owner's unchanged lane in publication order.
pub struct Iter<'a, V, const LANES: usize> {
    entries: &'a Entries<V, LANES>,
    next: Option<&'a map::Node<EntryTag>>,
}

impl<'a, V, const LANES: usize> Iterator for Iter<'a, V, LANES> {
    type Item = &'a V;

    fn next(&mut self) -> Option<Self::Item> {
        let entry = self.next?.bind(self.entries);
        self.next = entry.owner_next.as_ref();
        Some(&entry.value)
    }
}

/// Bounded wire lanes and per-owner chains over one canonical value store.
pub struct Ledger<V, const LANES: usize> {
    entries: Entries<V, LANES>,
    map: map::Tree<LANES>,
    lane_lengths: [u32; LANES],
}

impl<V, const LANES: usize> Ledger<V, LANES> {
    pub const fn record_bytes() -> usize {
        mem::size_of::<Entry<V>>()
            + mem::size_of::<key::Handle<EntryTag, { u32::MAX }, true>>()
            + mem::size_of::<links::Hook>()
            + map::Tree::<LANES>::record_bytes()
    }

    pub fn try_new(capacity: capacity::Count<LANES>) -> Result<Self, alloc::Error> {
        Ok(Self {
            entries: slab::Slab::try_from(capacity)?,
            map: map::Tree::try_new(capacity)?,
            lane_lengths: [0; LANES],
        })
    }

    pub fn bind_front(&mut self, lane: Index<LANES>) -> Option<Front<'_, V, LANES>> {
        self.select([(lane, ())]).map(|((), front)| front)
    }

    /// Takes the first occupied lane while preserving its caller's policy value.
    pub fn take_first<I, const N: usize>(
        &mut self,
        order: [(Index<LANES>, I); N],
    ) -> Option<(I, (V, Vacancy<'_, V, LANES>))> {
        self.select(order).map(|(item, front)| (item, front.take()))
    }

    pub fn select<I, const N: usize>(
        &mut self,
        order: [(Index<LANES>, I); N],
    ) -> Option<(I, Front<'_, V, LANES>)> {
        let order = order.map(|(lane, item)| (lane, (lane, item)));
        let ((lane, item), entry) = self.entries.select(order)?;
        let (_, index) = entry.into_owner();
        Some((
            item,
            Front {
                ledger: self,
                entry: map::Node::new(index),
                lane,
            },
        ))
    }

    pub fn push_back(
        &mut self,
        lane: Index<LANES>,
        owner: u64,
        value: V,
    ) -> Result<(), Rejected<V, LANES>> {
        let slot = match Batch::new(self).and_then(|slot| slot.bind([(lane, owner)])) {
            Ok(slot) => slot,
            Err(error) => {
                return Err(Rejected {
                    error,
                    lane,
                    owner,
                    value,
                });
            }
        };
        slot.insert([value]);
        Ok(())
    }

    pub fn pop_front(&mut self, lane: Index<LANES>) -> Option<(u64, V)> {
        self.bind_front(lane).map(Front::pop)
    }
}

impl<V> Ledger<V, 1> {
    pub fn clear(&mut self, mut release: impl for<'a> FnMut(slot::Occupied<'a, V>)) {
        while let Some(((), entry)) = self.entries.select([(Index::at::<0>(), ())]) {
            release(Entry::take(entry.detach()));
        }
        self.map.clear();
        self.lane_lengths = [0];
    }
}

const _: () = {
    assert!(mem::size_of::<Entry<u64>>() == 2 * mem::size_of::<usize>());
    assert!(mem::size_of::<Front<'static, u64, 2>>() == 2 * mem::size_of::<usize>());
    assert!(mem::size_of::<Detached<'static, u64, 2>>() == 2 * mem::size_of::<usize>());
    assert!(mem::size_of::<Vacancy<'static, u64, 2>>() == 3 * mem::size_of::<usize>());
    assert!(mem::size_of::<Index<2>>() == 1);
    assert!(mem::size_of::<Option<Index<2>>>() == 1);
};

mod batch;

/// One reserved slot retaining its admission or restoration phase.
type Vacant<'a, V, const N: usize, P> = slab::raw::Vacant<
    'a,
    Entry<V>,
    EntryTag,
    { u32::MAX },
    tagged::raw::Hook,
    N,
    capacity::Count<N>,
    P,
>;

type Record<'a, V, const N: usize> = slab::raw::Slot<
    'a,
    Entry<V>,
    EntryTag,
    { u32::MAX },
    tagged::raw::Hook,
    N,
    capacity::Count<N>,
    false,
>;

type Entries<V, const N: usize> =
    slab::Slab<Entry<V>, EntryTag, { u32::MAX }, tagged::raw::Hook, N, capacity::Count<N>>;
type Slots<'a, V, const LANES: usize, const N: usize> = slab::Reserved<
    'a,
    Entry<V>,
    EntryTag,
    { u32::MAX },
    tagged::raw::Hook,
    LANES,
    N,
    capacity::Count<LANES>,
>;
type Place = (links::Index, Option<links::Index>);