Struct ringbuffer::AllocRingBuffer
source · pub struct AllocRingBuffer<T, SIZE: RingbufferSize = PowerOfTwo> { /* private fields */ }Expand description
The AllocRingBuffer is a RingBufferExt which is based on a Vec. This means it allocates at runtime
on the heap, and therefore needs the alloc crate. This struct and therefore the dependency on
alloc can be disabled by disabling the alloc (default) feature.
Example
use ringbuffer::{AllocRingBuffer, RingBuffer, RingBufferExt, RingBufferWrite};
let mut buffer = AllocRingBuffer::with_capacity(2);
// First entry of the buffer is now 5.
buffer.push(5);
// The last item we pushed is 5
assert_eq!(buffer.get(-1), Some(&5));
// Second entry is now 42.
buffer.push(42);
assert_eq!(buffer.peek(), Some(&5));
assert!(buffer.is_full());
// Because capacity is reached the next push will be the first item of the buffer.
buffer.push(1);
assert_eq!(buffer.to_vec(), vec![42, 1]);Implementations§
source§impl<T> AllocRingBuffer<T, NonPowerOfTwo>
impl<T> AllocRingBuffer<T, NonPowerOfTwo>
sourcepub fn with_capacity_non_power_of_two(cap: usize) -> Self
pub fn with_capacity_non_power_of_two(cap: usize) -> Self
Creates a AllocRingBuffer with a certain capacity. This capacity is fixed.
for this ringbuffer to work, and must not be zero.
Note, that not using a power of two means some operations can’t be optimized as well. For example, bitwise ands might become modulos.
For example, on push operations, benchmarks have shown that a ringbuffer with a power-of-two
capacity constructed with with_capacity_non_power_of_two (so which don’t get the same optimization
as the ones constructed with with_capacity) can be up to 3x slower
Panics
if the capacity is zero
source§impl<T> AllocRingBuffer<T, PowerOfTwo>
impl<T> AllocRingBuffer<T, PowerOfTwo>
sourcepub fn with_capacity_power_of_2(cap_power_of_two: usize) -> Self
pub fn with_capacity_power_of_2(cap_power_of_two: usize) -> Self
Creates a AllocRingBuffer with a certain capacity. The actual capacity is the input to the
function raised to the power of two (effectively the input is the log2 of the actual capacity)
sourcepub fn with_capacity(cap: usize) -> Self
pub fn with_capacity(cap: usize) -> Self
Creates a AllocRingBuffer with a certain capacity. The capacity must be a power of two.
Panics
Panics when capacity is zero or not a power of two
sourcepub fn new() -> Self
pub fn new() -> Self
Creates an AllocRingBuffer with a capacity of RINGBUFFER_DEFAULT_CAPACITY.
Trait Implementations§
source§impl<T: Clone, SIZE: RingbufferSize> Clone for AllocRingBuffer<T, SIZE>
impl<T: Clone, SIZE: RingbufferSize> Clone for AllocRingBuffer<T, SIZE>
source§impl<T, SIZE: RingbufferSize> Default for AllocRingBuffer<T, SIZE>
impl<T, SIZE: RingbufferSize> Default for AllocRingBuffer<T, SIZE>
source§fn default() -> Self
fn default() -> Self
Creates a buffer with a capacity of RINGBUFFER_DEFAULT_CAPACITY.
source§impl<T, SIZE: RingbufferSize> Drop for AllocRingBuffer<T, SIZE>
impl<T, SIZE: RingbufferSize> Drop for AllocRingBuffer<T, SIZE>
source§impl<T, SIZE: RingbufferSize> Extend<T> for AllocRingBuffer<T, SIZE>
impl<T, SIZE: RingbufferSize> Extend<T> for AllocRingBuffer<T, SIZE>
source§fn extend<A: IntoIterator<Item = T>>(&mut self, iter: A)
fn extend<A: IntoIterator<Item = T>>(&mut self, iter: A)
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)source§impl<T, SIZE: RingbufferSize> From<AllocRingBuffer<T, SIZE>> for GrowableAllocRingBuffer<T>
impl<T, SIZE: RingbufferSize> From<AllocRingBuffer<T, SIZE>> for GrowableAllocRingBuffer<T>
source§fn from(v: AllocRingBuffer<T, SIZE>) -> GrowableAllocRingBuffer<T>
fn from(v: AllocRingBuffer<T, SIZE>) -> GrowableAllocRingBuffer<T>
source§impl<T> From<GrowableAllocRingBuffer<T>> for AllocRingBuffer<T, NonPowerOfTwo>
impl<T> From<GrowableAllocRingBuffer<T>> for AllocRingBuffer<T, NonPowerOfTwo>
source§fn from(v: GrowableAllocRingBuffer<T>) -> AllocRingBuffer<T, NonPowerOfTwo>
fn from(v: GrowableAllocRingBuffer<T>) -> AllocRingBuffer<T, NonPowerOfTwo>
source§impl<RB, SIZE: RingbufferSize> FromIterator<RB> for AllocRingBuffer<RB, SIZE>
impl<RB, SIZE: RingbufferSize> FromIterator<RB> for AllocRingBuffer<RB, SIZE>
source§fn from_iter<T: IntoIterator<Item = RB>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = RB>>(iter: T) -> Self
source§impl<T, SIZE: RingbufferSize> Index<isize> for AllocRingBuffer<T, SIZE>
impl<T, SIZE: RingbufferSize> Index<isize> for AllocRingBuffer<T, SIZE>
source§impl<T, SIZE: RingbufferSize> IndexMut<isize> for AllocRingBuffer<T, SIZE>
impl<T, SIZE: RingbufferSize> IndexMut<isize> for AllocRingBuffer<T, SIZE>
source§impl<T: PartialEq, SIZE: RingbufferSize> PartialEq<AllocRingBuffer<T, SIZE>> for AllocRingBuffer<T, SIZE>
impl<T: PartialEq, SIZE: RingbufferSize> PartialEq<AllocRingBuffer<T, SIZE>> for AllocRingBuffer<T, SIZE>
source§impl<T, SIZE: RingbufferSize> RingBuffer<T> for AllocRingBuffer<T, SIZE>
impl<T, SIZE: RingbufferSize> RingBuffer<T> for AllocRingBuffer<T, SIZE>
source§fn len(&self) -> usize
fn len(&self) -> usize
source§impl<T, SIZE: RingbufferSize> RingBufferExt<T> for AllocRingBuffer<T, SIZE>
impl<T, SIZE: RingbufferSize> RingBufferExt<T> for AllocRingBuffer<T, SIZE>
source§fn get(&self, index: isize) -> Option<&T>
fn get(&self, index: isize) -> Option<&T>
source§fn get_absolute(&self, index: usize) -> Option<&T>
fn get_absolute(&self, index: usize) -> Option<&T>
Self::get)source§fn get_absolute_mut(&mut self, index: usize) -> Option<&mut T>
fn get_absolute_mut(&mut self, index: usize) -> Option<&mut T>
Self::get_mut)source§fn clear(&mut self)
fn clear(&mut self)
source§fn fill_with<F: FnMut() -> T>(&mut self, f: F)
fn fill_with<F: FnMut() -> T>(&mut self, f: F)
source§fn get_mut(&mut self, index: isize) -> Option<&mut T>
fn get_mut(&mut self, index: isize) -> Option<&mut T>
source§fn peek(&self) -> Option<&T>
fn peek(&self) -> Option<&T>
Self::front)