o3 0.62.0

shared-nothing primitives
Documentation
//! In-place initialization whose guard borrows the backing array.

use std::{iter, marker, mem, ops, ptr::NonNull, slice};

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

/// A borrowed prefix whose initialized positions retain one invariant identity.
pub struct Prefix<'a, 'id, T, const N: usize> {
    storage: array::raw::Drain<'a, T>,
    brand: marker::PhantomData<*mut capacity::Domain<'id>>,
}

/// Exclusive admission to initialize one exact position in a scoped prefix.
#[must_use = "a prefix slot must be filled or abandoned"]
pub struct Slot<'a, 'b, 'id, T, const N: usize> {
    prefix: &'a mut Prefix<'b, 'id, T, N>,
    slot: array::Vacancy<N>,
}

/// Exclusive initialization of one caller-owned array.
#[must_use = "dropping a guard releases its initialized prefix"]
pub struct Guard<'a, T, const N: usize, const REVERSE: bool = false, P = ()> {
    storage: array::raw::Drain<'a, T, REVERSE>,
    phase: P,
}

pub struct Row<'loan, 'a, 'source, T, V, const N: usize, const REVERSE: bool> {
    source: &'source V,
    guard: &'loan mut Guard<'a, T, N, REVERSE, slice::Iter<'source, V>>,
}

/// Marks a guard with an exact next uninitialized position.
#[derive(Debug)]
pub struct Vacant;

/// Marks a guard whose complete array is initialized.
#[derive(Debug)]
pub struct Full;

impl<'a, 'id, T, const N: usize> Prefix<'a, 'id, T, N> {
    /// The operation can borrow the guard but cannot take its destruction duty.
    pub fn with<U, R>(
        entries: &'a mut [mem::MaybeUninit<U>; N],
        run: impl for<'brand> FnOnce(&mut Prefix<'a, 'brand, T, N>) -> R,
    ) -> R {
        const {
            assert!(N <= u8::MAX as usize + 1);
            assert!(size_of::<T>() == size_of::<U>(), "size");
            assert!(align_of::<T>() == align_of::<U>(), "align");
        };
        run(&mut Prefix {
            storage: array::raw::Drain::empty(entries),
            brand: marker::PhantomData,
        })
    }

    pub fn vacancy(&mut self) -> Option<Slot<'_, 'a, 'id, T, N>> {
        let slot = self.extent().vacancy()?;
        Some(Slot { prefix: self, slot })
    }

    pub const fn extent(&self) -> array::Len<N> {
        array::Len(self.storage.extent() as u32)
    }

    pub fn as_slice(&self) -> &[T] {
        self.storage.as_slice()
    }

    pub fn as_mut_slice(&mut self) -> &mut [T] {
        self.storage.as_mut_slice()
    }

    pub fn get(&self, key: capacity::Key<'id, u8>) -> &T {
        array::raw::Drain::key(self, key)
    }

    pub fn get_mut(&mut self, key: capacity::Key<'id, u8>) -> &mut T {
        array::raw::Drain::key_mut(self, key)
    }
}

impl<'a, 'b, 'id, T, const N: usize> Slot<'a, 'b, 'id, T, N> {
    pub(in crate::depot) fn parts(self) -> (NonNull<array::raw::Drain<'b, T>>, array::Vacancy<N>) {
        (NonNull::from_mut(&mut self.prefix.storage), self.slot)
    }

    /// Initializes the admitted position before issuing its retained key.
    pub fn insert(self, value: T) -> capacity::Key<'id, u8> {
        array::raw::Drain::publish(self, value)
    }
}

impl<'a, T, const N: usize, const REVERSE: bool> Guard<'a, T, N, REVERSE> {
    pub fn new(entries: &'a mut mem::MaybeUninit<[T; N]>) -> Self {
        Self {
            storage: array::raw::Drain::empty(entries.as_mut()),
            phase: (),
        }
    }

    /// Retains one destination vacancy per admitted source position.
    pub fn from_source<'source, U, V>(
        entries: &'a mut [mem::MaybeUninit<U>; N],
        source: &'source Prefix<'_, '_, V, N>,
    ) -> Guard<'a, T, N, REVERSE, slice::Iter<'source, V>> {
        Guard {
            storage: array::raw::Drain::empty(entries),
            phase: source.as_slice().iter(),
        }
    }

    pub fn next(
        self,
    ) -> ops::ControlFlow<Guard<'a, T, N, REVERSE, Full>, Guard<'a, T, N, REVERSE, Vacant>> {
        if self.storage.extent() < N {
            return ops::ControlFlow::Continue(self.phase(Vacant));
        }
        ops::ControlFlow::Break(self.phase(Full))
    }
}

impl<'a, 'source, T, V, const N: usize, const REVERSE: bool>
    Guard<'a, T, N, REVERSE, slice::Iter<'source, V>>
{
    pub fn next_source(&mut self) -> Option<Row<'_, 'a, 'source, T, V, N, REVERSE>> {
        let source = self.phase.next()?;
        Some(Row {
            source,
            guard: self,
        })
    }

    pub fn finish(self) -> Guard<'a, T, N, REVERSE> {
        self.phase(())
    }
}

impl<'a, 'b, 'source, T, V, const N: usize, const REVERSE: bool>
    Row<'a, 'b, 'source, T, V, N, REVERSE>
{
    pub(in crate::depot) fn storage(self) -> NonNull<array::raw::Drain<'b, T, REVERSE>> {
        NonNull::from_mut(&mut self.guard.storage)
    }

    pub fn source(&self) -> &'source V {
        self.source
    }

    pub fn insert(self, value: T) {
        array::raw::Drain::fill_row(self, value);
    }
}

impl<T, const N: usize, const REVERSE: bool> Guard<'_, T, N, REVERSE, Full> {
    pub fn into_array(self) -> [T; N] {
        array::raw::Drain::take_full(self)
    }
}

impl<'a, T, const N: usize, const REVERSE: bool> Guard<'a, T, N, REVERSE, Vacant> {
    pub fn get<'b, U>(&self, values: &'b [U; N]) -> &'b U {
        array::raw::Drain::at(self, values)
    }

    pub fn zip<'b, 'c, U>(
        &'b self,
        values: &'c [U; N],
    ) -> iter::Zip<slice::Iter<'b, T>, slice::Iter<'c, U>> {
        array::raw::Drain::paired(self, values)
    }

    pub fn insert(self, value: T) -> Guard<'a, T, N, REVERSE> {
        Guard {
            storage: array::raw::Drain::fill_vacancy(self, value),
            phase: (),
        }
    }
}

impl<'a, T, const N: usize, const REVERSE: bool, P> Guard<'a, T, N, REVERSE, P> {
    pub fn as_slice(&self) -> &[T] {
        self.storage.as_slice()
    }

    fn phase<Q>(self, phase: Q) -> Guard<'a, T, N, REVERSE, Q> {
        Guard {
            storage: self.storage,
            phase,
        }
    }
}

impl<'a, T, const N: usize, const REVERSE: bool, P> IntoIterator for Guard<'a, T, N, REVERSE, P> {
    type Item = T;
    type IntoIter = array::raw::Drain<'a, T, REVERSE>;

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

const _: () = {
    assert!(size_of::<Prefix<'static, 'static, u8, 256>>() == 2 * size_of::<usize>());
    assert!(size_of::<Guard<'static, u8, 1>>() == 2 * size_of::<usize>());
    assert!(size_of::<Guard<'static, u8, 1, false, Vacant>>() == 2 * size_of::<usize>());
    assert!(size_of::<Guard<'static, u8, 1, false, Full>>() == 2 * size_of::<usize>());
};