pub struct AllocRingBuffer<T> { /* 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

Creates a AllocRingBuffer with a certain capacity. This capacity is fixed. for this ringbuffer to work, cap must be a power of two and greater than zero.

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)

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

Creates an AllocRingBuffer with a capacity of RINGBUFFER_DEFAULT_CAPACITY.

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Creates a buffer with a capacity of crate::RINGBUFFER_DEFAULT_CAPACITY.

Executes the destructor for this type. Read more
Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Creates a value from an iterator. Read more
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more
Performs the mutable indexing (container[index]) operation. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
Returns the capacity of the buffer.
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. Read more
Returns true if the buffer is entirely empty.
Returns true when the length of the ringbuffer equals the capacity. This happens whenever more elements than capacity have been pushed to the buffer. Read more
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. Read more
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. Read more
Gets a value relative to the start of the array (rarely useful, usually you want Self::get)
Gets a value mutably relative to the start of the array (rarely useful, usually you want Self::get_mut)
Empties the buffer entirely. Sets the length to 0 but keeps the capacity allocated.
Sets every element in the ringbuffer to the value returned by f.
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) Read more
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) Read more
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) Read more
Returns the value at the back of the queue. This is the item that was pushed most recently. Read more
Returns a mutable reference to the value at the back of the queue. This is the item that was pushed most recently. Read more
Creates a mutable iterator over the buffer starting from the item pushed the longest ago, and ending at the element most recently pushed. Read more
Creates an iterator over the buffer starting from the item pushed the longest ago, and ending at the element most recently pushed. Read more
dequeues the top item off the ringbuffer, and moves this item out.
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. Read more
Returns an iterator over the elements in the ringbuffer, dequeueing elements as they are iterated over. Read more
Pushes a value onto the buffer. Cycles around if capacity is reached.
alias for push, forming a more natural counterpart to dequeue

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.