pub struct ConstGenericRingBuffer<T, const CAP: usize> { /* private fields */ }
Expand description

The ConstGenericRingBuffer struct is a RingBuffer implementation which does not require alloc but uses const generics instead.

ConstGenericRingBuffer allocates the ringbuffer on the stack, and the size must be known at compile time through const-generics.

Example

use ringbuffer::{ConstGenericRingBuffer, RingBuffer};

let mut buffer = ConstGenericRingBuffer::<_, 2>::new();

// 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, const CAP: usize> ConstGenericRingBuffer<T, CAP>

source

pub const fn new<const N: usize>() -> Selfwhere ConstGenericRingBuffer<T, CAP>: From<ConstGenericRingBuffer<T, N>>,

Creates a const generic ringbuffer, size is passed as a const generic.

Note that the size does not have to be a power of two, but that not using a power of two might be significantly (up to 3 times) slower.

Trait Implementations§

source§

impl<T: Clone, const CAP: usize> Clone for ConstGenericRingBuffer<T, CAP>

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, const CAP: usize> Debug for ConstGenericRingBuffer<T, CAP>

source§

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

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

impl<T, const CAP: usize> Default for ConstGenericRingBuffer<T, CAP>

source§

fn default() -> Self

Creates a buffer with a capacity specified through the Cap type parameter.

Panics

Panics if CAP is 0

source§

impl<T, const CAP: usize> Drop for ConstGenericRingBuffer<T, CAP>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<T, const CAP: usize> Extend<T> for ConstGenericRingBuffer<T, CAP>

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, const CAP: usize> From<&[T]> for ConstGenericRingBuffer<T, CAP>

source§

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

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

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

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 ConstGenericRingBuffer<T, CAP>

source§

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

Converts to this type from the input type.
source§

impl<const CAP: usize> From<&str> for ConstGenericRingBuffer<char, CAP>

source§

fn from(value: &str) -> Self

Converts to this type from the input type.
source§

impl<T, const CAP: usize> From<[T; CAP]> for ConstGenericRingBuffer<T, CAP>

source§

fn from(value: [T; CAP]) -> 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, 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, const CAP: usize> From<ConstGenericRingBuffer<T, CAP>> for GrowableAllocRingBuffer<T>

source§

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

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

impl<const CAP: usize> From<String> for ConstGenericRingBuffer<char, CAP>

source§

fn from(value: String) -> Self

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

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

source§

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

Converts to this type from the input type.
source§

impl<RB, const CAP: usize> FromIterator<RB> for ConstGenericRingBuffer<RB, CAP>

source§

fn from_iter<T: IntoIterator<Item = RB>>(iter: T) -> Self

Creates a value from an iterator. Read more
source§

impl<T, const CAP: usize> Index<usize> for ConstGenericRingBuffer<T, CAP>

§

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, const CAP: usize> IndexMut<usize> for ConstGenericRingBuffer<T, CAP>

source§

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

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

impl<'a, T, const CAP: usize> IntoIterator for &'a ConstGenericRingBuffer<T, CAP>

§

type Item = &'a T

The type of the elements being iterated over.
§

type IntoIter = RingBufferIterator<'a, T, ConstGenericRingBuffer<T, CAP>>

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, const CAP: usize> IntoIterator for &'a mut ConstGenericRingBuffer<T, CAP>

§

type Item = &'a mut T

The type of the elements being iterated over.
§

type IntoIter = RingBufferMutIterator<'a, T, ConstGenericRingBuffer<T, CAP>>

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, const CAP: usize> IntoIterator for ConstGenericRingBuffer<T, CAP>

§

type Item = T

The type of the elements being iterated over.
§

type IntoIter = RingBufferIntoIterator<T, ConstGenericRingBuffer<T, CAP>>

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, const CAP: usize> PartialEq<ConstGenericRingBuffer<T, CAP>> for ConstGenericRingBuffer<T, CAP>

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, const CAP: usize> RingBuffer<T> for ConstGenericRingBuffer<T, CAP>

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: PartialEq, const CAP: usize> Eq for ConstGenericRingBuffer<T, CAP>

Auto Trait Implementations§

§

impl<T, const CAP: usize> RefUnwindSafe for ConstGenericRingBuffer<T, CAP>where T: RefUnwindSafe,

§

impl<T, const CAP: usize> Send for ConstGenericRingBuffer<T, CAP>where T: Send,

§

impl<T, const CAP: usize> Sync for ConstGenericRingBuffer<T, CAP>where T: Sync,

§

impl<T, const CAP: usize> Unpin for ConstGenericRingBuffer<T, CAP>where T: Unpin,

§

impl<T, const CAP: usize> UnwindSafe for ConstGenericRingBuffer<T, CAP>where T: UnwindSafe,

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.