pub struct AllocRingBuffer<T> { /* private fields */ }
Expand description
The AllocRingBuffer
is a RingBuffer
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};
let mut buffer = AllocRingBuffer::new(2);
// First entry of the buffer is now 5.
buffer.push(5);
// The last item we pushed is 5
assert_eq!(buffer.back(), 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>
impl<T> AllocRingBuffer<T>
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
👎Deprecated: alias of new
pub fn with_capacity(cap: usize) -> Self
Alias of with_capacity
.
Trait Implementations§
Source§impl<T: Clone> Clone for AllocRingBuffer<T>
impl<T: Clone> Clone for AllocRingBuffer<T>
Source§impl<T: Debug> Debug for AllocRingBuffer<T>
impl<T: Debug> Debug for AllocRingBuffer<T>
Source§impl<T> Drop for AllocRingBuffer<T>
impl<T> Drop for AllocRingBuffer<T>
Source§impl<T> Extend<T> for AllocRingBuffer<T>
impl<T> Extend<T> for AllocRingBuffer<T>
Source§fn extend<A: IntoIterator<Item = T>>(&mut self, iter: A)
fn extend<A: IntoIterator<Item = T>>(&mut self, iter: A)
Extends a collection with the contents of an iterator. Read more
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
🔬This is a nightly-only experimental API. (
extend_one
)Extends a collection with exactly one element.
Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
🔬This is a nightly-only experimental API. (
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
Source§impl<T, const CAP: usize> From<AllocRingBuffer<T>> for ConstGenericRingBuffer<T, CAP>
impl<T, const CAP: usize> From<AllocRingBuffer<T>> for ConstGenericRingBuffer<T, CAP>
Source§fn from(value: AllocRingBuffer<T>) -> Self
fn from(value: AllocRingBuffer<T>) -> Self
Converts to this type from the input type.
Source§impl<T> From<AllocRingBuffer<T>> for GrowableAllocRingBuffer<T>
impl<T> From<AllocRingBuffer<T>> for GrowableAllocRingBuffer<T>
Source§fn from(v: AllocRingBuffer<T>) -> GrowableAllocRingBuffer<T>
fn from(v: AllocRingBuffer<T>) -> GrowableAllocRingBuffer<T>
Converts to this type from the input type.
Source§impl<T, const CAP: usize> From<ConstGenericRingBuffer<T, CAP>> for AllocRingBuffer<T>
impl<T, const CAP: usize> From<ConstGenericRingBuffer<T, CAP>> for AllocRingBuffer<T>
Source§fn from(value: ConstGenericRingBuffer<T, CAP>) -> Self
fn from(value: ConstGenericRingBuffer<T, CAP>) -> Self
Converts to this type from the input type.
Source§impl<T> From<GrowableAllocRingBuffer<T>> for AllocRingBuffer<T>
impl<T> From<GrowableAllocRingBuffer<T>> for AllocRingBuffer<T>
Source§fn from(v: GrowableAllocRingBuffer<T>) -> AllocRingBuffer<T>
fn from(v: GrowableAllocRingBuffer<T>) -> AllocRingBuffer<T>
Converts to this type from the input type.
Source§impl<T> From<LinkedList<T>> for AllocRingBuffer<T>
impl<T> From<LinkedList<T>> for AllocRingBuffer<T>
Source§fn from(value: LinkedList<T>) -> Self
fn from(value: LinkedList<T>) -> Self
Converts to this type from the input type.
Source§impl<T> From<Vec<T>> for AllocRingBuffer<T>
impl<T> From<Vec<T>> for AllocRingBuffer<T>
Source§impl<T> From<VecDeque<T>> for AllocRingBuffer<T>
impl<T> From<VecDeque<T>> for AllocRingBuffer<T>
Source§impl<T> Index<usize> for AllocRingBuffer<T>
impl<T> Index<usize> for AllocRingBuffer<T>
Source§impl<T> IndexMut<usize> for AllocRingBuffer<T>
impl<T> IndexMut<usize> for AllocRingBuffer<T>
Source§impl<'a, T> IntoIterator for &'a AllocRingBuffer<T>
impl<'a, T> IntoIterator for &'a AllocRingBuffer<T>
Source§impl<'a, T> IntoIterator for &'a mut AllocRingBuffer<T>
impl<'a, T> IntoIterator for &'a mut AllocRingBuffer<T>
Source§impl<T> IntoIterator for AllocRingBuffer<T>
impl<T> IntoIterator for AllocRingBuffer<T>
Source§impl<T: PartialEq> PartialEq for AllocRingBuffer<T>
impl<T: PartialEq> PartialEq for AllocRingBuffer<T>
Source§impl<T> RingBuffer<T> for AllocRingBuffer<T>
impl<T> RingBuffer<T> for AllocRingBuffer<T>
Source§fn push(&mut self, value: T)
fn push(&mut self, value: T)
Pushes a value onto the buffer. Cycles around if capacity is reached.
Source§fn dequeue(&mut self) -> Option<T>
fn dequeue(&mut self) -> Option<T>
dequeues the top item off the ringbuffer, and moves this item out.
Source§fn get_signed(&self, index: isize) -> Option<&T>
fn get_signed(&self, index: isize) -> Option<&T>
Gets a value relative to the current index. 0 is the next index to be written to with push.
-1 and down are the last elements pushed and 0 and up are the items that were pushed the longest ago.
Source§fn get(&self, index: usize) -> Option<&T>
fn get(&self, index: usize) -> Option<&T>
Gets a value relative to the current index. 0 is the next index to be written to with push.
Source§fn clear(&mut self)
fn clear(&mut self)
Empties the buffer entirely. Sets the length to 0 but keeps the capacity allocated.
Source§fn fill_with<F: FnMut() -> T>(&mut self, f: F)
fn fill_with<F: FnMut() -> T>(&mut self, f: F)
Sets every element in the ringbuffer to the value returned by f.
Source§fn len(&self) -> usize
fn len(&self) -> usize
Returns the length of the internal buffer.
This length grows up to the capacity and then stops growing.
This is because when the length is reached, new items are appended at the start.
Source§fn is_full(&self) -> bool
fn is_full(&self) -> bool
Returns true when the length of the ringbuffer equals the capacity. This happens whenever
more elements than capacity have been pushed to the buffer.
Source§fn buffer_size(&self) -> usize
fn buffer_size(&self) -> usize
Returns the number of elements allocated for this ringbuffer (can be larger than capacity).
Source§fn skip(&mut self)
fn skip(&mut self)
dequeues the top item off the queue, but does not return it. Instead it is dropped.
If the ringbuffer is empty, this function is a nop.
Source§fn drain(&mut self) -> RingBufferDrainingIterator<'_, T, Self>
fn drain(&mut self) -> RingBufferDrainingIterator<'_, T, Self>
Returns an iterator over the elements in the ringbuffer,
dequeueing elements as they are iterated over. Read more
Source§fn fill_default(&mut self)where
T: Default,
fn fill_default(&mut self)where
T: Default,
Sets every element in the ringbuffer to it’s default value
Source§fn get_mut_signed(&mut self, index: isize) -> Option<&mut T>
fn get_mut_signed(&mut self, index: isize) -> Option<&mut T>
Gets a value relative to the current index mutably. 0 is the next index to be written to with push.
-1 and down are the last elements pushed and 0 and up are the items that were pushed the longest ago.
Source§fn get_mut(&mut self, index: usize) -> Option<&mut T>
fn get_mut(&mut self, index: usize) -> Option<&mut T>
Gets a value relative to the current index mutably. 0 is the next index to be written to with push.
Source§fn peek(&self) -> Option<&T>
fn peek(&self) -> Option<&T>
Returns the value at the current index.
This is the value that will be overwritten by the next push and also the value pushed
the longest ago. (alias of
Self::front
)Source§fn front(&self) -> Option<&T>
fn front(&self) -> Option<&T>
Returns the value at the front of the queue.
This is the value that will be overwritten by the next push and also the value pushed
the longest ago.
(alias of peek)
Source§fn front_mut(&mut self) -> Option<&mut T>
fn front_mut(&mut self) -> Option<&mut T>
Returns a mutable reference to the value at the back of the queue.
This is the value that will be overwritten by the next push.
(alias of peek)
Source§fn back(&self) -> Option<&T>
fn back(&self) -> Option<&T>
Returns the value at the back of the queue.
This is the item that was pushed most recently.
Source§fn back_mut(&mut self) -> Option<&mut T>
fn back_mut(&mut self) -> Option<&mut T>
Returns a mutable reference to the value at the back of the queue.
This is the item that was pushed most recently.
Source§fn iter_mut(&mut self) -> RingBufferMutIterator<'_, T, Self>
fn iter_mut(&mut self) -> RingBufferMutIterator<'_, T, Self>
Creates a mutable iterator over the buffer starting from the item pushed the longest ago,
and ending at the element most recently pushed.
Source§fn iter(&self) -> RingBufferIterator<'_, T, Self>
fn iter(&self) -> RingBufferIterator<'_, T, Self>
Creates an iterator over the buffer starting from the item pushed the longest ago,
and ending at the element most recently pushed.
impl<T: Eq + PartialEq> Eq for AllocRingBuffer<T>
impl<T: Send> Send for AllocRingBuffer<T>
impl<T: Sync> Sync for AllocRingBuffer<T>
Auto Trait Implementations§
impl<T> Freeze for AllocRingBuffer<T>
impl<T> RefUnwindSafe for AllocRingBuffer<T>where
T: RefUnwindSafe,
impl<T> Unpin for AllocRingBuffer<T>
impl<T> UnwindSafe for AllocRingBuffer<T>where
T: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more