dasp_ring_buffer

Struct Fixed

Source
pub struct Fixed<S> { /* private fields */ }
Expand description

A ring buffer with a fixed length.

AKA Circular buffer, cyclic buffer, FIFO queue.

Elements are pushed and popped from the buffer simultaneously in order to retain a consistent length.

A Fixed ring buffer can be created around any type with a slice to write to.

fn main() {
    // From a fixed size array.
    dasp_ring_buffer::Fixed::from([1, 2, 3, 4]);

    // From a Vec.
    dasp_ring_buffer::Fixed::from(vec![1, 2, 3, 4]);

    // From a Boxed slice.
    dasp_ring_buffer::Fixed::from(vec![1, 2, 3].into_boxed_slice());

    // From a mutably borrowed slice.
    let mut slice = [1, 2, 3, 4];
    dasp_ring_buffer::Fixed::from(&mut slice[..]);

    // An immutable ring buffer from an immutable slice.
    let slice = [1, 2, 3, 4];
    dasp_ring_buffer::Fixed::from(&slice[..]);
}

Implementations§

Source§

impl<S> Fixed<S>
where S: Slice,

Source

pub fn len(&self) -> usize

The fixed length of the buffer.

fn main() {
    let rb = dasp_ring_buffer::Fixed::from([0; 4]);
    assert_eq!(rb.len(), 4);
}
Source

pub fn push(&mut self, item: S::Element) -> S::Element
where S: SliceMut,

Push the given item onto the back of the queue and return the item at the front of the queue, ensuring that the length is retained.

fn main() {
    let mut rb = dasp_ring_buffer::Fixed::from([0, 1, 2, 3]);
    assert_eq!(rb.push(4), 0);
    assert_eq!(rb.push(5), 1);
    assert_eq!(rb.push(6), 2);
    assert_eq!(rb.push(7), 3);
    assert_eq!(rb.push(8), 4);
    assert_eq!([rb[0], rb[1], rb[2], rb[3]], [5, 6, 7, 8]);
}
Source

pub fn get(&self, index: usize) -> &S::Element

Borrows the item at the given index.

If index is out of range it will be looped around the length of the data slice.

fn main() {
    let mut rb = dasp_ring_buffer::Fixed::from([0, 1, 2]);
    assert_eq!(*rb.get(0), 0);
    assert_eq!(*rb.get(1), 1);
    assert_eq!(*rb.get(2), 2);
    assert_eq!(*rb.get(3), 0);
    assert_eq!(*rb.get(4), 1);
    assert_eq!(*rb.get(5), 2);
}
Source

pub fn get_mut(&mut self, index: usize) -> &mut S::Element
where S: SliceMut,

Mutably borrows the item at the given index.

If index is out of range it will be looped around the length of the data slice.

Source

pub fn set_first(&mut self, index: usize)

Sets the index of the first element within the data slice.

If index is out of range it will be looped around the length of the data slice.

fn main() {
    let mut rb = dasp_ring_buffer::Fixed::from([0, 1, 2, 3]);
    assert_eq!(rb[0], 0);
    rb.set_first(2);
    assert_eq!(rb[0], 2);
    rb.set_first(5);
    assert_eq!(rb[0], 1);
}
Source

pub fn slices(&self) -> (&[S::Element], &[S::Element])

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.

fn main() {
    let mut ring_buffer = dasp_ring_buffer::Fixed::from([0; 4]);
    assert_eq!(ring_buffer.slices(), (&[0, 0, 0, 0][..], &[][..]));
    ring_buffer.push(1);
    ring_buffer.push(2);
    assert_eq!(ring_buffer.slices(), (&[0, 0][..], &[1, 2][..]));
    ring_buffer.push(3);
    ring_buffer.push(4);
    assert_eq!(ring_buffer.slices(), (&[1, 2, 3, 4][..], &[][..]));
}
Source

pub fn slices_mut(&mut self) -> (&mut [S::Element], &mut [S::Element])
where S: SliceMut,

The same as the slices method, but returns mutable slices instead.

Source

pub fn iter_loop(&self) -> Skip<Cycle<Iter<'_, S::Element>>>

Produce an iterator that repeatedly yields a reference to each element in the buffer.

Source

pub fn iter(&self) -> Take<Skip<Cycle<Iter<'_, S::Element>>>>

Produce an iterator that yields a reference to each element in the buffer.

Source

pub fn iter_mut( &mut self, ) -> Chain<IterMut<'_, S::Element>, IterMut<'_, S::Element>>
where S: SliceMut,

Produce an iterator that yields a mutable reference to each element in the buffer.

Source

pub fn from_raw_parts(first: usize, data: S) -> Self

Creates a Fixed ring buffer from its starting index and data buffer type.

**Panic!**s if the given index is out of range of the given data slice.

Note: This method should only be necessary if you require specifying a first index. Please see the ring_buffer::Fixed::from function for a simpler constructor that does not require a first index.

Source

pub unsafe fn from_raw_parts_unchecked(first: usize, data: S) -> Self

Creates a Fixed ring buffer from its starting index and data buffer type.

This method is unsafe as there is no guarantee that first < data.slice().len().

Source

pub fn into_raw_parts(self) -> (usize, S)

Consumes the Fixed ring buffer and returns its parts:

  • usize is an index into the first element of the buffer.
  • S is the buffer data.

Trait Implementations§

Source§

impl<S: Clone> Clone for Fixed<S>

Source§

fn clone(&self) -> Fixed<S>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S: Debug> Debug for Fixed<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S> From<S> for Fixed<S>
where S: Slice,

Source§

fn from(data: S) -> Self

Construct a Fixed ring buffer from the given data slice.

**Panic!**s if the given data buffer is empty.

Source§

impl<S, T> FromIterator<T> for Fixed<S>
where S: Slice<Element = T> + FromIterator<T>,

Source§

fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = T>,

Creates a value from an iterator. Read more
Source§

impl<S: Hash> Hash for Fixed<S>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

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

impl<S> Index<usize> for Fixed<S>
where S: Slice,

Source§

type Output = <S as Slice>::Element

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

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

impl<S> IndexMut<usize> for Fixed<S>
where S: SliceMut,

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

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

impl<S: PartialEq> PartialEq for Fixed<S>

Source§

fn eq(&self, other: &Fixed<S>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<S: Copy> Copy for Fixed<S>

Source§

impl<S: Eq> Eq for Fixed<S>

Source§

impl<S> StructuralPartialEq for Fixed<S>

Auto Trait Implementations§

§

impl<S> Freeze for Fixed<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for Fixed<S>
where S: RefUnwindSafe,

§

impl<S> Send for Fixed<S>
where S: Send,

§

impl<S> Sync for Fixed<S>
where S: Sync,

§

impl<S> Unpin for Fixed<S>
where S: Unpin,

§

impl<S> UnwindSafe for Fixed<S>
where S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.