mod byte_ring;
mod capacity;
mod owned;
mod pool;
mod raw;
mod rolling;
mod shared;
mod shared_pool;
mod snapshot_buf;
mod view;
use std::mem::MaybeUninit;
use std::ptr::{self, NonNull, copy_nonoverlapping};
use crate::marker::ThreadBound;
pub use byte_ring::ByteRing;
pub use capacity::CapacityError;
pub use owned::{Owned, Writer};
pub use pool::{Lease, Pool};
pub use rolling::RollingBuffer;
pub use shared::Shared;
pub use shared_pool::{Pooled, SharedLease, SharedPool};
pub use snapshot_buf::SnapshotBuf;
pub use view::{OwnedView, View};
pub struct SpareWriter<'a> {
ptr: NonNull<MaybeUninit<u8>>,
capacity: usize,
written: usize,
target: &'a mut u32,
_thread: ThreadBound,
}
impl<'a> SpareWriter<'a> {
pub fn capacity(&self) -> usize {
self.capacity
}
pub fn len(&self) -> usize {
self.written
}
pub fn is_empty(&self) -> bool {
self.written == 0
}
pub fn remaining(&self) -> usize {
self.capacity - self.written
}
pub fn as_mut_ptr(&mut self) -> *mut u8 {
unsafe { self.ptr.as_ptr().add(self.written).cast() }
}
pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<u8>] {
unsafe {
std::slice::from_raw_parts_mut(self.ptr.as_ptr().add(self.written), self.remaining())
}
}
pub fn try_commit_initialized(&mut self, initialized: &[u8]) -> Result<(), CapacityError> {
let attempted = self
.written
.checked_add(initialized.len())
.ok_or_else(|| CapacityError::new(usize::MAX, self.capacity))?;
if initialized.as_ptr() != self.as_mut_ptr() || attempted > self.capacity {
return Err(CapacityError::new(attempted, self.capacity));
}
self.written = attempted;
Ok(())
}
pub fn try_push(&mut self, byte: u8) -> Result<(), CapacityError> {
if self.written == self.capacity {
return Err(CapacityError::new(self.written + 1, self.capacity));
}
unsafe {
self.ptr
.as_ptr()
.add(self.written)
.write(MaybeUninit::new(byte))
};
self.written += 1;
Ok(())
}
pub fn try_extend_from_slice(&mut self, src: &[u8]) -> Result<(), CapacityError> {
let attempted = self
.written
.checked_add(src.len())
.ok_or_else(|| CapacityError::new(usize::MAX, self.capacity))?;
if attempted > self.capacity {
return Err(CapacityError::new(attempted, self.capacity));
}
unsafe {
copy_nonoverlapping(
src.as_ptr(),
self.ptr.as_ptr().add(self.written).cast(),
src.len(),
)
};
self.written = attempted;
Ok(())
}
pub fn finish(self) -> usize {
self.written
}
unsafe fn new(ptr: *mut MaybeUninit<u8>, capacity: usize, target: &'a mut u32) -> Self {
debug_assert!(capacity <= (u32::MAX - *target) as usize);
Self {
ptr: unsafe { NonNull::new_unchecked(ptr) },
capacity,
written: 0,
target,
_thread: ThreadBound::NEW,
}
}
fn commit(&mut self) {
*self.target = self.target.wrapping_add(self.written as u32);
self.written = 0;
}
}
impl Drop for SpareWriter<'_> {
fn drop(&mut self) {
self.commit();
}
}
unsafe fn compact(buf: *mut MaybeUninit<u8>, head: &mut u32, tail: &mut u32) {
if *head == 0 {
return;
}
let len = (*tail - *head) as usize;
if len != 0 {
unsafe { ptr::copy(buf.add(*head as usize), buf, len) };
}
*head = 0;
*tail = len as u32;
}
unsafe fn consume(head: &mut u32, tail: &mut u32, amount: usize) {
*head = head.wrapping_add(amount as u32);
if *head == *tail {
*head = 0;
*tail = 0;
}
}