use std::{mem, num::NonZero, ops};
use crate::{
buffer::{self, hold, is_within, segments, write::consume},
depot::capacity,
};
mod fifo;
pub(in crate::buffer) mod raw;
pub use fifo::Fifo;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RetainError {
Segments(segments::Error),
Copy(hold::CopyError),
}
impl From<segments::Error> for RetainError {
fn from(cause: segments::Error) -> Self {
Self::Segments(cause)
}
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Capacity {
bytes: capacity::Count,
segments: capacity::Count,
}
impl Capacity {
#[must_use]
pub const fn new(bytes: capacity::Count, segments: capacity::Count) -> Self {
Self { bytes, segments }
}
#[must_use]
pub const fn bytes(self) -> capacity::Count {
self.bytes
}
#[must_use]
pub const fn segments(self) -> capacity::Count {
self.segments
}
}
const _: () = assert!(mem::size_of::<Capacity>() == mem::size_of::<u64>());
const _: () = {
assert!(mem::size_of::<Fifo<u64>>() == 4 * mem::size_of::<u64>());
assert!(mem::size_of::<Option<Fifo<u64>>>() == mem::size_of::<Fifo<u64>>());
};
#[derive(Debug, PartialEq, Eq)]
pub enum Error {
Bytes {
requested: NonZero<usize>,
available: usize,
},
Segments {
capacity: usize,
},
}
const _: () = assert!(size_of::<Error>() == 2 * size_of::<usize>());
#[must_use]
pub struct Drain<'a, T> {
target: &'a mut Fifo<T>,
amount: u32,
}
impl<'a, T: buffer::raw::Segment> Drain<'a, T> {
pub fn try_new(target: &'a mut Fifo<T>, amount: usize) -> Result<Self, consume::Error> {
consume::Error::check(amount, target.len())?;
Ok(Self {
target,
amount: amount as u32,
})
}
pub fn fragments(&self) -> Iter<'_, T> {
Iter::new(self.target.cursor(), 0, self.amount)
}
pub fn commit(self) {
raw::commit(self);
}
}
pub struct Cursor<'a, T> {
queue: &'a Fifo<T>,
segment: u32,
start: u32,
}
impl<'a, T> Cursor<'a, T> {
const fn new(queue: &'a Fifo<T>) -> Self {
Self {
queue,
segment: 0,
start: 0,
}
}
fn end(&self, offset: usize, len: usize) -> Result<u32, Bounds> {
let floor = self.start as usize;
let ceiling = self.queue.len();
if offset < floor || offset > ceiling || len > ceiling - offset {
return Err(Bounds {
offset,
amount: len,
floor,
ceiling,
});
}
Ok((offset + len) as u32)
}
}
impl<T> Cursor<'_, T> {
#[must_use]
pub const fn is_empty(&self) -> bool {
self.len() == 0
}
pub const fn len(&self) -> usize {
self.queue.len()
}
}
impl<T: buffer::raw::Segment> Cursor<'_, T> {
pub fn seek(&mut self, offset: usize) -> Result<(), Bounds> {
raw::seek(self, offset)
}
}
const _: () = assert!(mem::size_of::<Cursor<'static, u64>>() == 2 * mem::size_of::<u64>());
pub struct Array<'a, T, const N: usize> {
source: Iter<'a, T>,
}
impl<'a, T: buffer::raw::Segment, const N: usize> Array<'a, T, N> {
pub fn try_new(location: &Cursor<'a, T>, offset: usize) -> Result<Self, Bounds> {
let end = location.end(offset, N)?;
Ok(Self {
source: Iter::new(Cursor { ..*location }, offset as u32, end),
})
}
pub fn read<'out>(self, target: &'out mut mem::MaybeUninit<[u8; N]>) -> &'out [u8; N]
where
'a: 'out,
{
raw::read(self, target)
}
pub fn copy(self, target: &mut [u8; N]) {
raw::copy(self, target);
}
}
const _: () = assert!(size_of::<Array<'static, (), 9>>() == 3 * size_of::<usize>());
pub struct Iter<'a, T> {
location: Cursor<'a, T>,
start: u32,
end: u32,
}
impl<'a, T> Iter<'a, T> {
const fn new(location: Cursor<'a, T>, start: u32, end: u32) -> Self {
Self {
location,
start,
end,
}
}
pub(in crate::buffer) fn remaining(&self) -> usize {
(self.end - self.start) as usize
}
}
impl<'a, T: buffer::raw::Segment> Iterator for Iter<'a, T> {
type Item = &'a [u8];
fn next(&mut self) -> Option<Self::Item> {
Some(raw::next_view(self)?.into_storage().as_slice())
}
}
const _: () = assert!(mem::size_of::<Iter<'static, u64>>() == 3 * mem::size_of::<u64>());
pub struct Slice<'a, T> {
queue: &'a Fifo<T>,
start: u32,
end: u32,
}
impl<T> Clone for Slice<'_, T> {
fn clone(&self) -> Self {
*self
}
}
impl<T> Copy for Slice<'_, T> {}
impl<'a, T> Slice<'a, T> {
#[must_use]
pub const fn len(&self) -> usize {
(self.end - self.start) as usize
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.start == self.end
}
#[must_use]
pub fn get(&self, range: ops::Range<usize>) -> Option<Self> {
if !is_within(&range, self.len()) {
return None;
}
Some(Self {
queue: self.queue,
start: self.start + range.start as u32,
end: self.start + range.end as u32,
})
}
#[must_use]
pub fn contiguous(&self) -> Option<&'a [u8]>
where
T: buffer::raw::Segment,
{
let first = self.fragments().next().unwrap_or(&[]);
(first.len() == self.len()).then_some(first)
}
#[must_use]
pub const fn fragments(self) -> Iter<'a, T> {
Iter::new(self.queue.cursor(), self.start, self.end)
}
pub(in crate::buffer) fn chunks_from(self, offset: usize) -> Option<Iter<'a, T>> {
self.get(offset..self.len()).map(Self::fragments)
}
}
const _: () = assert!(mem::size_of::<Slice<'static, u64>>() == 2 * mem::size_of::<u64>());
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Bounds {
pub offset: usize,
pub amount: usize,
pub floor: usize,
pub ceiling: usize,
}
const _: () = assert!(size_of::<Bounds>() == 4 * size_of::<usize>());