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

A fixed-size circular buffer.

A CircularBuffer may live on the stack. Wrap the CircularBuffer in a Box using CircularBuffer::boxed() if you need the struct to be heap-allocated.

See the module-level documentation for more details and examples.

Implementations§

Returns an empty CircularBuffer.

Examples
use circular_buffer::CircularBuffer;
let buf = CircularBuffer::<16, u32>::new();
assert_eq!(buf, []);

Returns an empty heap-allocated CircularBuffer.

Examples
use circular_buffer::CircularBuffer;
let buf = CircularBuffer::<1024, f64>::boxed();
assert_eq!(buf.len(), 0);

Returns the number of elements in the buffer.

Examples
use circular_buffer::CircularBuffer;

let mut buf = CircularBuffer::<16, u32>::new();
assert_eq!(buf.len(), 0);

buf.push_back(1);
buf.push_back(2);
buf.push_back(3);
assert_eq!(buf.len(), 3);

Returns the capacity of the buffer.

This is the maximum number of elements that the buffer can hold.

This method always returns the generic const parameter N.

Examples
use circular_buffer::CircularBuffer;
let mut buf = CircularBuffer::<16, u32>::new();
assert_eq!(buf.capacity(), 16);

Returns true if the buffer contains 0 elements.

Examples
use circular_buffer::CircularBuffer;

let mut buf = CircularBuffer::<16, u32>::new();
assert!(buf.is_empty());

buf.push_back(1);
assert!(!buf.is_empty());

Returns true if the number of elements in the buffer matches the buffer capacity.

Examples
use circular_buffer::CircularBuffer;

let mut buf = CircularBuffer::<5, u32>::new();
assert!(!buf.is_full());

buf.push_back(1);
assert!(!buf.is_full());

buf.push_back(2);
buf.push_back(3);
buf.push_back(4);
buf.push_back(5);
assert!(buf.is_full());

Returns an iterator over the elements of the buffer.

The iterator advances from front to back. Use .rev() to advance from back to front.

Examples

Iterate from front to back:

use circular_buffer::CircularBuffer;

let buf = CircularBuffer::<5, char>::from_iter("abc".chars());
let mut it = buf.iter();

assert_eq!(it.next(), Some(&'a'));
assert_eq!(it.next(), Some(&'b'));
assert_eq!(it.next(), Some(&'c'));
assert_eq!(it.next(), None);

Iterate from back to front:

use circular_buffer::CircularBuffer;

let buf = CircularBuffer::<5, char>::from_iter("abc".chars());
let mut it = buf.iter().rev();

assert_eq!(it.next(), Some(&'c'));
assert_eq!(it.next(), Some(&'b'));
assert_eq!(it.next(), Some(&'a'));
assert_eq!(it.next(), None);

Returns an iterator over the elements of the buffer that allows modifying each value.

The iterator advances from front to back. Use .rev() to advance from back to front.

Examples
use circular_buffer::CircularBuffer;

let mut buf = CircularBuffer::<5, u32>::from([1, 2, 3]);
for elem in buf.iter_mut() {
    *elem += 5;
}
assert_eq!(buf, [6, 7, 8]);

Returns an iterator over the specified range of elements of the buffer.

The iterator advances from front to back. Use .rev() to advance from back to front.

Panics

If the start of the range is greater than the end, or if the end is greater than the length of the buffer.

Examples

Iterate from front to back:

use circular_buffer::CircularBuffer;

let buf = CircularBuffer::<16, char>::from_iter("abcdefghi".chars());
let mut it = buf.range(3..6);

assert_eq!(it.next(), Some(&'d'));
assert_eq!(it.next(), Some(&'e'));
assert_eq!(it.next(), Some(&'f'));
assert_eq!(it.next(), None);

Iterate from back to front:

use circular_buffer::CircularBuffer;

let buf = CircularBuffer::<16, char>::from_iter("abcdefghi".chars());
let mut it = buf.range(3..6).rev();

assert_eq!(it.next(), Some(&'f'));
assert_eq!(it.next(), Some(&'e'));
assert_eq!(it.next(), Some(&'d'));
assert_eq!(it.next(), None);

Returns an iterator over the specified range of elements of the buffer that allows modifying each value.

The iterator advances from front to back. Use .rev() to advance from back to front.

Panics

If the start of the range is greater than the end, or if the end is greater than the length of the buffer.

Examples

Iterate from front to back:

use circular_buffer::CircularBuffer;

let mut buf = CircularBuffer::<16, i32>::from_iter([1, 2, 3, 4, 5, 6]);
for elem in buf.range_mut(..3) {
    *elem *= -1;
}
assert_eq!(buf, [-1, -2, -3, 4, 5, 6]);

Returns a pair of slices which contain the elements of this buffer.

The second slice may be empty if the internal buffer is contiguous.

Examples
use circular_buffer::CircularBuffer;

let mut buf = CircularBuffer::<4, char>::new();
buf.push_back('a');
buf.push_back('b');
buf.push_back('c');
buf.push_back('d');

// Buffer is contiguous; second slice is empty
assert_eq!(buf.as_slices(), (&['a', 'b', 'c', 'd'][..], &[][..]));

buf.push_back('e');
buf.push_back('f');

// Buffer is disjoint; both slices are non-empty
assert_eq!(buf.as_slices(), (&['c', 'd'][..], &['e', 'f'][..]));

Returns a pair of mutable slices which contain the elements of this buffer.

These slices can be used to modify or replace the elements in the buffer.

The second slice may be empty if the internal buffer is contiguous.

Examples
use circular_buffer::CircularBuffer;

let mut buf = CircularBuffer::<4, char>::new();
buf.push_back('a');
buf.push_back('b');
buf.push_back('c');
buf.push_back('d');
buf.push_back('e');
buf.push_back('f');

assert_eq!(buf, ['c', 'd', 'e', 'f']);

let (left, right) = buf.as_mut_slices();
assert_eq!(left, &mut ['c', 'd'][..]);
assert_eq!(right, &mut ['e', 'f'][..]);

left[0] = 'z';

assert_eq!(buf, ['z', 'd', 'e', 'f']);

Returns a reference to the back element, or None if the buffer is empty.

Examples
use circular_buffer::CircularBuffer;

let mut buf = CircularBuffer::<4, char>::new();
assert_eq!(buf.back(), None);

buf.push_back('a');
buf.push_back('b');
buf.push_back('c');
assert_eq!(buf.back(), Some(&'c'));

Returns a mutable reference to the back element, or None if the buffer is empty.

Examples
use circular_buffer::CircularBuffer;

let mut buf = CircularBuffer::<4, char>::new();
assert_eq!(buf.back_mut(), None);

buf.push_back('a');
buf.push_back('b');
buf.push_back('c');
match buf.back_mut() {
    None => (),
    Some(x) => *x = 'z',
}
assert_eq!(buf, ['a', 'b', 'z']);

Returns a reference to the front element, or None if the buffer is empty.

Examples
use circular_buffer::CircularBuffer;

let mut buf = CircularBuffer::<4, char>::new();
assert_eq!(buf.front(), None);

buf.push_back('a');
buf.push_back('b');
buf.push_back('c');
assert_eq!(buf.front(), Some(&'a'));

Returns a mutable reference to the front element, or None if the buffer is empty.

Examples
use circular_buffer::CircularBuffer;

let mut buf = CircularBuffer::<4, char>::new();
assert_eq!(buf.front_mut(), None);

buf.push_back('a');
buf.push_back('b');
buf.push_back('c');
match buf.front_mut() {
    None => (),
    Some(x) => *x = 'z',
}
assert_eq!(buf, ['z', 'b', 'c']);

Returns a reference to the element at the given index, or None if the element does not exist.

Examples
use circular_buffer::CircularBuffer;

let mut buf = CircularBuffer::<4, char>::new();
assert_eq!(buf.get(1), None);

buf.push_back('a');
buf.push_back('b');
buf.push_back('c');
assert_eq!(buf.get(1), Some(&'b'));

Returns a mutable reference to the element at the given index, or None if the element does not exist.

Examples
use circular_buffer::CircularBuffer;

let mut buf = CircularBuffer::<4, char>::new();
assert_eq!(buf.get_mut(1), None);

buf.push_back('a');
buf.push_back('b');
buf.push_back('c');
match buf.get_mut(1) {
    None => (),
    Some(x) => *x = 'z',
}
assert_eq!(buf, ['a', 'z', 'c']);

Appends an element to the back of the buffer.

If the buffer is full, the element at the front of the buffer is automatically dropped.

Examples
use circular_buffer::CircularBuffer;

let mut buf = CircularBuffer::<3, char>::new();

buf.push_back('a'); assert_eq!(buf, ['a']);
buf.push_back('b'); assert_eq!(buf, ['a', 'b']);
buf.push_back('c'); assert_eq!(buf, ['a', 'b', 'c']);
// The buffer is now full; adding more values causes the front elements to be dropped
buf.push_back('d'); assert_eq!(buf, ['b', 'c', 'd']);
buf.push_back('e'); assert_eq!(buf, ['c', 'd', 'e']);
buf.push_back('f'); assert_eq!(buf, ['d', 'e', 'f']);

Appends an element to the front of the buffer.

If the buffer is full, the element at the back of the buffer is automatically dropped.

Examples
use circular_buffer::CircularBuffer;

let mut buf = CircularBuffer::<3, char>::new();

buf.push_front('a'); assert_eq!(buf, ['a']);
buf.push_front('b'); assert_eq!(buf, ['b', 'a']);
buf.push_front('c'); assert_eq!(buf, ['c', 'b', 'a']);
// The buffer is now full; adding more values causes the back elements to be dropped
buf.push_front('d'); assert_eq!(buf, ['d', 'c', 'b']);
buf.push_front('e'); assert_eq!(buf, ['e', 'd', 'c']);
buf.push_front('f'); assert_eq!(buf, ['f', 'e', 'd']);

Removes and returns an element from the back of the buffer.

If the buffer is empty, None is returned.

Examples
use circular_buffer::CircularBuffer;

let mut buf = CircularBuffer::<3, char>::from(['a', 'b', 'c']);

assert_eq!(buf.pop_back(), Some('c'));
assert_eq!(buf.pop_back(), Some('b'));
assert_eq!(buf.pop_back(), Some('a'));
assert_eq!(buf.pop_back(), None);

Removes and returns an element from the front of the buffer.

If the buffer is empty, None is returned.

Examples
use circular_buffer::CircularBuffer;

let mut buf = CircularBuffer::<3, char>::from(['a', 'b', 'c']);

assert_eq!(buf.pop_front(), Some('a'));
assert_eq!(buf.pop_front(), Some('b'));
assert_eq!(buf.pop_front(), Some('c'));
assert_eq!(buf.pop_front(), None);

Removes and returns an element at the specified index.

If the index is out of bounds, None is returned.

Examples
use circular_buffer::CircularBuffer;

let mut buf = CircularBuffer::<3, char>::from(['a', 'b', 'c']);

assert_eq!(buf.remove(1), Some('b'));
assert_eq!(buf, ['a', 'c']);

assert_eq!(buf.remove(5), None);

Swap the element at index i with the element at index j.

Panics

If either i or j is out of bounds.

Examples
use circular_buffer::CircularBuffer;

let mut buf = CircularBuffer::<5, char>::from(['a', 'b', 'c', 'd']);
assert_eq!(buf, ['a', 'b', 'c', 'd']);

buf.swap(0, 3);
assert_eq!(buf, ['d', 'b', 'c', 'a']);

Trying to swap an invalid index panics:

use circular_buffer::CircularBuffer;
let mut buf = CircularBuffer::<5, char>::from(['a', 'b', 'c', 'd']);
buf.swap(0, 7);

Removes the element at index and returns it, replacing it with the back of the buffer.

Returns None if index is out-of-bounds.

Examples
use circular_buffer::CircularBuffer;

let mut buf = CircularBuffer::<5, char>::from(['a', 'b', 'c', 'd']);
assert_eq!(buf, ['a', 'b', 'c', 'd']);

assert_eq!(buf.swap_remove_back(2), Some('c'));
assert_eq!(buf, ['a', 'b', 'd']);

assert_eq!(buf.swap_remove_back(7), None);

Removes the element at index and returns it, replacing it with the front of the buffer.

Returns None if index is out-of-bounds.

Examples
use circular_buffer::CircularBuffer;

let mut buf = CircularBuffer::<5, char>::from(['a', 'b', 'c', 'd']);
assert_eq!(buf, ['a', 'b', 'c', 'd']);

assert_eq!(buf.swap_remove_front(2), Some('c'));
assert_eq!(buf, ['b', 'a', 'd']);

assert_eq!(buf.swap_remove_front(7), None);

Shortens the buffer, keeping only the front len elements and dropping the rest.

If len is equal or greater to the buffer’s current length, this has no effect.

Calling truncate_back(0) is equivalent to clear().

Examples
use circular_buffer::CircularBuffer;

let mut buf = CircularBuffer::<4, u32>::from([10, 20, 30]);

buf.truncate_back(1);
assert_eq!(buf, [10]);

// Truncating to a length that is greater than the buffer's length has no effect
buf.truncate_back(8);
assert_eq!(buf, [10]);

Shortens the buffer, keeping only the back len elements and dropping the rest.

If len is equal or greater to the buffer’s current length, this has no effect.

Calling truncate_front(0) is equivalent to clear().

Examples
use circular_buffer::CircularBuffer;

let mut buf = CircularBuffer::<4, u32>::from([10, 20, 30]);

buf.truncate_front(1);
assert_eq!(buf, [30]);

// Truncating to a length that is greater than the buffer's length has no effect
buf.truncate_front(8);
assert_eq!(buf, [30]);

Drops all the elements in the buffer.

Examples
use circular_buffer::CircularBuffer;

let mut buf = CircularBuffer::<4, u32>::from([10, 20, 30]);
assert_eq!(buf, [10, 20, 30]);
buf.clear();
assert_eq!(buf, []);

Clones and appends all the elements from the slice to the back of the buffer.

This is an optimized version of extend() for slices.

If slice contains more values than the available capacity, the elements at the front of the buffer are dropped.

Examples
use circular_buffer::CircularBuffer;

let mut buf: CircularBuffer<5, u32> = CircularBuffer::from([1, 2, 3]);
buf.extend_from_slice(&[4, 5, 6, 7]);
assert_eq!(buf, [3, 4, 5, 6, 7]);

Clones the elements of the buffer into a new Vec, leaving the buffer unchanged.

Examples
use circular_buffer::CircularBuffer;

let buf: CircularBuffer<5, u32> = CircularBuffer::from([1, 2, 3]);
let vec: Vec<u32> = buf.to_vec();

assert_eq!(buf, [1, 2, 3]);
assert_eq!(vec, [1, 2, 3]);

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
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.
Creates a value from an iterator. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. 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 ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
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
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 cursor. 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
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.