use std::num;
use crate::{
buffer,
buffer::{
bytes::window,
storage,
write::{self, consume, consume::Consume},
},
mem::rc,
};
#[derive(Default)]
pub struct Snapshot<const MAX_CAPACITY: usize> {
pub(super) state: Option<window::State<(), MAX_CAPACITY>>,
pub(super) tail: u32,
}
impl<const MAX_CAPACITY: usize> Snapshot<MAX_CAPACITY> {
pub const fn new() -> Self {
Self {
state: None,
tail: 0,
}
}
pub fn try_with(capacity: num::NonZero<usize>) -> Result<Self, storage::AllocError> {
Self::try_copy(&[], capacity)
}
#[inline]
pub fn try_copy(
src: &[u8],
capacity: num::NonZero<usize>,
) -> Result<Self, storage::AllocError> {
let state = window::try_copy(src, capacity)?;
Ok(Self {
state: Some(state),
tail: 0,
})
}
pub fn spare(&mut self) -> write::Spare<'_> {
let Some(state) = &mut self.state else {
return unsafe { write::Spare::from_parts(&mut [], &mut self.tail) };
};
state.spare()
}
pub fn into_shared(self) -> Option<storage::Shared> {
if self.is_empty() {
return None;
}
Some(self.freeze())
}
pub fn freeze(self) -> storage::Shared {
let Some(state) = self.state else {
return storage::Shared::new();
};
let len = state.head - self.tail;
storage::Shared::from(unsafe { state.buf.into_range(self.tail, len) })
}
pub fn try_snapshot(&self) -> Result<Option<storage::Shared>, rc::Error> {
use buffer::raw::Retain as _;
let Some(state) = &self.state else {
return Ok(None);
};
if state.head == self.tail {
return Ok(None);
}
let len = state.head - self.tail;
Ok(Some(storage::Shared::from(unsafe {
state.buf.try_retain()?.into_range(self.tail, len)
})))
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn len(&self) -> usize {
self.state
.as_ref()
.map_or(0, |state| (state.head - self.tail) as usize)
}
pub fn clear(&mut self) {
if let Some(state) = &self.state {
self.tail = state.head;
}
}
pub fn try_as_mut(&mut self) -> Result<&mut [u8], storage::AllocError> {
let Some(state) = &mut self.state else {
return Ok(&mut []);
};
state.try_as_mut(&mut self.tail)
}
}
impl<const MAX_CAPACITY: usize> Consume for Snapshot<MAX_CAPACITY> {
fn remaining(&self) -> usize {
self.len()
}
fn try_consume(&mut self, amount: usize) -> Result<(), consume::Error> {
consume::Error::check(amount, self.len())?;
self.tail += amount as u32;
Ok(())
}
fn consume_max(&mut self, max: usize) -> usize {
let amount = max.min(self.len());
self.tail += amount as u32;
amount
}
}
impl<const MAX_CAPACITY: usize> AsRef<[u8]> for Snapshot<MAX_CAPACITY> {
fn as_ref(&self) -> &[u8] {
self.state.as_ref().map_or(&[], |state| {
unsafe { state.as_slice().get_unchecked(self.tail as usize..) }
})
}
}
impl<const MAX_CAPACITY: usize> buffer::Residency for Snapshot<MAX_CAPACITY> {
fn resident(&self) -> usize {
self.state
.as_ref()
.map_or(0, |state| state.capacity().get() as usize)
}
}
impl<const MAX_CAPACITY: usize> buffer::View<'_, Snapshot<MAX_CAPACITY>> {
pub fn try_retain(self) -> Result<storage::Shared, rc::Error> {
use buffer::raw::Retain as _;
match &self.source.state {
Some(state) if !self.is_empty() => {
let owner = state.buf.try_retain()?;
Ok(storage::Shared::from(unsafe {
storage::Shared::assume(self.as_slice(), owner)
}))
}
_ => Ok(storage::Shared::new()),
}
}
}
const _: () = assert!(size_of::<Snapshot<1>>() == 3 * size_of::<usize>());
unsafe impl<const MAX_CAPACITY: usize> buffer::raw::Source for Snapshot<MAX_CAPACITY> {}