o3 0.62.0

shared-nothing primitives
Documentation
use std::mem;

use crate::depot::{self, capacity};

/// Exact-capacity ring position and occupancy.
#[derive(Clone, Copy)]
#[repr(C)]
pub(crate) struct State<W> {
    head: W,
    len: W,
}

impl State<u32> {
    pub(in crate::depot::queue::ring) const fn count(self) -> capacity::Count {
        capacity::Count::new(self.len)
    }
}

impl<W: depot::raw::Word> State<W> {
    pub(crate) const EMPTY: Self = Self {
        head: W::ZERO,
        len: W::ZERO,
    };

    pub(crate) fn head(self) -> usize {
        self.head.offset()
    }

    pub(crate) fn len(self) -> usize {
        self.len.offset()
    }

    pub(crate) fn is_empty(self) -> bool {
        self.len() == 0
    }

    pub(crate) fn position(self, capacity: usize, offset: usize) -> usize {
        let suffix = capacity - self.head();
        if offset < suffix {
            self.head() + offset
        } else {
            offset - suffix
        }
    }

    /// Claims one complete suffix and returns its two physical runs.
    /// # Safety
    /// `0 < capacity <= W::MAX`, `head < capacity`, `len <= capacity - self.len()`.
    pub(crate) unsafe fn extend(&mut self, capacity: usize, len: usize) -> (usize, [usize; 2]) {
        let old = self.len();
        let tail = self.position(capacity, old);
        self.len = unsafe { W::from_offset(old + len) };
        super::runs(tail, capacity, len)
    }

    /// Releases a complete prefix and resets an empty ring's origin.
    /// # Safety
    /// `0 < capacity <= W::MAX`, `head < capacity`, `len <= self.len() <= capacity`.
    pub(crate) unsafe fn consume(&mut self, capacity: usize, len: usize) {
        let left = self.len() - len;
        let head = if left == 0 {
            0
        } else {
            self.position(capacity, len)
        };
        self.head = unsafe { W::from_offset(head) };
        self.len = unsafe { W::from_offset(left) };
    }

    /// Claims one end and increments occupancy in the retained direction.
    /// # Safety
    /// `len < capacity`, and `capacity` is representable by `W`.
    pub(super) unsafe fn push<const FRONT: bool>(&mut self, capacity: usize) -> usize {
        let len = self.len();
        let index = if FRONT {
            let head = self.head();
            let previous = if head == 0 { capacity - 1 } else { head - 1 };
            self.head = unsafe { W::from_offset(previous) };
            previous
        } else {
            self.position(capacity, len)
        };
        self.len = unsafe { W::from_offset(len + 1) };
        index
    }

    /// Releases and returns the physical head position.
    /// # Safety
    /// `len != 0`, `head < capacity`, and `capacity` is representable by `W`.
    pub(crate) unsafe fn pop_front(&mut self, capacity: usize) -> usize {
        let index = self.head();
        let next = index + 1;
        self.head = unsafe { W::from_offset(if next == capacity { 0 } else { next }) };
        self.len = unsafe { W::from_offset(self.len() - 1) };
        index
    }
}

const _: () = {
    assert!(mem::size_of::<State<u32>>() == mem::size_of::<u64>());
    assert!(mem::size_of::<State<usize>>() == 2 * mem::size_of::<usize>());
};