use core::ops::{Index, IndexMut};
use crate::ringbuffer_trait::{
RingBuffer, RingBufferIntoIterator, RingBufferIterator, RingBufferMutIterator,
};
extern crate alloc;
use crate::{mask_and, GrowableAllocRingBuffer};
use core::ptr;
#[derive(Debug)]
pub struct AllocRingBuffer<T> {
buf: *mut T,
size: usize,
capacity: usize,
readptr: usize,
writeptr: usize,
}
unsafe impl<T: Sync> Sync for AllocRingBuffer<T> {}
unsafe impl<T: Send> Send for AllocRingBuffer<T> {}
impl<T, const N: usize> From<[T; N]> for AllocRingBuffer<T> {
fn from(value: [T; N]) -> Self {
let mut rb = Self::new(value.len());
rb.extend(value);
rb
}
}
impl<T: Clone, const N: usize> From<&[T; N]> for AllocRingBuffer<T> {
#[allow(trivial_casts)]
fn from(value: &[T; N]) -> Self {
Self::from(value as &[T])
}
}
impl<T: Clone> From<&[T]> for AllocRingBuffer<T> {
fn from(value: &[T]) -> Self {
let mut rb = Self::new(value.len());
rb.extend(value.iter().cloned());
rb
}
}
impl<T> From<GrowableAllocRingBuffer<T>> for AllocRingBuffer<T> {
fn from(mut v: GrowableAllocRingBuffer<T>) -> AllocRingBuffer<T> {
let mut rb = AllocRingBuffer::new(v.len());
rb.extend(v.drain());
rb
}
}
impl<T: Clone> From<&mut [T]> for AllocRingBuffer<T> {
fn from(value: &mut [T]) -> Self {
Self::from(&*value)
}
}
impl<T: Clone, const CAP: usize> From<&mut [T; CAP]> for AllocRingBuffer<T> {
fn from(value: &mut [T; CAP]) -> Self {
Self::from(value.clone())
}
}
impl<T> From<alloc::vec::Vec<T>> for AllocRingBuffer<T> {
fn from(value: alloc::vec::Vec<T>) -> Self {
let mut res = AllocRingBuffer::new(value.len());
res.extend(value);
res
}
}
impl<T> From<alloc::collections::VecDeque<T>> for AllocRingBuffer<T> {
fn from(value: alloc::collections::VecDeque<T>) -> Self {
let mut res = AllocRingBuffer::new(value.len());
res.extend(value);
res
}
}
impl<T> From<alloc::collections::LinkedList<T>> for AllocRingBuffer<T> {
fn from(value: alloc::collections::LinkedList<T>) -> Self {
let mut res = AllocRingBuffer::new(value.len());
res.extend(value);
res
}
}
impl From<alloc::string::String> for AllocRingBuffer<char> {
fn from(value: alloc::string::String) -> Self {
let mut res = AllocRingBuffer::new(value.len());
res.extend(value.chars());
res
}
}
impl From<&str> for AllocRingBuffer<char> {
fn from(value: &str) -> Self {
let mut res = AllocRingBuffer::new(value.len());
res.extend(value.chars());
res
}
}
impl<T, const CAP: usize> From<crate::ConstGenericRingBuffer<T, CAP>> for AllocRingBuffer<T> {
fn from(mut value: crate::ConstGenericRingBuffer<T, CAP>) -> Self {
let mut res = AllocRingBuffer::new(value.len());
res.extend(value.drain());
res
}
}
impl<T> Drop for AllocRingBuffer<T> {
fn drop(&mut self) {
self.drain().for_each(drop);
let layout = alloc::alloc::Layout::array::<T>(self.size).unwrap();
unsafe {
alloc::alloc::dealloc(self.buf as *mut u8, layout);
}
}
}
impl<T: Clone> Clone for AllocRingBuffer<T> {
fn clone(&self) -> Self {
debug_assert_ne!(self.capacity, 0);
let mut new = Self::new(self.capacity);
self.iter().cloned().for_each(|i| new.push(i));
new
}
}
impl<T: PartialEq> PartialEq for AllocRingBuffer<T> {
fn eq(&self, other: &Self) -> bool {
self.capacity == other.capacity
&& self.len() == other.len()
&& self.iter().zip(other.iter()).all(|(a, b)| a == b)
}
}
impl<T: Eq + PartialEq> Eq for AllocRingBuffer<T> {}
impl<T> IntoIterator for AllocRingBuffer<T> {
type Item = T;
type IntoIter = RingBufferIntoIterator<T, Self>;
fn into_iter(self) -> Self::IntoIter {
RingBufferIntoIterator::new(self)
}
}
impl<'a, T> IntoIterator for &'a AllocRingBuffer<T> {
type Item = &'a T;
type IntoIter = RingBufferIterator<'a, T, AllocRingBuffer<T>>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<'a, T> IntoIterator for &'a mut AllocRingBuffer<T> {
type Item = &'a mut T;
type IntoIter = RingBufferMutIterator<'a, T, AllocRingBuffer<T>>;
fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}
impl<T> Extend<T> for AllocRingBuffer<T> {
fn extend<A: IntoIterator<Item = T>>(&mut self, iter: A) {
let iter = iter.into_iter();
for i in iter {
self.push(i);
}
}
}
unsafe impl<T> RingBuffer<T> for AllocRingBuffer<T> {
#[inline]
unsafe fn ptr_capacity(rb: *const Self) -> usize {
(*rb).capacity
}
#[inline]
unsafe fn ptr_buffer_size(rb: *const Self) -> usize {
(*rb).size
}
impl_ringbuffer!(readptr, writeptr);
#[inline]
fn push(&mut self, value: T) {
if self.is_full() {
let previous_value =
unsafe { ptr::read(get_unchecked_mut(self, mask_and(self.size, self.readptr))) };
unsafe {
drop(previous_value);
}
self.readptr += 1;
}
let index = mask_and(self.size, self.writeptr);
unsafe {
ptr::write(get_unchecked_mut(self, index), value);
}
self.writeptr += 1;
}
fn dequeue(&mut self) -> Option<T> {
if self.is_empty() {
None
} else {
let index = mask_and(self.size, self.readptr);
let res = unsafe { get_unchecked_mut(self, index) };
self.readptr += 1;
unsafe { Some(ptr::read(res)) }
}
}
impl_ringbuffer_ext!(
get_unchecked,
get_unchecked_mut,
readptr,
writeptr,
mask_and
);
#[inline]
fn fill_with<F: FnMut() -> T>(&mut self, mut f: F) {
self.clear();
self.readptr = 0;
self.writeptr = self.capacity;
for i in 0..self.capacity {
unsafe { ptr::write(get_unchecked_mut(self, i), f()) };
}
}
}
impl<T> AllocRingBuffer<T> {
#[inline]
#[must_use]
pub fn with_capacity_power_of_2(cap_power_of_two: usize) -> Self {
Self::new(1 << cap_power_of_two)
}
#[inline]
#[must_use]
#[deprecated = "alias of new"]
pub fn with_capacity(cap: usize) -> Self {
Self::new(cap)
}
#[inline]
#[must_use]
pub fn new(capacity: usize) -> Self {
assert_ne!(capacity, 0, "Capacity must be greater than 0");
let size = capacity.next_power_of_two();
let layout = alloc::alloc::Layout::array::<T>(size).unwrap();
let buf = unsafe { alloc::alloc::alloc(layout) as *mut T };
Self {
buf,
size,
capacity,
readptr: 0,
writeptr: 0,
}
}
}
#[inline]
unsafe fn get_unchecked<'a, T>(rb: *const AllocRingBuffer<T>, index: usize) -> &'a T {
let p = (*rb).buf.add(index);
&*p
}
#[inline]
unsafe fn get_unchecked_mut<T>(rb: *mut AllocRingBuffer<T>, index: usize) -> *mut T {
let p = (*rb).buf.add(index);
p.cast()
}
impl<T> Index<usize> for AllocRingBuffer<T> {
type Output = T;
fn index(&self, index: usize) -> &Self::Output {
self.get(index).expect("index out of bounds")
}
}
impl<T> IndexMut<usize> for AllocRingBuffer<T> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
self.get_mut(index).expect("index out of bounds")
}
}
#[cfg(test)]
mod tests {
use crate::{AllocRingBuffer, RingBuffer};
#[test]
fn test_generic_clone() {
fn helper(a: &AllocRingBuffer<i32>) -> AllocRingBuffer<i32> {
a.clone()
}
_ = helper(&AllocRingBuffer::new(2));
_ = helper(&AllocRingBuffer::new(5));
}
#[test]
fn test_not_power_of_two() {
let mut rb = AllocRingBuffer::new(10);
const NUM_VALS: usize = 1000;
for _ in 0..100 {
for i in 0..NUM_VALS {
rb.enqueue(i);
}
assert!(rb.is_full());
for i in 0..10 {
assert_eq!(Some(i + NUM_VALS - rb.capacity()), rb.dequeue())
}
assert!(rb.is_empty())
}
}
#[test]
fn test_with_capacity_power_of_two() {
let b = AllocRingBuffer::<i32>::with_capacity_power_of_2(2);
assert_eq!(b.capacity, 4);
}
#[test]
#[should_panic]
fn test_index_zero_length() {
let b = AllocRingBuffer::<i32>::new(2);
let _ = b[2];
}
#[test]
fn test_extend() {
let mut buf = AllocRingBuffer::<u8>::new(4);
(0..4).for_each(|_| buf.push(0));
let new_data = [0, 1, 2];
buf.extend(new_data);
let expected = [0, 0, 1, 2];
for i in 0..4 {
let actual = buf[i];
let expected = expected[i];
assert_eq!(actual, expected);
}
}
#[test]
fn test_extend_with_overflow() {
let mut buf = AllocRingBuffer::<u8>::new(8);
(0..8).for_each(|_| buf.push(0));
let new_data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
buf.extend(new_data);
let expected = [2, 3, 4, 5, 6, 7, 8, 9];
for i in 0..8 {
let actual = buf[i];
let expected = expected[i];
assert_eq!(actual, expected);
}
}
#[test]
fn test_conversions() {
let data: &[i32] = &[1, 2, 3, 4];
let buf = AllocRingBuffer::from(data);
assert_eq!(buf.capacity, 4);
assert_eq!(buf.to_vec(), alloc::vec![1, 2, 3, 4]);
let buf = AllocRingBuffer::from(&[1, 2, 3, 4]);
assert_eq!(buf.capacity, 4);
assert_eq!(buf.to_vec(), alloc::vec![1, 2, 3, 4]);
let buf = AllocRingBuffer::from([1, 2, 3, 4]);
assert_eq!(buf.capacity, 4);
assert_eq!(buf.to_vec(), alloc::vec![1, 2, 3, 4]);
}
}