o3 0.62.0

shared-nothing primitives
Documentation
//! Fixed-capacity arrays specialized for [`Copy`] elements.

use std::{mem, ops};

use crate::depot::array::{self, Len, Vacancy};

/// An inline vector without drop glue.
#[derive(Copy, Debug)]
#[repr(C)]
pub struct Vec<T, const N: usize> {
    pub(super) len: Len<N>,
    pub(super) entries: [mem::MaybeUninit<T>; N],
}

impl<T, const N: usize> Vec<T, N> {
    pub(super) const EMPTY: Self = Self {
        len: Len::ZERO,
        entries: [const { mem::MaybeUninit::uninit() }; N],
    };

    pub(super) fn init_array<const M: usize>(&mut self, values: [T; M]) {
        self.fill(Len::exact::<M>(), values.into_iter());
    }

    pub(super) fn take_entries(&mut self) -> &mut [mem::MaybeUninit<T>] {
        mem::replace(&mut self.len, Len::ZERO).prefix_mut(&mut self.entries)
    }

    fn fill(&mut self, len: Len<N>, values: impl Iterator<Item = T>) {
        for (entry, value) in self.entries.iter_mut().zip(values) {
            entry.write(value);
        }
        self.len = len;
    }

    pub(super) fn init(len: Len<N>, make: impl FnMut(Vacancy<N>) -> T) -> Self {
        let mut prefix = Self::EMPTY;
        prefix.fill(len, len.positions().map(make));
        prefix
    }

    /// Maps one bounded source into this destination while retaining its borrow.
    pub fn map_from<'a, U: Copy>(
        &mut self,
        source: &'a Vec<U, N>,
        mut make: impl FnMut(Vacancy<N>, &'a U) -> T,
    ) {
        drop(array::raw::Drain::take(self));
        let values = source
            .len
            .positions()
            .zip(&**source)
            .map(|(slot, value)| make(slot, value));
        self.fill(source.len, values);
    }

    pub fn vacancy(&mut self) -> Option<array::Vacant<'_, T, N>> {
        let slot = self.len.vacancy()?;
        Some(array::Vacant { raw: self, slot })
    }

    /// Returns the initialized-prefix length with its fixed capacity attached.
    pub const fn extent(&self) -> Len<N> {
        self.len
    }
}

impl<T: Copy, const N: usize> Clone for Vec<T, N> {
    fn clone(&self) -> Self {
        *self
    }
}

/// A consuming iterator without drop glue.
#[derive(Debug)]
#[repr(C)]
pub struct IntoIter<T, const N: usize> {
    pub(super) entries: [mem::MaybeUninit<T>; N],
    pub(super) front: Len<N>,
    pub(super) back: Len<N>,
}

const _: () = {
    assert!(mem::size_of::<IntoIter<u8, 4>>() == 12);
    assert!(mem::size_of::<IntoIter<u64, 4>>() == 40);
    assert!(mem::align_of::<IntoIter<u64, 4>>() == 8);
};

impl<T: Copy, const N: usize> Vec<T, N> {
    pub const fn new() -> Self {
        Self::EMPTY
    }

    pub fn from_one(value: T) -> Self {
        let mut prefix = Self::new();
        prefix.init_array([value]);
        prefix
    }

    pub fn retain(&mut self, keep: impl FnMut(&T) -> bool) {
        array::raw::retain(self, keep);
    }

    pub fn pop(&mut self) -> Option<T> {
        array::raw::pop(self)
    }
}

impl<T: Copy, const N: usize> Default for Vec<T, N> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: Copy, const N: usize> From<[T; N]> for Vec<T, N> {
    fn from(values: [T; N]) -> Self {
        let mut prefix = Self::new();
        prefix.init_array(values);
        prefix
    }
}

impl<T, const N: usize> ops::Deref for Vec<T, N> {
    type Target = [T];

    fn deref(&self) -> &Self::Target {
        array::raw::slice(self)
    }
}

impl<T, const N: usize> ops::DerefMut for Vec<T, N> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        array::raw::slice_mut(self)
    }
}

impl<T: Copy, const N: usize> IntoIterator for Vec<T, N> {
    type IntoIter = IntoIter<T, N>;
    type Item = T;

    fn into_iter(self) -> Self::IntoIter {
        IntoIter::new(self)
    }
}

impl<T, const N: usize> IntoIter<T, N> {
    pub(super) fn new(prefix: Vec<T, N>) -> Self {
        Self {
            entries: prefix.entries,
            front: Len::ZERO,
            back: prefix.len,
        }
    }
}

impl<T, const N: usize> Iterator for IntoIter<T, N> {
    type Item = T;

    fn next(&mut self) -> Option<T> {
        array::raw::next(self)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self.back.get() - self.front.get();
        (len, Some(len))
    }
}

impl<T, const N: usize> ExactSizeIterator for IntoIter<T, N> {}