use std::{alloc, marker, mem, num::NonZero, ptr, slice};
pub use bundle::Bundle;
pub use extent::Extent;
pub use layout::Layout;
pub use rows::Rows;
pub use shape::Shape;
pub use uninit::Uninit;
use crate::{
alloc::{Exhausted, Overflow},
base::marker::ThreadBound,
};
mod layout;
pub(crate) use layout::Root;
#[derive(Debug)]
pub struct Initialized;
mod bundle;
mod extent;
mod rows;
mod shape;
mod uninit;
pub struct Vacant<'a, T>(&'a mut Vec<T>, ThreadBound);
impl<'a, T> Vacant<'a, T> {
pub unsafe fn new(values: &'a mut Vec<T>) -> Self {
Self(values, ThreadBound::NEW)
}
pub fn insert(self, value: T) {
let Self(values, _bound) = self;
let len = values.len();
unsafe {
values.as_mut_ptr().add(len).write(value);
values.set_len(len + 1);
}
}
}
pub trait VecExt<T> {
unsafe fn swap_remove(&mut self, index: usize) -> (T, Option<&T>);
}
impl<T> VecExt<T> for Vec<T> {
unsafe fn swap_remove(&mut self, index: usize) -> (T, Option<&T>) {
let last = self.len() - 1;
let pointer = self.as_mut_ptr();
let value = unsafe { pointer.add(last).read() };
unsafe { self.set_len(last) };
if index == last {
return (value, None);
}
let slot = unsafe { &mut *pointer.add(index) };
(mem::replace(slot, value), Some(slot))
}
}
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct Empty<E: Extent>(E);
impl<E: Extent> Empty<E> {
pub const unsafe fn assume(extent: E) -> Self {
Self(extent)
}
pub(in crate::alloc) const fn into_inner(self) -> E {
self.0
}
}
pub unsafe trait TailExtent: Extent {
#[doc(hidden)]
type Index: TailIndex;
#[doc(hidden)]
fn indices(self) -> impl Iterator<Item = Self::Index>;
}
unsafe impl TailExtent for usize {
type Index = usize;
fn indices(self) -> impl Iterator<Item = Self::Index> {
0..self
}
}
unsafe impl TailExtent for NonZero<u32> {
type Index = u32;
fn indices(self) -> impl Iterator<Item = Self::Index> {
0..self.get()
}
}
unsafe impl TailExtent for NonZero<u16> {
type Index = u16;
fn indices(self) -> impl Iterator<Item = Self::Index> {
0..self.get()
}
}
pub unsafe trait TailIndex: Copy {
#[doc(hidden)]
fn get(self) -> usize;
}
unsafe impl TailIndex for usize {
fn get(self) -> usize {
self
}
}
unsafe impl TailIndex for u16 {
fn get(self) -> usize {
usize::from(self)
}
}
unsafe impl TailIndex for u32 {
fn get(self) -> usize {
const { assert!(usize::BITS >= u32::BITS) };
self as usize
}
}
#[repr(C)]
pub struct Tail<S, T> {
prior: S,
offset: usize,
value: marker::PhantomData<fn() -> T>,
}
pub trait Recipe: Sized {
type Fields;
fn try_build(self) -> Result<Bundle<Self::Fields>, Overflow>;
unsafe fn build(self) -> Bundle<Self::Fields>;
fn tail<T>(self, len: usize) -> impl Recipe<Fields = Tail<Self::Fields, T>> {
layout::Tail {
prior: self,
len,
value: marker::PhantomData,
}
}
}
pub trait LayoutExt: Sized {
fn try_allocate<T>(self) -> Result<ptr::NonNull<T>, Exhausted>;
unsafe fn deallocate<T>(self, pointer: ptr::NonNull<T>);
}
impl LayoutExt for alloc::Layout {
fn try_allocate<T>(self) -> Result<ptr::NonNull<T>, Exhausted> {
if self.size() == 0 {
return Ok(self.dangling_ptr().cast());
}
unsafe { try_new(self, false) }
}
unsafe fn deallocate<T>(self, pointer: ptr::NonNull<T>) {
if self.size() != 0 {
unsafe { dealloc(self, pointer) };
}
}
}
pub(crate) unsafe fn try_new<T>(
layout: alloc::Layout,
zeroed: bool,
) -> Result<ptr::NonNull<T>, Exhausted> {
let pointer = unsafe {
if zeroed {
alloc::alloc_zeroed(layout)
} else {
alloc::alloc(layout)
}
};
ptr::NonNull::new(pointer.cast()).ok_or(Exhausted)
}
pub(crate) unsafe fn dealloc<T>(layout: alloc::Layout, pointer: ptr::NonNull<T>) {
unsafe { alloc::dealloc(pointer.cast().as_ptr(), layout) };
}
pub(crate) unsafe fn resize(
layout: alloc::Layout,
pointer: ptr::NonNull<u8>,
target: alloc::Layout,
) -> Result<ptr::NonNull<u8>, Exhausted> {
let pointer = unsafe { alloc::realloc(pointer.as_ptr(), layout, target.size()) };
ptr::NonNull::new(pointer).ok_or(Exhausted)
}
pub(in crate::alloc) fn init<T>(
entries: &mut [mem::MaybeUninit<T>],
mut make: impl FnMut(usize) -> T,
) {
for (index, entry) in entries.iter_mut().enumerate() {
entry.write(make(index));
}
}
pub struct Fields<'a, S> {
fields: &'a S,
pointer: ptr::NonNull<u8>,
}
impl<T> Fields<'_, Layout<T>> {
pub const fn root(self) -> ptr::NonNull<T> {
self.pointer.cast()
}
}
impl<'a, S, T> Fields<'a, Tail<S, T>> {
pub fn tail(self) -> ptr::NonNull<T> {
unsafe { self.pointer.add(self.fields.offset) }.cast()
}
pub const fn prior(self) -> Fields<'a, S> {
Fields {
fields: &self.fields.prior,
pointer: self.pointer,
}
}
}
impl<S> Clone for Fields<'_, S> {
fn clone(&self) -> Self {
*self
}
}
impl<S> Copy for Fields<'_, S> {}
#[derive(Debug)]
#[repr(C)]
pub struct Block<E: Shape, S, X = (), R = mem::MaybeUninit<<E as Shape>::Root>> {
root: ptr::NonNull<R>,
tail: ptr::NonNull<E::Tail>,
extent: E,
pub state: S,
extra: X,
}
impl<E: Shape<Last = ()>, S> Block<E, S, Initialized, E::Root> {
pub fn values(&self) -> &[E::Extra] {
let extra = unsafe { self.as_ptr().byte_add(self.extent.extra()).cast() };
unsafe { slice::from_raw_parts(extra, self.extent.len()) }
}
}
impl<E: Shape, S, X, R> Drop for Block<E, S, X, R> {
fn drop(&mut self) {
unsafe {
ptr::drop_in_place(ptr::slice_from_raw_parts_mut(
self.root.as_ptr(),
self.extent.len(),
));
LayoutExt::deallocate(self.extent.layout(), self.root);
}
}
}
impl<E: Shape, S, X, R> Block<E, S, X, R> {
pub(crate) const fn as_ptr(&self) -> *mut R {
self.root.as_ptr()
}
pub const fn extent(&self) -> &E {
&self.extent
}
pub fn tail(&self) -> &[E::Tail] {
unsafe { slice::from_raw_parts(self.tail.as_ptr(), self.extent.tail_len()) }
}
pub fn parts_mut(&mut self) -> (&mut S, &mut [R], &mut [E::Tail]) {
unsafe {
(
&mut self.state,
slice::from_raw_parts_mut(self.root.as_ptr(), self.extent.len()),
slice::from_raw_parts_mut(self.tail.as_ptr(), self.extent.tail_len()),
)
}
}
}
impl<E: Shape, S, X, R> AsRef<[R]> for Block<E, S, X, R> {
fn as_ref(&self) -> &[R] {
unsafe { slice::from_raw_parts(self.root.as_ptr(), self.extent.len()) }
}
}
impl<E: Shape, S, X, R> AsMut<[R]> for Block<E, S, X, R> {
fn as_mut(&mut self) -> &mut [R] {
unsafe { slice::from_raw_parts_mut(self.root.as_ptr(), self.extent.len()) }
}
}
impl<E: Shape, S> Block<E, S, ptr::NonNull<E::Extra>, mem::MaybeUninit<E::Root>> {
pub fn values(&self) -> &[E::Extra] {
unsafe { slice::from_raw_parts(self.extra.as_ptr(), self.extent.len()) }
}
pub fn values_mut(&mut self) -> &mut [E::Extra] {
unsafe { slice::from_raw_parts_mut(self.extra.as_ptr(), self.extent.len()) }
}
}
type Parts<E, S> = Block<
E,
S,
(
ptr::NonNull<<E as Shape>::Extra>,
ptr::NonNull<<E as Shape>::Last>,
),
>;
impl<E: Rows, S> Parts<E, S> {
pub fn extra(&self) -> (&[E::Extra], &[E::Last]) {
unsafe {
(
slice::from_raw_parts(self.extra.0.as_ptr(), self.extent.len()),
slice::from_raw_parts(self.extra.1.as_ptr(), self.extent.len()),
)
}
}
pub fn extra_mut(&mut self) -> (&mut [E::Extra], &mut [E::Last]) {
unsafe {
(
slice::from_raw_parts_mut(self.extra.0.as_ptr(), self.extent.len()),
slice::from_raw_parts_mut(self.extra.1.as_ptr(), self.extent.len()),
)
}
}
}