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>

source

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)

source

pub fn with_capacity(cap: usize) -> Self

👎Deprecated: alias of new

Alias of with_capacity.

source

pub fn new(capacity: usize) -> Self

Creates a AllocRingBuffer with a certain capacity. The capacity must not be zero.

Panics

Panics when capacity is zero

Trait Implementations§

source§

impl<T: Clone> Clone for AllocRingBuffer<T>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for AllocRingBuffer<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T> Drop for AllocRingBuffer<T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<T> Extend<T> for AllocRingBuffer<T>

source§

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)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

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: Clone> From<&[T]> for AllocRingBuffer<T>

source§

fn from(value: &[T]) -> Self

Converts to this type from the input type.
source§

impl<T: Clone, const N: usize> From<&[T; N]> for AllocRingBuffer<T>

source§

fn from(value: &[T; N]) -> Self

Converts to this type from the input type.
source§

impl<T: Clone> From<&mut [T]> for AllocRingBuffer<T>

source§

fn from(value: &mut [T]) -> Self

Converts to this type from the input type.
source§

impl<T: Clone, const CAP: usize> From<&mut [T; CAP]> for AllocRingBuffer<T>

source§

fn from(value: &mut [T; CAP]) -> Self

Converts to this type from the input type.
source§

impl From<&str> for AllocRingBuffer<char>

source§

fn from(value: &str) -> Self

Converts to this type from the input type.
source§

impl<T, const N: usize> From<[T; N]> for AllocRingBuffer<T>

source§

fn from(value: [T; N]) -> Self

Converts to this type from the input type.
source§

impl<T, const CAP: usize> From<AllocRingBuffer<T>> for ConstGenericRingBuffer<T, CAP>

source§

fn from(value: AllocRingBuffer<T>) -> Self

Converts to this type from the input type.
source§

impl<T> From<AllocRingBuffer<T>> for GrowableAllocRingBuffer<T>

source§

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>

source§

fn from(value: ConstGenericRingBuffer<T, CAP>) -> Self

Converts to this type from the input type.
source§

impl<T> From<GrowableAllocRingBuffer<T>> for AllocRingBuffer<T>

source§

fn from(v: GrowableAllocRingBuffer<T>) -> AllocRingBuffer<T>

Converts to this type from the input type.
source§

impl<T> From<LinkedList<T, Global>> for AllocRingBuffer<T>

source§

fn from(value: LinkedList<T>) -> Self

Converts to this type from the input type.
source§

impl From<String> for AllocRingBuffer<char>

source§

fn from(value: String) -> Self

Converts to this type from the input type.
source§

impl<T> From<Vec<T, Global>> for AllocRingBuffer<T>

source§

fn from(value: Vec<T>) -> Self

Converts to this type from the input type.
source§

impl<T> From<VecDeque<T, Global>> for AllocRingBuffer<T>

source§

fn from(value: VecDeque<T>) -> Self

Converts to this type from the input type.
source§

impl<T> Index<usize> for AllocRingBuffer<T>

§

type Output = T

The returned type after indexing.
source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<T> IndexMut<usize> for AllocRingBuffer<T>

source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
source§

impl<'a, T> IntoIterator for &'a AllocRingBuffer<T>

§

type Item = &'a T

The type of the elements being iterated over.
§

type IntoIter = RingBufferIterator<'a, T, AllocRingBuffer<T>>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<'a, T> IntoIterator for &'a mut AllocRingBuffer<T>

§

type Item = &'a mut T

The type of the elements being iterated over.
§

type IntoIter = RingBufferMutIterator<'a, T, AllocRingBuffer<T>>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T> IntoIterator for AllocRingBuffer<T>

§

type Item = T

The type of the elements being iterated over.
§

type IntoIter = RingBufferIntoIterator<T, AllocRingBuffer<T>>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T: PartialEq> PartialEq<AllocRingBuffer<T>> for AllocRingBuffer<T>

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T> RingBuffer<T> for AllocRingBuffer<T>

source§

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>

dequeues the top item off the ringbuffer, and moves this item out.
source§

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>

Gets a value relative to the current index. 0 is the next index to be written to with push.
source§

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)

Sets every element in the ringbuffer to the value returned by f.
source§

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_empty(&self) -> bool

Returns true if the buffer is entirely empty.
source§

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 capacity(&self) -> usize

Returns the capacity of the buffer.
source§

fn buffer_size(&self) -> usize

Returns the number of elements allocated for this ringbuffer (can be larger than capacity).
source§

fn enqueue(&mut self, value: T)

alias for push, forming a more natural counterpart to dequeue
source§

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>

Returns an iterator over the elements in the ringbuffer, dequeueing elements as they are iterated over. Read more
source§

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>

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>

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>

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>

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>

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>

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>

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>

Creates an iterator over the buffer starting from the item pushed the longest ago, and ending at the element most recently pushed.
source§

impl<T: Eq + PartialEq> Eq for AllocRingBuffer<T>

source§

impl<T: Send> Send for AllocRingBuffer<T>

source§

impl<T: Sync> Sync for AllocRingBuffer<T>

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.