Struct sized_chunks::ring_buffer::RingBuffer [−][src]
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
impl<A, N> RingBuffer<A, N> where
N: ChunkLength<A>,
[src]
N: ChunkLength<A>,
pub const CAPACITY: usize
[src]
The capacity of this ring buffer, as a usize
.
#[must_use]pub fn new() -> Self
[src]
Construct an empty ring buffer.
#[must_use]pub fn unit(value: A) -> Self
[src]
Construct a ring buffer with a single item.
#[must_use]pub fn pair(value1: A, value2: A) -> Self
[src]
Construct a ring buffer with two items.
#[must_use]pub fn drain_from(other: &mut Self) -> Self
[src]
Construct a new ring buffer and move every item from other
into the
new buffer.
Time: O(n)
#[must_use]pub fn collect_from<I>(iter: &mut I, count: usize) -> Self where
I: Iterator<Item = A>,
[src]
I: Iterator<Item = A>,
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)
#[must_use]pub fn from_front(other: &mut Self, count: usize) -> Self
[src]
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
#[must_use]pub fn from_back(other: &mut Self, count: usize) -> Self
[src]
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
#[must_use]pub fn is_full(&self) -> bool
[src]
Test if the ring buffer is full.
#[must_use]pub fn iter(&self) -> Iter<'_, A, N>ⓘ
[src]
Get an iterator over references to the items in the ring buffer in order.
#[must_use]pub fn iter_mut(&mut self) -> IterMut<'_, A, N>ⓘ
[src]
Get an iterator over mutable references to the items in the ring buffer in order.
#[must_use]pub fn slice<R: RangeBounds<usize>>(&self, range: R) -> Slice<'_, A, N>
[src]
Get a Slice
for a subset of the ring buffer.
#[must_use]pub fn slice_mut<R: RangeBounds<usize>>(
&mut self,
range: R
) -> SliceMut<'_, A, N>
[src]
&mut self,
range: R
) -> SliceMut<'_, A, N>
Get a SliceMut
for a subset of the ring buffer.
#[must_use]pub unsafe fn get_unchecked(&self, index: usize) -> &A
[src]
Get an unchecked reference to the value at the given index.
Safety
You must ensure the index is not out of bounds.
#[must_use]pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut A
[src]
Get an unchecked mutable reference to the value at the given index.
Safety
You must ensure the index is not out of bounds.
pub fn push_back(&mut self, value: A)
[src]
Push a value to the back of the buffer.
Panics if the capacity of the buffer is exceeded.
Time: O(1)
pub fn push_front(&mut self, value: A)
[src]
Push a value to the front of the buffer.
Panics if the capacity of the buffer is exceeded.
Time: O(1)
pub fn pop_back(&mut self) -> Option<A>
[src]
Pop a value from the back of the buffer.
Returns None
if the buffer is empty.
Time: O(1)
pub fn pop_front(&mut self) -> Option<A>
[src]
Pop a value from the front of the buffer.
Returns None
if the buffer is empty.
Time: O(1)
pub fn drop_left(&mut self, index: usize)
[src]
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
pub fn drop_right(&mut self, index: usize)
[src]
Discard all items from index
onward.
Panics if index
is out of bounds.
Time: O(n) for the number of items dropped
#[must_use]pub fn split_off(&mut self, index: usize) -> Self
[src]
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
pub fn append(&mut self, other: &mut Self)
[src]
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
pub fn drain_from_front(&mut self, other: &mut Self, count: usize)
[src]
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
pub fn drain_from_back(&mut self, other: &mut Self, count: usize)
[src]
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
pub fn insert(&mut self, index: usize, value: A)
[src]
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
pub fn insert_ordered(&mut self, value: A) where
A: Ord,
[src]
A: Ord,
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.
Examples
let mut chunk = Chunk::<i32, U64>::from_iter(0..5); chunk.insert_ordered(3); assert_eq!(&[0, 1, 2, 3, 3, 4], chunk.as_slice());
pub fn insert_from<Iterable, I>(&mut self, index: usize, iter: Iterable) where
Iterable: IntoIterator<Item = A, IntoIter = I>,
I: ExactSizeIterator<Item = A>,
[src]
Iterable: IntoIterator<Item = A, IntoIter = I>,
I: ExactSizeIterator<Item = A>,
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).
pub fn remove(&mut self, index: usize) -> A
[src]
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
pub fn drain(&mut self) -> Drain<'_, A, N>ⓘNotable traits for Drain<'a, A, N>
impl<'a, A: 'a, N: ChunkLength<A> + 'a> Iterator for Drain<'a, A, N> type Item = A;
[src]
Notable traits for Drain<'a, A, N>
impl<'a, A: 'a, N: ChunkLength<A> + 'a> Iterator for Drain<'a, A, N> type Item = A;
Construct an iterator that drains values from the front of the buffer.
pub fn clear(&mut self)
[src]
Discard the contents of the buffer.
Time: O(n)
Trait Implementations
impl<'a, A, N> Arbitrary<'a> for RingBuffer<A, N> where
A: Arbitrary<'a>,
N: ChunkLength<A> + 'static,
[src]
A: Arbitrary<'a>,
N: ChunkLength<A> + 'static,
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
[src]
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self>
[src]
fn size_hint(depth: usize) -> (usize, Option<usize>)
[src]
impl<A, N> Array for RingBuffer<A, N> where
N: ChunkLength<A>,
[src]
N: ChunkLength<A>,
#[must_use]fn get(&self, index: usize) -> Option<&A>
[src]
Get a reference to the value at a given index.
pub fn first(&self) -> Option<&Self::Output>
[src]
pub fn last(&self) -> Option<&Self::Output>
[src]
pub fn contains(&self, target: &Self::Output) -> bool where
Self::Output: PartialEq<Self::Output>,
[src]
Self::Output: PartialEq<Self::Output>,
pub fn binary_search(&self, target: &Self::Output) -> Result<usize, usize> where
Self::Output: Ord,
[src]
Self::Output: Ord,
pub fn binary_search_by<F>(&self, compare: F) -> Result<usize, usize> where
F: FnMut(&Self::Output) -> Ordering,
[src]
F: FnMut(&Self::Output) -> Ordering,
pub fn binary_search_by_key<K, F>(
&self,
key: &K,
extract: F
) -> Result<usize, usize> where
K: Ord,
F: FnMut(&Self::Output) -> K,
[src]
&self,
key: &K,
extract: F
) -> Result<usize, usize> where
K: Ord,
F: FnMut(&Self::Output) -> K,
pub fn is_sorted(&self) -> bool where
Self::Output: PartialOrd<Self::Output>,
[src]
Self::Output: PartialOrd<Self::Output>,
pub fn is_sorted_by<F>(&self, compare: F) -> bool where
F: FnMut(&Self::Output, &Self::Output) -> Option<Ordering>,
[src]
F: FnMut(&Self::Output, &Self::Output) -> Option<Ordering>,
pub fn is_sorted_by_key<K, F>(&self, extract: F) -> bool where
K: PartialOrd<K>,
F: FnMut(&Self::Output) -> K,
[src]
K: PartialOrd<K>,
F: FnMut(&Self::Output) -> K,
pub fn starts_with(&self, slice: &[Self::Output]) -> bool where
Self::Output: PartialEq<Self::Output>,
Self::Output: Sized,
[src]
Self::Output: PartialEq<Self::Output>,
Self::Output: Sized,
pub fn ends_with(&self, slice: &[Self::Output]) -> bool where
Self::Output: PartialEq<Self::Output>,
Self::Output: Sized,
[src]
Self::Output: PartialEq<Self::Output>,
Self::Output: Sized,
impl<A, N> ArrayMut for RingBuffer<A, N> where
N: ChunkLength<A>,
[src]
N: ChunkLength<A>,
#[must_use]fn get_mut(&mut self, index: usize) -> Option<&mut A>
[src]
Get a mutable reference to the value at a given index.
pub fn first_mut(&mut self) -> Option<&mut Self::Output>
[src]
pub fn last_mut(&mut self) -> Option<&mut Self::Output>
[src]
pub fn set(&mut self, index: usize, value: Self::Output) -> Option<Self::Output> where
Self::Output: Sized,
[src]
Self::Output: Sized,
pub fn swap(&mut self, index1: usize, index2: usize) where
Self::Output: Sized,
[src]
Self::Output: Sized,
pub fn map_pair<F, A>(&mut self, index1: usize, index2: usize, f: F) -> A where
F: FnMut(&mut Self::Output, &mut Self::Output) -> A,
[src]
F: FnMut(&mut Self::Output, &mut Self::Output) -> A,
pub fn sort_unstable(&mut self) where
Self::Output: Ord,
Self::Output: Sized,
[src]
Self::Output: Ord,
Self::Output: Sized,
pub fn sort_unstable_by<F>(&mut self, compare: F) where
F: FnMut(&Self::Output, &Self::Output) -> Ordering,
Self::Output: Sized,
[src]
F: FnMut(&Self::Output, &Self::Output) -> Ordering,
Self::Output: Sized,
pub fn sort_unstable_by_key<F, K>(&mut self, extract: F) where
K: Ord,
F: FnMut(&Self::Output) -> K,
Self::Output: Sized,
[src]
K: Ord,
F: FnMut(&Self::Output) -> K,
Self::Output: Sized,
impl<A: Clone, N: ChunkLength<A>> Clone for RingBuffer<A, N>
[src]
fn clone(&self) -> Self
[src]
pub fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<A: Debug, N: ChunkLength<A>> Debug for RingBuffer<A, N>
[src]
impl<A, N: ChunkLength<A>> Default for RingBuffer<A, N>
[src]
impl<A, N: ChunkLength<A>> Drop for RingBuffer<A, N>
[src]
impl<A: Eq, N: ChunkLength<A>> Eq for RingBuffer<A, N>
[src]
impl<'a, A: Clone + 'a, N: ChunkLength<A>> Extend<&'a A> for RingBuffer<A, N>
[src]
fn extend<I: IntoIterator<Item = &'a A>>(&mut self, iter: I)
[src]
pub fn extend_one(&mut self, item: A)
[src]
pub fn extend_reserve(&mut self, additional: usize)
[src]
impl<A, N: ChunkLength<A>> Extend<A> for RingBuffer<A, N>
[src]
fn extend<I: IntoIterator<Item = A>>(&mut self, iter: I)
[src]
pub fn extend_one(&mut self, item: A)
[src]
pub fn extend_reserve(&mut self, additional: usize)
[src]
impl<'a, A: 'a, N: ChunkLength<A> + 'a> From<&'a RingBuffer<A, N>> for Slice<'a, A, N>
[src]
#[must_use]fn from(buffer: &'a RingBuffer<A, N>) -> Self
[src]
impl<'a, A: 'a, N: ChunkLength<A> + 'a> From<&'a mut RingBuffer<A, N>> for SliceMut<'a, A, N>
[src]
#[must_use]fn from(buffer: &'a mut RingBuffer<A, N>) -> Self
[src]
impl<A, N: ChunkLength<A>> FromIterator<A> for RingBuffer<A, N>
[src]
#[must_use]fn from_iter<I: IntoIterator<Item = A>>(iter: I) -> Self
[src]
impl<A, N> HasLength for RingBuffer<A, N> where
N: ChunkLength<A>,
[src]
N: ChunkLength<A>,
#[must_use]fn len(&self) -> usize
[src]
Get the length of the ring buffer.
pub fn is_empty(&self) -> bool
[src]
impl<A: Hash, N: ChunkLength<A>> Hash for RingBuffer<A, N>
[src]
fn hash<H: Hasher>(&self, hasher: &mut H)
[src]
pub fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl<A, N> Index<usize> for RingBuffer<A, N> where
N: ChunkLength<A>,
[src]
N: ChunkLength<A>,
type Output = A
The returned type after indexing.
#[must_use]fn index(&self, index: usize) -> &Self::Output
[src]
impl<A, N> IndexMut<usize> for RingBuffer<A, N> where
N: ChunkLength<A>,
[src]
N: ChunkLength<A>,
impl<A, N: ChunkLength<A>> IntoIterator for RingBuffer<A, N>
[src]
type Item = A
The type of the elements being iterated over.
type IntoIter = OwnedIter<A, N>
Which kind of iterator are we turning this into?
#[must_use]fn into_iter(self) -> Self::IntoIter
[src]
impl<'a, A, N: ChunkLength<A>> IntoIterator for &'a RingBuffer<A, N>
[src]
type Item = &'a A
The type of the elements being iterated over.
type IntoIter = Iter<'a, A, N>
Which kind of iterator are we turning this into?
#[must_use]fn into_iter(self) -> Self::IntoIter
[src]
impl<'a, A, N: ChunkLength<A>> IntoIterator for &'a mut RingBuffer<A, N>
[src]
type Item = &'a mut A
The type of the elements being iterated over.
type IntoIter = IterMut<'a, A, N>
Which kind of iterator are we turning this into?
#[must_use]fn into_iter(self) -> Self::IntoIter
[src]
impl<A: Ord, N: ChunkLength<A>> Ord for RingBuffer<A, N>
[src]
#[must_use]fn cmp(&self, other: &Self) -> Ordering
[src]
#[must_use]pub fn max(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn min(self, other: Self) -> Self
1.21.0[src]
#[must_use]pub fn clamp(self, min: Self, max: Self) -> Self
1.50.0[src]
impl<A, N, PrimSlice> PartialEq<PrimSlice> for RingBuffer<A, N> where
PrimSlice: Borrow<[A]>,
A: PartialEq,
N: ChunkLength<A>,
[src]
PrimSlice: Borrow<[A]>,
A: PartialEq,
N: ChunkLength<A>,
#[must_use]fn eq(&self, other: &PrimSlice) -> bool
[src]
#[must_use]pub fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
impl<'a, A: PartialEq + 'a, N: ChunkLength<A> + 'a> PartialEq<RingBuffer<A, N>> for Slice<'a, A, N>
[src]
#[must_use]fn eq(&self, other: &RingBuffer<A, N>) -> bool
[src]
#[must_use]pub fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
impl<'a, A: PartialEq + 'a, N: ChunkLength<A> + 'a> PartialEq<RingBuffer<A, N>> for SliceMut<'a, A, N>
[src]
#[must_use]fn eq(&self, other: &RingBuffer<A, N>) -> bool
[src]
#[must_use]pub fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
impl<A: PartialEq, N: ChunkLength<A>> PartialEq<RingBuffer<A, N>> for RingBuffer<A, N>
[src]
#[must_use]fn eq(&self, other: &Self) -> bool
[src]
#[must_use]pub fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
impl<A, N> PartialEq<Slice<'_, A, N>> for RingBuffer<A, N> where
A: PartialEq,
N: ChunkLength<A>,
[src]
A: PartialEq,
N: ChunkLength<A>,
fn eq(&self, other: &Slice<'_, A, N>) -> bool
[src]
#[must_use]pub fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
impl<A, N> PartialEq<SliceMut<'_, A, N>> for RingBuffer<A, N> where
A: PartialEq,
N: ChunkLength<A>,
[src]
A: PartialEq,
N: ChunkLength<A>,
fn eq(&self, other: &SliceMut<'_, A, N>) -> bool
[src]
#[must_use]pub fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
impl<A: PartialOrd, N: ChunkLength<A>> PartialOrd<RingBuffer<A, N>> for RingBuffer<A, N>
[src]
#[must_use]fn partial_cmp(&self, other: &Self) -> Option<Ordering>
[src]
#[must_use]pub fn lt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]pub fn le(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]pub fn gt(&self, other: &Rhs) -> bool
1.0.0[src]
#[must_use]pub fn ge(&self, other: &Rhs) -> bool
1.0.0[src]
impl<A, N> PoolClone for RingBuffer<A, N> where
A: Clone,
N: ChunkLength<A>,
[src]
A: Clone,
N: ChunkLength<A>,
unsafe fn clone_uninit(&self, target: &mut MaybeUninit<Self>)
[src]
impl<A, N> PoolDefault for RingBuffer<A, N> where
N: ChunkLength<A>,
[src]
N: ChunkLength<A>,
unsafe fn default_uninit(target: &mut MaybeUninit<Self>)
[src]
impl<N: ChunkLength<u8>> Read for RingBuffer<u8, N>
[src]
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
[src]
pub fn read_vectored(
&mut self,
bufs: &mut [IoSliceMut<'_>]
) -> Result<usize, Error>
1.36.0[src]
&mut self,
bufs: &mut [IoSliceMut<'_>]
) -> Result<usize, Error>
pub fn is_read_vectored(&self) -> bool
[src]
pub unsafe fn initializer(&self) -> Initializer
[src]
pub fn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
1.0.0[src]
pub fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
1.0.0[src]
pub fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
1.6.0[src]
pub fn by_ref(&mut self) -> &mut Self
1.0.0[src]
pub fn bytes(self) -> Bytes<Self>
1.0.0[src]
pub fn chain<R>(self, next: R) -> Chain<Self, R> where
R: Read,
1.0.0[src]
R: Read,
pub fn take(self, limit: u64) -> Take<Self>
1.0.0[src]
impl<N: ChunkLength<u8>> Write for RingBuffer<u8, N>
[src]
fn write(&mut self, buf: &[u8]) -> Result<usize>
[src]
fn flush(&mut self) -> Result<()>
[src]
pub fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>
1.36.0[src]
pub fn is_write_vectored(&self) -> bool
[src]
pub fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
1.0.0[src]
pub fn write_all_vectored(
&mut self,
bufs: &mut [IoSlice<'_>]
) -> Result<(), Error>
[src]
&mut self,
bufs: &mut [IoSlice<'_>]
) -> Result<(), Error>
pub fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error>
1.0.0[src]
pub fn by_ref(&mut self) -> &mut Self
1.0.0[src]
Auto Trait Implementations
impl<A, N> RefUnwindSafe for RingBuffer<A, N> where
N: RefUnwindSafe,
<N as ChunkLength<A>>::SizedType: RefUnwindSafe,
N: RefUnwindSafe,
<N as ChunkLength<A>>::SizedType: RefUnwindSafe,
impl<A, N> Send for RingBuffer<A, N> where
N: Send,
<N as ChunkLength<A>>::SizedType: Send,
N: Send,
<N as ChunkLength<A>>::SizedType: Send,
impl<A, N> Sync for RingBuffer<A, N> where
N: Sync,
<N as ChunkLength<A>>::SizedType: Sync,
N: Sync,
<N as ChunkLength<A>>::SizedType: Sync,
impl<A, N> Unpin for RingBuffer<A, N> where
N: Unpin,
<N as ChunkLength<A>>::SizedType: Unpin,
N: Unpin,
<N as ChunkLength<A>>::SizedType: Unpin,
impl<A, N> UnwindSafe for RingBuffer<A, N> where
N: UnwindSafe,
<N as ChunkLength<A>>::SizedType: UnwindSafe,
N: UnwindSafe,
<N as ChunkLength<A>>::SizedType: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Same<T> for T
[src]
type Output = T
Should always be Self
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,