use std::{cell, num::NonZero, ops, ptr};
use crate::{
buffer::{self, storage},
mem,
};
mod from;
mod init;
mod owners;
mod tag;
pub(in crate::buffer) use init::{Complete, Init};
pub use tag::Tag;
#[derive(Debug)]
#[repr(transparent)]
pub struct Extent(pub(super) ptr::NonNull<[u8]>);
impl Extent {
pub(in crate::buffer) const EMPTY: Self = Self(ptr::NonNull::slice_from_raw_parts(
ptr::NonNull::dangling(),
0,
));
pub(in crate::buffer) const unsafe fn new(ptr: ptr::NonNull<u8>, len: usize) -> Self {
Self(ptr::NonNull::slice_from_raw_parts(ptr, len))
}
pub(in crate::buffer) fn from_bytes(bytes: &[u8]) -> Self {
Self(ptr::NonNull::from(bytes))
}
pub(in crate::buffer) const fn len(&self) -> usize {
self.0.len()
}
pub(in crate::buffer) const unsafe fn bind<O>(self, owner: O) -> storage::Shared<O> {
storage::Shared { owner, bytes: self }
}
pub(in crate::buffer) unsafe fn restrict(&self, range: ops::Range<usize>) -> Self {
let len = range.end - range.start;
let pointer = self.0.cast::<u8>().as_ptr().wrapping_add(range.start);
unsafe { Self::new(ptr::NonNull::new_unchecked(pointer), len) }
}
}
const _: () = {
assert!(size_of::<Extent>() == 2 * size_of::<usize>());
assert!(usize::BITS >= 64);
};
#[repr(C, align(8))]
#[derive(Debug)]
pub(in crate::buffer) struct Prefix<T: Copy = u32> {
refs: mem::raw::Count,
word: cell::Cell<T>,
}
impl<T: Copy> Prefix<T> {
pub(in crate::buffer) const fn empty(word: T) -> Self {
Self {
refs: mem::raw::Count::empty(),
word: cell::Cell::new(word),
}
}
pub(in crate::buffer) const fn one(word: T) -> Self {
Self {
refs: mem::raw::Count::one(),
word: cell::Cell::new(word),
}
}
pub(in crate::buffer) fn word(&self) -> T {
self.word.get()
}
pub(in crate::buffer) unsafe fn write(&self, word: T) {
self.word.set(word);
}
}
unsafe impl<T: Copy> mem::raw::Edge for Prefix<T> {
fn count(&self) -> &mem::raw::Count {
&self.refs
}
}
pub(in crate::buffer) fn share<P: buffer::raw::Policy>(
owner: storage::Owned<P>,
) -> Option<storage::Shared<crate::mem::raw::Owner<buffer::raw::Header<P>>>> {
let heap = owner.allocation?;
let len = NonZero::new(owner.len)?;
Some(unsafe { buffer::raw::Header::into_range(heap.into_shared(), 0, len.get()) })
}
pub(in crate::buffer::storage) fn length<T: AsRef<[u8]>>(
owner: &storage::NonEmpty<T>,
) -> NonZero<usize> {
unsafe { NonZero::new_unchecked(owner.as_ref().len()) }
}
pub(in crate::buffer) unsafe fn present<T: buffer::raw::Source>(owner: T) -> storage::NonEmpty<T> {
storage::NonEmpty(owner)
}
unsafe impl<T: buffer::raw::Source> buffer::raw::Source for storage::NonEmpty<T> {}
unsafe impl<T> mem::raw::Convert<T> for storage::NonEmpty<T> {
fn into(self) -> T {
self.into_storage()
}
}
pub(in crate::buffer) unsafe fn consume<T: buffer::raw::Segment>(
owner: &mut storage::NonEmpty<T>,
amount: NonZero<usize>,
) {
let extent = unsafe { owner.0.extent() };
*extent = unsafe { extent.restrict(amount.get()..extent.len()) };
}
pub(in crate::buffer) unsafe fn split<T: buffer::raw::Segment>(
owner: &mut storage::NonEmpty<T>,
amount: NonZero<usize>,
edge: T::Backing,
) -> storage::NonEmpty<T> {
let extent = unsafe { owner.0.extent() };
let prefix = unsafe { extent.restrict(0..amount.get()) };
*extent = unsafe { extent.restrict(amount.get()..extent.len()) };
storage::NonEmpty(unsafe { prefix.bind(edge) }.into())
}