Struct sample::ring_buffer::Bounded
[−]
[src]
pub struct Bounded<S> { /* fields omitted */ }
A ring buffer with an upper bound on its length.
AKA Circular buffer, cyclic buffer, FIFO queue.
Elements can be pushed to the back of the buffer and popped from the front.
Elements must be Copy
due to the behaviour of the push
and pop
methods. If you require
working with non-Copy
elements, the std
VecDeque
type may be better suited.
A Bounded
ring buffer can be created from any type providing a slice to use for pushing and
popping elements.
extern crate sample; use sample::ring_buffer; fn main() { // From a fixed size array. ring_buffer::Bounded::from([0; 4]); // From a Vec. ring_buffer::Bounded::from(vec![0; 4]); // From a Boxed slice. ring_buffer::Bounded::from(vec![0; 3].into_boxed_slice()); // From a mutably borrowed slice. let mut slice = [0; 4]; ring_buffer::Bounded::from(&mut slice[..]); // An immutable ring buffer from an immutable slice. let slice = [0; 4]; ring_buffer::Bounded::from(&slice[..]); }
Two slightly more efficient constructors are provided for fixed-size arrays and boxed slices. These are generally more efficient as they do not require initialising elements.
extern crate sample; use sample::ring_buffer; fn main() { // Fixed-size array. ring_buffer::Bounded::<[i32; 4]>::array(); // Boxed slice. let mut rb = ring_buffer::Bounded::boxed_slice(4); rb.push(1); }
Methods
impl<T> Bounded<Box<[T]>> where
T: Copy,
[src]
T: Copy,
fn boxed_slice(max_len: usize) -> Self
[src]
A Bounded
ring buffer that uses a Box
ed slice with the given maximum length to
represent the data.
Slightly more efficient than using the similar From
constructor as this creates the
underlying slice with uninitialised memory.
extern crate sample; use sample::ring_buffer; fn main() { let mut rb = ring_buffer::Bounded::boxed_slice(4); assert_eq!(rb.max_len(), 4); assert_eq!(rb.len(), 0); rb.push(1); rb.push(2); }
impl<S> Bounded<S> where
S: Slice,
S::Element: Copy,
[src]
S: Slice,
S::Element: Copy,
fn array() -> Self where
S: FixedSizeArray,
[src]
S: FixedSizeArray,
A Bounded
buffer that uses a fixed-size array to represent data.
Slightly more efficient than using the similar From
constructor as this creates the
underlying array with uninitialised memory.
extern crate sample; use sample::ring_buffer; fn main() { let mut rb = ring_buffer::Bounded::<[f32; 3]>::array(); assert_eq!(rb.len(), 0); assert_eq!(rb.max_len(), 3); }
fn from_full(data: S) -> Self
[src]
The same as the From
implementation, but assumes that the given data
is full of valid
elements and initialises the ring buffer with a length equal to max_len
.
extern crate sample; use sample::ring_buffer; fn main() { let mut rb = ring_buffer::Bounded::from_full([0, 1, 2, 3]); assert_eq!(rb.len(), rb.max_len()); assert_eq!(rb.pop(), Some(0)); assert_eq!(rb.pop(), Some(1)); assert_eq!(rb.pop(), Some(2)); assert_eq!(rb.pop(), Some(3)); assert_eq!(rb.pop(), None); }
fn max_len(&self) -> usize
[src]
The maximum length that the Bounded
buffer can be before pushing would overwrite the
front of the buffer.
extern crate sample; fn main() { let mut ring_buffer = sample::ring_buffer::Bounded::<[i32; 3]>::array(); assert_eq!(ring_buffer.max_len(), 3); }
fn len(&self) -> usize
[src]
The current length of the ring buffer.
extern crate sample; fn main() { let mut ring_buffer = sample::ring_buffer::Bounded::<[i32; 3]>::array(); assert_eq!(ring_buffer.len(), 0); }
fn is_full(&self) -> bool
[src]
Whether or not the ring buffer's length is equal to the maximum length.
Equivalent to self.len() == self.max_len()
.
extern crate sample; use sample::ring_buffer; fn main() { let mut rb = ring_buffer::Bounded::<[i32; 2]>::array(); assert!(!rb.is_full()); rb.push(0); rb.push(1); assert!(rb.is_full()); }
fn slices(&self) -> (&[S::Element], &[S::Element])
[src]
The start and end slices that make up the ring buffer.
These two slices chained together represent all elements within the buffer in order.
The first slice is always aligned contiguously behind the second slice.
extern crate sample; fn main() { let mut ring_buffer = sample::ring_buffer::Bounded::<[i32; 4]>::array(); assert_eq!(ring_buffer.slices(), (&[][..], &[][..])); ring_buffer.push(1); ring_buffer.push(2); assert_eq!(ring_buffer.slices(), (&[1, 2][..], &[][..])); ring_buffer.push(3); ring_buffer.push(4); assert_eq!(ring_buffer.slices(), (&[1, 2, 3, 4][..], &[][..])); ring_buffer.push(5); ring_buffer.push(6); assert_eq!(ring_buffer.slices(), (&[3, 4][..], &[5, 6][..])); }
fn slices_mut(&mut self) -> (&mut [S::Element], &mut [S::Element]) where
S: SliceMut,
[src]
S: SliceMut,
The same as the slices
method, but returns mutable slices instead.
fn iter(&self) -> Chain<Iter<S::Element>, Iter<S::Element>>
[src]
Produce an iterator that yields a reference to each element in the buffer.
This method uses the slices
method internally.
extern crate sample; use sample::ring_buffer; fn main() { let mut rb = ring_buffer::Bounded::<[i32; 3]>::array(); assert_eq!(rb.iter().count(), 0); rb.push(1); rb.push(2); assert_eq!(rb.iter().cloned().collect::<Vec<_>>(), vec![1, 2]); }
fn iter_mut(&mut self) -> Chain<IterMut<S::Element>, IterMut<S::Element>> where
S: SliceMut,
[src]
S: SliceMut,
Produce an iterator that yields a mutable reference to each element in the buffer.
This method uses the slices_mut
method internally.
fn get(&self, index: usize) -> Option<&S::Element>
[src]
Borrows the item at the given index.
Returns None
if there is no element at the given index.
extern crate sample; use sample::ring_buffer; fn main() { let mut rb = ring_buffer::Bounded::<[i32; 4]>::array(); assert_eq!(rb.get(1), None); rb.push(0); rb.push(1); assert_eq!(rb.get(1), Some(&1)); }
fn get_mut(&mut self, index: usize) -> Option<&mut S::Element> where
S: SliceMut,
[src]
S: SliceMut,
Mutably borrows the item at the given index.
Returns None
if there is no element at the given index.
fn push(&mut self, elem: S::Element) -> Option<S::Element> where
S: SliceMut,
[src]
S: SliceMut,
Pushes the given element to the back of the buffer.
If the buffer length is currently the max length, this replaces the element at the front of the buffer and returns it.
If the buffer length is less than the max length, this pushes the element to the back of
the buffer and increases the length of the buffer by 1
. None
is returned.
extern crate sample; fn main() { let mut ring_buffer = sample::ring_buffer::Bounded::<[i32; 3]>::array(); assert_eq!(ring_buffer.push(1), None); assert_eq!(ring_buffer.push(2), None); assert_eq!(ring_buffer.len(), 2); assert_eq!(ring_buffer.push(3), None); assert_eq!(ring_buffer.len(), 3); assert_eq!(ring_buffer.push(4), Some(1)); assert_eq!(ring_buffer.len(), 3); }
fn pop(&mut self) -> Option<S::Element> where
S: SliceMut,
[src]
S: SliceMut,
Pop an element from the front of the ring buffer.
If the buffer is empty, this returns None
.
extern crate sample; use sample::ring_buffer; fn main() { let mut rb = ring_buffer::Bounded::from_full([0, 1, 2]); assert_eq!(rb.len(), rb.max_len()); assert_eq!(rb.pop(), Some(0)); assert_eq!(rb.pop(), Some(1)); assert_eq!(rb.push(3), None); assert_eq!(rb.pop(), Some(2)); assert_eq!(rb.pop(), Some(3)); assert_eq!(rb.pop(), None); }
fn from_raw_parts(start: usize, len: usize, data: S) -> Self
[src]
Creates a Bounded
ring buffer from its start index, length and data slice.
The maximum length of the Bounded
ring buffer is assumed to the length of the given slice.
Note: Existing elements within the given data
's slice
will not be dropped when
overwritten by calls to push. Thus, it is safe for the slice to contain uninitialized
elements when using this method.
Note: This method should only be necessary if you require specifying the start
and
initial len
. Please see the Bounded::array
and Bounded::boxed_slice
functions for
simpler constructor options that do not require manually passing indices.
Panic!s if the following conditions are not met:
start
<data.slice().len()
len
<=data.slice().len()
unsafe fn from_raw_parts_unchecked(start: usize, len: usize, data: S) -> Self
[src]
Creates a Bounded
ring buffer from its start
index, len
and data slice.
This method is unsafe as there is no guarantee that either:
start
<data.slice().len()
orlen
<=data.slice().len()
.
unsafe fn into_raw_parts(self) -> (usize, usize, S)
[src]
Consumes the Bounded
ring buffer and returns its parts:
- The first
usize
is an index into the first element of the buffer. - The second
usize
is the length of the ring buffer. S
is the buffer data.
This method is unsafe as the returned data may contain uninitialised memory in the case that the ring buffer is not full.
Trait Implementations
impl<S: Copy> Copy for Bounded<S>
[src]
impl<S: Clone> Clone for Bounded<S>
[src]
fn clone(&self) -> Bounded<S>
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl<S: Debug> Debug for Bounded<S>
[src]
impl<S: PartialEq> PartialEq for Bounded<S>
[src]
fn eq(&self, __arg_0: &Bounded<S>) -> bool
[src]
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &Bounded<S>) -> bool
[src]
This method tests for !=
.
impl<S: Eq> Eq for Bounded<S>
[src]
impl<S: Hash> Hash for Bounded<S>
[src]
fn hash<__HS: Hasher>(&self, __arg_0: &mut __HS)
[src]
Feeds this value into the given [Hasher
]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more
impl<S> From<S> for Bounded<S> where
S: Slice,
S::Element: Copy,
[src]
S: Slice,
S::Element: Copy,
fn from(data: S) -> Self
[src]
Construct a Bounded
ring buffer from the given data slice.
Panic!s if the given data
buffer is empty.
impl<S, T> FromIterator<T> for Bounded<S> where
S: Slice<Element = T> + FromIterator<T>,
T: Copy,
[src]
S: Slice<Element = T> + FromIterator<T>,
T: Copy,
fn from_iter<I>(iter: I) -> Self where
I: IntoIterator<Item = T>,
[src]
I: IntoIterator<Item = T>,
Creates a value from an iterator. Read more
impl<S> Index<usize> for Bounded<S> where
S: Slice,
S::Element: Copy,
[src]
S: Slice,
S::Element: Copy,
type Output = S::Element
The returned type after indexing.
fn index(&self, index: usize) -> &Self::Output
[src]
Performs the indexing (container[index]
) operation.
impl<S> IndexMut<usize> for Bounded<S> where
S: SliceMut,
S::Element: Copy,
[src]
S: SliceMut,
S::Element: Copy,