pub struct RingBuffer<T> { /* private fields */ }Expand description
A queue that also allows direct index addressing.
This queue trades some features from VecDeque in the std library for the feature of direct indexing.
Instead of returning (), push_back returns the index of the new element in the buffer. Methods
like get_absolute can then be used for element access.
The index returned by push_back or used by get_absolute may not be the actual index but a
higher number that gets wrapped around internally so the indices are still valid after elements get
moved around by expanding or shrinking the container.
The features not present in RingBuffer whereas present in VecDeque include:
- Double ended functionality, including
push_frontandpop_back appenddraininsertremove- basically anything that messes with the position of elements in the buffer apart from
swap
Implementations§
Source§impl<T> RingBuffer<T>
impl<T> RingBuffer<T>
Sourcepub fn new() -> RingBuffer<T>
pub fn new() -> RingBuffer<T>
Sourcepub fn with_capacity(capacity: usize) -> RingBuffer<T>
pub fn with_capacity(capacity: usize) -> RingBuffer<T>
Creates an empty RingBuffer with space for at least capacity elements.
§Example
let buffer: RingBuffer<usize> = RingBuffer::with_capacity(10);
assert!(buffer.capacity() >= 10);Sourcepub fn as_slices(&self) -> (&[T], &[T])
pub fn as_slices(&self) -> (&[T], &[T])
Returns a pair of slices which contain, in order, the contents of the
RingBuffer.
§Example
let mut buffer = RingBuffer::new();
buffer.push_back(41);
buffer.push_back(42);
buffer.push_back(1);
buffer.push_back(2);
assert_eq!(buffer.len(), 4);
assert_eq!(buffer.capacity(), 4);
assert_eq!(buffer.front(), Some(&41));
assert_eq!(buffer.as_slices(), (&[41, 42, 1, 2][..], &[][..]));
buffer.pop_front();
buffer.pop_front();
buffer.push_back(3);
buffer.push_back(4);
assert_eq!(buffer.as_slices(), (&[1, 2][..], &[3, 4][..]));Sourcepub fn back(&self) -> Option<&T>
pub fn back(&self) -> Option<&T>
Provides a reference to the back element of the queue, or None if the RingBuffer is
empty.
§Example
let mut buffer = RingBuffer::new();
assert_eq!(buffer.back(), None);
buffer.push_back(1);
buffer.push_back(2);
assert_eq!(buffer.back(), Some(&2));Sourcepub fn back_mut(&mut self) -> Option<&mut T>
pub fn back_mut(&mut self) -> Option<&mut T>
Provides a mutable reference to the back element of the queue, or None if the
RingBuffer is empty.
§Example
let mut buffer = RingBuffer::new();
assert_eq!(buffer.back(), None);
buffer.push_back(1);
buffer.push_back(2);
if let Some(x) = buffer.back_mut() {
*x = 42;
}
assert_eq!(buffer.back(), Some(&42));Sourcepub fn back_absolute_index(&self) -> Option<RingBufferIndex>
pub fn back_absolute_index(&self) -> Option<RingBufferIndex>
Returns the absolute index of the back element of the queue, or None if the
RingBuffer is empty.
§Example
let mut buffer = RingBuffer::new();
assert!(buffer.back_absolute_index().is_none());
buffer.push_back(1);
buffer.push_back(2);
let back_index = buffer.back_absolute_index();
assert!(back_index.is_some());
assert_eq!(buffer.get_absolute(back_index.unwrap()), buffer.back());Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements the RingBuffer can hold without reallocating.
For fast wrapping functionality, capacity is always a power of two.
§Example
let buffer: RingBuffer<usize> = RingBuffer::with_capacity(10);
assert!(buffer.capacity() >= 16);Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the RingBuffer, removing all values.
§Example
let mut buffer = RingBuffer::new();
buffer.push_back(1);
buffer.clear();
assert!(buffer.is_empty());Sourcepub fn contains(&self, x: &T) -> boolwhere
T: PartialEq<T>,
pub fn contains(&self, x: &T) -> boolwhere
T: PartialEq<T>,
Returns true if the RingBuffer contains an element equal to the
given value.
§Example
let mut buffer = RingBuffer::new();
buffer.push_back(0);
buffer.push_back(1);
assert_eq!(buffer.contains(&1), true);
assert_eq!(buffer.contains(&42), false);Sourcepub fn front(&self) -> Option<&T>
pub fn front(&self) -> Option<&T>
Provides a reference to the front element of the queue, or None if the RingBuffer is
empty.
§Example
let mut buffer = RingBuffer::new();
assert_eq!(buffer.front(), None);
buffer.push_back(1);
buffer.push_back(2);
assert_eq!(buffer.front(), Some(&1));Sourcepub fn front_mut(&mut self) -> Option<&mut T>
pub fn front_mut(&mut self) -> Option<&mut T>
Provides a mutable reference to the front element of the queue, or None if the
RingBuffer is empty.
§Example
let mut buffer = RingBuffer::new();
assert_eq!(buffer.front_mut(), None);
buffer.push_back(1);
buffer.push_back(2);
if let Some(x) = buffer.front_mut() {
*x = 42;
}
assert_eq!(buffer.front(), Some(&42));Sourcepub fn front_absolute_index(&self) -> Option<RingBufferIndex>
pub fn front_absolute_index(&self) -> Option<RingBufferIndex>
Returns the absolute index of the front element of the queue, or None if the
RingBuffer is empty.
§Example
let mut buffer = RingBuffer::new();
assert!(buffer.front_absolute_index().is_none());
buffer.push_back(1);
buffer.push_back(2);
let front_index = buffer.front_absolute_index();
assert!(front_index.is_some());
assert_eq!(buffer.get_absolute(front_index.unwrap()), buffer.front());Sourcepub fn get_absolute(&self, index: RingBufferIndex) -> Option<&T>
pub fn get_absolute(&self, index: RingBufferIndex) -> Option<&T>
Retrieves an element in the RingBuffer by absolute index.
The index provided is the index used by the internal container.
This is designed to be used in conjunction with push_back.
This also gets wrapped around, supporting expanding and shrinking the container without
breaking the access with absolute addresses.
§Example
let mut buffer = RingBuffer::new();
let first = buffer.push_back(1);
buffer.push_back(2);
let second = buffer.push_back(42);
assert_eq!(buffer.get_absolute(first), Some(&1));
assert_eq!(buffer.get_absolute(second), Some(&42));Sourcepub fn get_absolute_mut(&mut self, index: RingBufferIndex) -> Option<&mut T>
pub fn get_absolute_mut(&mut self, index: RingBufferIndex) -> Option<&mut T>
Retrieves a mutable element in the RingBuffer by absolute index.
The index provided is the index used by the internal container.
This is designed to be used in conjunction with push_back.
This also gets wrapped around, supporting expanding and shrinking the container without
breaking the access with absolute addresses.
§Example
let mut buffer = RingBuffer::new();
buffer.push_back(1);
let the_answer = buffer.push_back(2);
buffer.push_back(3);
assert_eq!(buffer.get_absolute(the_answer), Some(&2));
if let Some(x) = buffer.get_absolute_mut(the_answer) {
*x = 42;
}
assert_eq!(buffer.get_absolute(the_answer), Some(&42));Sourcepub unsafe fn get_absolute_unchecked(&self, index: RingBufferIndex) -> &T
pub unsafe fn get_absolute_unchecked(&self, index: RingBufferIndex) -> &T
Retrieves an element in the RingBuffer by absolute index without checking for validity.
This does the same as get_absolute, but skips any checks. Use this if you really need
the performance and can guarantee the accessed element is valid.
§Safety
The caller is responsible for maintaining Rusts ownership guarantees. The validity of a buffer entry also isn’t checked, so while being valid memory, the reference may not point to an object in valid state.
§Example
let mut buffer = RingBuffer::new();
let first = buffer.push_back(1);
buffer.push_back(2);
let second = buffer.push_back(42);
unsafe {
assert_eq!(buffer.get_absolute_unchecked(first), &1);
assert_eq!(buffer.get_absolute_unchecked(second), &42);
}Sourcepub unsafe fn get_absolute_mut_unchecked(
&mut self,
index: RingBufferIndex,
) -> &mut T
pub unsafe fn get_absolute_mut_unchecked( &mut self, index: RingBufferIndex, ) -> &mut T
Retrieves a mutable element in the RingBuffer by absolute index without checking for
validity.
This does the same as get_absolute_mut, but skips any checks. Use this if you really need
the performance and can guarantee the accessed element is valid.
§Safety
The caller is responsible for maintaining Rusts ownership guarantees. The validity of a buffer entry also isn’t checked, so while being valid memory, the reference may not point to an object in valid state.
§Example
let mut buffer = RingBuffer::new();
buffer.push_back(1);
let the_answer = buffer.push_back(2);
buffer.push_back(3);
assert_eq!(buffer.get_absolute(the_answer), Some(&2));
unsafe {
*buffer.get_absolute_mut_unchecked(the_answer) = 42;
}
assert_eq!(buffer.get_absolute(the_answer), Some(&42));Sourcepub fn get_relative(&self, index: usize) -> Option<&T>
pub fn get_relative(&self, index: usize) -> Option<&T>
Retrieves an element in the RingBuffer by relative index.
Element at index 0 is the front of the queue.
§Example
let mut buffer = RingBuffer::new();
buffer.push_back(3);
buffer.push_back(4);
buffer.push_back(5);
buffer.push_back(6);
assert_eq!(buffer.get_relative(1), Some(&4));Sourcepub fn get_relative_mut(&mut self, index: usize) -> Option<&mut T>
pub fn get_relative_mut(&mut self, index: usize) -> Option<&mut T>
Retrieves an element in the RingBuffer mutably by relative index.
Element at index 0 is the front of the queue.
§Example
let mut buffer = RingBuffer::new();
buffer.push_back(3);
buffer.push_back(4);
buffer.push_back(5);
buffer.push_back(6);
if let Some(elem) = buffer.get_relative_mut(1) {
*elem = 7;
}
assert_eq!(buffer.get_relative(1), Some(&7));Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the RingBuffer is empty.
§Example
let mut buffer = RingBuffer::new();
assert!(buffer.is_empty());
buffer.push_back(1);
assert!(!buffer.is_empty());Sourcepub fn is_full(&self) -> bool
pub fn is_full(&self) -> bool
Returns true if the buffer is at full capacity.
§Example
let mut buffer = RingBuffer::with_capacity(2);
assert_eq!(buffer.capacity(), 2);
assert!(!buffer.is_full());
buffer.push_back(1);
buffer.push_back(2);
assert!(buffer.is_full());Sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
pub fn iter(&self) -> Iter<'_, T> ⓘ
Returns a front-to-back iterator.
§Example
let mut buffer = RingBuffer::new();
buffer.push_back(3);
buffer.push_back(4);
buffer.push_back(5);
let b: &[_] = &[&3, &4, &5];
let c: Vec<&usize> = buffer.iter().collect();
assert_eq!(buffer.front(), Some(&3));
assert_eq!(&c[..], b);Sourcepub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
Returns a front-to-back iterator that returns mutable references.
§Example
let mut buffer = RingBuffer::new();
buffer.push_back(3);
buffer.push_back(4);
buffer.push_back(5);
for num in buffer.iter_mut() {
*num = *num - 2;
}
let b: &[_] = &[&mut 1, &mut 2, &mut 3];
assert_eq!(&buffer.iter_mut().collect::<Vec<&mut usize>>()[..], b);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the RingBuffer.
§Example
let mut buffer = RingBuffer::new();
assert_eq!(buffer.len(), 0);
buffer.push_back(1);
assert_eq!(buffer.len(), 1);Sourcepub fn pop_front(&mut self) -> Option<T>
pub fn pop_front(&mut self) -> Option<T>
Removes the first element and returns it, or None if the RingBuffer is
empty.
§Example
let mut buffer = RingBuffer::new();
buffer.push_back(1);
buffer.push_back(2);
assert_eq!(buffer.pop_front(), Some(1));
assert_eq!(buffer.pop_front(), Some(2));
assert_eq!(buffer.pop_front(), None);Sourcepub fn push_back(&mut self, new_element: T) -> RingBufferIndex
pub fn push_back(&mut self, new_element: T) -> RingBufferIndex
Appends an element to the back of the RingBuffer.
§Examples
let mut buffer = RingBuffer::new();
buffer.push_back(1);
buffer.push_back(42);
assert_eq!(42, *buffer.back().unwrap());Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional more elements to be inserted in the given
RingBuffer. The collection may reserve more space to avoid frequent reallocations.
§Panics
Panics if the new capacity overflows usize.
§Example
let mut buffer: RingBuffer<usize> = vec![1].into_iter().collect();
buffer.reserve(10);
assert!(buffer.capacity() >= 11);Sourcepub fn shrink_to(&mut self, min_capacity: usize)
pub fn shrink_to(&mut self, min_capacity: usize)
Shrinks the capacity of the RingBuffer with a lower bound.
The capacity will remain at least as large as the maximum of the length and the supplied value.
Does nothing if the current capacity is smaller than the supplied minimum capacity.
Also only shrinks if no element position needs to change as to preserve the indices of the allocated elements.
§Example
let mut buffer = RingBuffer::with_capacity(15);
buffer.extend(0..4);
assert!(buffer.capacity() >= 15);
assert!(!(buffer.capacity() < 6));
buffer.shrink_to(6);
assert!(buffer.capacity() >= 6);
assert!(!(buffer.capacity() < 4));
buffer.shrink_to(0);
assert!(buffer.capacity() >= 4);Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the RingBuffer as much as possible.
It will drop down as close as possible to the length but the allocator may still inform the
RingBuffer that there is space for a few more elements.
§Example
let mut buffer = RingBuffer::with_capacity(15);
buffer.extend(0..4);
assert!(buffer.capacity() >= 15);
buffer.shrink_to_fit();
assert!(buffer.capacity() >= 4);Sourcepub fn swap_relative(&mut self, i: usize, j: usize)
pub fn swap_relative(&mut self, i: usize, j: usize)
Swaps elements at relative indices i and j.
i and j may be equal.
Element at index 0 is the front of the queue.
§Panics
Panics if either index is out of bounds.
§Example
let mut buffer = RingBuffer::new();
buffer.push_back(3);
buffer.push_back(4);
buffer.push_back(5);
buffer.push_back(6);
buffer.push_back(7);
assert_eq!(buffer.get_relative(0), Some(&3));
assert_eq!(buffer.get_relative(2), Some(&5));
buffer.swap_relative(0, 2);
assert_eq!(buffer.get_relative(0), Some(&5));
assert_eq!(buffer.get_relative(2), Some(&3));Sourcepub fn swap_absolute(&mut self, i: RingBufferIndex, j: RingBufferIndex)
pub fn swap_absolute(&mut self, i: RingBufferIndex, j: RingBufferIndex)
Swaps elements at absolute indices i and j.
i and j may be equal.
This is designed to be used in conjunction with push_back.
§Panics
Panics if either index is out of bounds.
§Example
let mut buffer = RingBuffer::new();
buffer.push_back(3);
let first = buffer.push_back(4);
buffer.push_back(5);
let second = buffer.push_back(6);
buffer.push_back(7);
assert_eq!(buffer.get_absolute(first), Some(&4));
assert_eq!(buffer.get_absolute(second), Some(&6));
buffer.swap_absolute(first, second);
assert_eq!(buffer.get_absolute(first), Some(&6));
assert_eq!(buffer.get_absolute(second), Some(&4));Sourcepub fn truncate(&mut self, len: usize)
pub fn truncate(&mut self, len: usize)
Shortens the RingBuffer, dropping excess elements from the back.
If len is greater than the RingBuffer’s current length, this has no
effect.
§Example
let mut buffer = RingBuffer::new();
buffer.push_back(5);
buffer.push_back(10);
buffer.push_back(15);
assert_eq!(buffer.len(), 3);
buffer.truncate(1);
assert_eq!(buffer.len(), 1);
assert_eq!(buffer.get_relative(0), Some(&15));Trait Implementations§
Source§impl<T> Clone for RingBuffer<T>where
T: Clone,
impl<T> Clone for RingBuffer<T>where
T: Clone,
Source§fn clone(&self) -> RingBuffer<T>
fn clone(&self) -> RingBuffer<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T> Debug for RingBuffer<T>where
T: Debug,
impl<T> Debug for RingBuffer<T>where
T: Debug,
Source§impl<T> Default for RingBuffer<T>
impl<T> Default for RingBuffer<T>
Source§fn default() -> RingBuffer<T>
fn default() -> RingBuffer<T>
Source§impl<T> Drop for RingBuffer<T>
impl<T> Drop for RingBuffer<T>
Source§impl<'a, T> Extend<&'a T> for RingBuffer<T>where
T: 'a + Copy,
impl<'a, T> Extend<&'a T> for RingBuffer<T>where
T: 'a + Copy,
Source§fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<T> Extend<T> for RingBuffer<T>
impl<T> Extend<T> for RingBuffer<T>
Source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)