[][src]Struct bit_mask_ring_buf::BitMaskRingBuf

pub struct BitMaskRingBuf<T: Copy + Clone + Default> { /* fields omitted */ }

A fast ring buffer implementation with cheap and safe indexing. It works by bit-masking an integer index to get the corresponding index in an array/vec whose length is a power of 2. This is best used when indexing the buffer with an isize value. Copies/reads with slices are implemented with memcpy.

Implementations

impl<T: Copy + Clone + Default> BitMaskRingBuf<T>[src]

pub fn from_capacity(capacity: usize) -> Self[src]

Creates a new BitMaskRingBuf with a capacity that is at least the given capacity. The buffer will be initialized with the default value.

  • capacity - The capacity of the ring buffer. The actual capacity will be set to the next highest power of 2 if capacity is not already a power of 2.

pub fn from_ms(milliseconds: f64, sample_rate: f64) -> Self[src]

Creates a new BitMaskRingBuf with a capacity that holds at least a number of frames/samples in a given time peroid. The actual capacity will be set to the lowest power of 2 that can hold that many values. The buffer will be initialized with the default value.

  • milliseconds - The time period in milliseconds.
  • sample_rate - The sample rate in samples per second.

Panics

  • This will panic if either milliseconds or sample_rate is less than 0.

pub fn set_capacity(&mut self, capacity: usize)[src]

Sets the capacity of the ring buffer. The actual capacity will be set to the next highest power of 2 if capacity is not already a power of 2. This will also clear all values to the default value.

pub fn set_capacity_from_ms(&mut self, milliseconds: f64, sample_rate: f64)[src]

Creates a new BitMaskRingBuf with a capacity that holds at least a number of frames/samples in a given time peroid. The actual capacity will be set to the lowest power of 2 that can hold that many values. This will also clear all values to the default value.

  • milliseconds - The time period in milliseconds.
  • sample_rate - The sample rate in samples per second.

Panics

  • This will panic if either milliseconds or sample_rate is less than 0.

pub fn clear(&mut self)[src]

Clears all values in the ring buffer to the default value.

pub fn as_slices(&self, start: isize) -> (&[T], &[T])[src]

Returns two slices that contain the all the data in the ring buffer starting at the index start.

Returns

  • The first slice is the starting chunk of data. This will never be empty.
  • The second slice is the second contiguous chunk of data. This may or may not be empty depending if the buffer needed to wrap around to the beginning of its internal memory layout.

pub fn as_slices_len(&self, start: isize, len: usize) -> (&[T], &[T])[src]

Returns two slices of data in the ring buffer starting at the index start and with length len.

  • start - The starting index
  • len - The length of data to read. If len is greater than the capacity of the ring buffer, then that capacity will be used instead.

Returns

  • The first slice is the starting chunk of data.
  • The second slice is the second contiguous chunk of data. This may or may not be empty depending if the buffer needed to wrap around to the beginning of its internal memory layout.

pub fn as_mut_slices(&mut self, start: isize) -> (&mut [T], &mut [T])[src]

Returns two mutable slices that contain the all the data in the ring buffer starting at the index start.

Returns

  • The first slice is the starting chunk of data. This will never be empty.
  • The second slice is the second contiguous chunk of data. This may or may not be empty depending if the buffer needed to wrap around to the beginning of its internal memory layout.

pub fn as_mut_slices_len(
    &mut self,
    start: isize,
    len: usize
) -> (&mut [T], &mut [T])
[src]

Returns two mutable slices of data in the ring buffer starting at the index start and with length len.

  • start - The starting index
  • len - The length of data to read. If len is greater than the capacity of the ring buffer, then that capacity will be used instead.

Returns

  • The first slice is the starting chunk of data.
  • The second slice is the second contiguous chunk of data. This may or may not be empty depending if the buffer needed to wrap around to the beginning of its internal memory layout.

pub fn read_into(&self, slice: &mut [T], start: isize)[src]

Copies the data from the ring buffer starting from the index start into the given slice. If the length of slice is larger than the capacity of the ring buffer, then the data will be reapeated until the given slice is filled.

  • slice - This slice to copy the data into.
  • start - The index of the ring buffer to start copying from.

pub fn write_latest(&mut self, slice: &[T], start: isize)[src]

Copies data from the given slice into the ring buffer starting from the index start. If the length of slice is larger than the capacity of the ring buffer, then only the latest data will be copied.

  • slice - This slice to copy data from.
  • start - The index of the ring buffer to start copying from.

pub fn capacity(&self) -> usize[src]

Returns the capacity of the ring buffer.

pub fn constrain(&self, i: isize) -> isize[src]

Returns the actual index of the ring buffer from the given i index. This is cheap due to the ring buffer's bit-masking algorithm.

Trait Implementations

impl<T: Copy + Clone + Default> Index<isize> for BitMaskRingBuf<T>[src]

type Output = T

The returned type after indexing.

impl<T: Copy + Clone + Default> IndexMut<isize> for BitMaskRingBuf<T>[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for BitMaskRingBuf<T> where
    T: RefUnwindSafe

impl<T> Send for BitMaskRingBuf<T> where
    T: Send

impl<T> Sync for BitMaskRingBuf<T> where
    T: Sync

impl<T> Unpin for BitMaskRingBuf<T> where
    T: Unpin

impl<T> UnwindSafe for BitMaskRingBuf<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.