pub struct RingBuffer<A, const N: usize> { /* private fields */ }
Expand description

A fixed capacity ring buffer.

A ring buffer is an array where the first logical index is at some arbitrary location inside the array, and the indices wrap around to the start of the array once they overflow its bounds.

This gives us the ability to push to either the front or the end of the array in constant time, at the cost of losing the ability to get a single contiguous slice reference to the contents.

It differs from the Chunk in that the latter will have mostly constant time pushes, but may occasionally need to shift its contents around to make room. They both have constant time pop, and they both have linear time insert and remove.

The RingBuffer offers its own Slice and SliceMut types to compensate for the loss of being able to take a slice, but they’re somewhat less efficient, so the general rule should be that you shouldn’t choose a RingBuffer if you rely heavily on slices - but if you don’t, it’s probably a marginally better choice overall than Chunk.

Feature Flag

To use this data structure, you need to enable the ringbuffer feature.

Implementations

The capacity of this ring buffer, as a usize.

Construct an empty ring buffer.

Construct a ring buffer with a single item.

Construct a ring buffer with two items.

Construct a new ring buffer and move every item from other into the new buffer.

Time: O(n)

Construct a new ring buffer and populate it by taking count items from the iterator iter.

Panics if the iterator contains less than count items.

Time: O(n)

Construct a new ring buffer and populate it by taking count items from the front of other.

Time: O(n) for the number of items moved

Construct a new ring buffer and populate it by taking count items from the back of other.

Time: O(n) for the number of items moved

Test if the ring buffer is full.

Get an iterator over references to the items in the ring buffer in order.

Get an iterator over mutable references to the items in the ring buffer in order.

Get a Slice for a subset of the ring buffer.

Get a SliceMut for a subset of the ring buffer.

Get an unchecked reference to the value at the given index.

Safety

You must ensure the index is not out of bounds.

Get an unchecked mutable reference to the value at the given index.

Safety

You must ensure the index is not out of bounds.

Push a value to the back of the buffer.

Panics if the capacity of the buffer is exceeded.

Time: O(1)

Push a value to the front of the buffer.

Panics if the capacity of the buffer is exceeded.

Time: O(1)

Pop a value from the back of the buffer.

Returns None if the buffer is empty.

Time: O(1)

Pop a value from the front of the buffer.

Returns None if the buffer is empty.

Time: O(1)

Discard all items up to but not including index.

Panics if index is out of bounds.

Time: O(n) for the number of items dropped

Discard all items from index onward.

Panics if index is out of bounds.

Time: O(n) for the number of items dropped

Split a buffer into two, the original buffer containing everything up to index and the returned buffer containing everything from index onwards.

Panics if index is out of bounds.

Time: O(n) for the number of items in the new buffer

Remove all items from other and append them to the back of self.

Panics if the capacity of self is exceeded.

other will be an empty buffer after this operation.

Time: O(n) for the number of items moved

Remove count items from the front of other and append them to the back of self.

Panics if self doesn’t have count items left, or if other has fewer than count items.

Time: O(n) for the number of items moved

Remove count items from the back of other and append them to the front of self.

Panics if self doesn’t have count items left, or if other has fewer than count items.

Time: O(n) for the number of items moved

Insert a new value at index index, shifting all the following values to the right.

Panics if the index is out of bounds.

Time: O(n) for the number of items shifted

Insert a new value into the buffer in sorted order.

This assumes every element of the buffer is already in sorted order. If not, the value will still be inserted but the ordering is not guaranteed.

Time: O(log n) to find the insert position, then O(n) for the number of elements shifted.

Insert multiple values at index index, shifting all the following values to the right.

Panics if the index is out of bounds or the chunk doesn’t have room for all the values.

Time: O(m+n) where m is the number of elements inserted and n is the number of elements following the insertion index. Calling insert repeatedly would be O(m*n).

Remove the value at index index, shifting all the following values to the left.

Returns the removed value.

Panics if the index is out of bounds.

Time: O(n) for the number of items shifted

Construct an iterator that drains values from the front of the buffer.

Discard the contents of the buffer.

Time: O(n)

Trait Implementations

Generate an arbitrary value of Self from the given unstructured data. Read more

Generate an arbitrary value of Self from the entirety of the given unstructured data. Read more

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more

Get a reference to the value at a given index.

Get a reference to the first element in the array.

Get a reference to the last element in the array.

Return true if an element equivalent to target exists in the array.

Perform a binary search for target.

Perform a binary search using a comparator function.

Perform a binary search using a key and a key extractor function.

Test whether the array is sorted.

Test whether the array is sorted using a comparator function.

Test whether the array is sorted using a key extractor function.

Test whether the array starts with the elements in slice.

Test whether the array ends with the elements in slice.

Get a mutable reference to the value at a given index.

Get a mutable reference to the first element in the array.

Get a mutable reference to the last element in the array.

Set the value of the element at the given index. Read more

Swap the elements at two indexes.

Get mutable references to the elements at two indexes and call a function on them. Read more

Sort the elements of the array.

Sort the elements of the array using a comparator function.

Sort the elements of the array using a key extractor function.

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

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

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

Converts to this type from the input type.

Converts to this type from the input type.

Creates a value from an iterator. Read more

Get the length of the ring buffer.

Return whether the data structure is empty.

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

The returned type after indexing.

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

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

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Clone an instance of Self into an uninitialised instance of Self. Read more

Initialise an instance of Self to its default state. Read more

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more

Like read, except that it reads into a slice of buffers. Read more

🔬 This is a nightly-only experimental API. (can_vector)

Determines if this Reader has an efficient read_vectored implementation. Read more

Read all bytes until EOF in this source, placing them into buf. Read more

Read all bytes until EOF in this source, appending them to buf. Read more

Read the exact number of bytes required to fill buf. Read more

🔬 This is a nightly-only experimental API. (read_buf)

Pull some bytes from this source into the specified buffer. Read more

🔬 This is a nightly-only experimental API. (read_buf)

Read the exact number of bytes required to fill buf. Read more

Creates a “by reference” adaptor for this instance of Read. Read more

Transforms this Read instance to an Iterator over its bytes. Read more

Creates an adapter which will chain this stream with another. Read more

Creates an adapter which will read at most limit bytes from it. Read more

Write a buffer into this writer, returning how many bytes were written. Read more

Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more

Like write, except that it writes from a slice of buffers. Read more

🔬 This is a nightly-only experimental API. (can_vector)

Determines if this Writer has an efficient write_vectored implementation. Read more

Attempts to write an entire buffer into this writer. Read more

🔬 This is a nightly-only experimental API. (write_all_vectored)

Attempts to write multiple buffers into this writer. Read more

Writes a formatted string into this writer, returning any error encountered. Read more

Creates a “by reference” adapter for this instance of Write. Read more

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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.