pub struct SliceRB<T> { /* private fields */ }Expand description
A ring buffer implementation optimized for working with slices. Note this pretty
much does the same thing as VecDeque, but with the added ability to index
using negative values, as well as working with buffers allocated on the stack.
This struct can be used without the standard library (#![no_std]).
This struct has no consumer/producer logic, and is meant to be used for DSP or as a base for other data structures.
This data type is optimized for manipulating data in chunks with slices. Indexing one element at a time is slow.
The length of this ring buffer cannot be 0.
§Example
// Create a ring buffer with type u32. The data will be
// initialized with the value of `0`.
let mut rb = SliceRB::<u32>::new(NonZeroUsize::new(4).unwrap(), 0);
// Memcpy data from a slice into the ring buffer at arbitrary
// `isize` indexes. Earlier data will not be copied if it will
// be overwritten by newer data, avoiding unecessary memcpy's.
// The correct placement of the newer data will still be preserved.
rb.write_latest(&[0, 2, 3, 4, 1], 0);
assert_eq!(rb[0], 1);
assert_eq!(rb[1], 2);
assert_eq!(rb[2], 3);
assert_eq!(rb[3], 4);
// Memcpy into slices at arbitrary `isize` indexes and length.
let mut read_buffer = [0u32; 7];
rb.read_into(&mut read_buffer, 2);
assert_eq!(read_buffer, [3, 4, 1, 2, 3, 4, 1]);
// Read/write by retrieving slices directly.
let (s1, s2) = rb.as_slices_len(1, 4);
assert_eq!(s1, &[2, 3, 4]);
assert_eq!(s2, &[1]);
// Read/write to buffer by indexing. (Note that indexing
// one element at a time is slow.)
rb[0] = 0;
rb[1] = 1;
rb[2] = 2;
rb[3] = 3;
// Wrap when reading/writing outside of bounds.
assert_eq!(rb[-1], 3);
assert_eq!(rb[10], 2);Implementations§
Source§impl<T> SliceRB<T>
impl<T> SliceRB<T>
Sourcepub unsafe fn new_uninit(len: NonZeroUsize) -> Self
pub unsafe fn new_uninit(len: NonZeroUsize) -> Self
Creates a new SliceRB without initializing data.
len- The length of the ring buffer.
§Safety
- Undefined behavior may occur if uninitialized data is read from. By using this you assume the responsibility of making sure any data is initialized before it is read.
§Example
unsafe {
let rb = SliceRB::<u32>::new_uninit(NonZeroUsize::new(3).unwrap());
assert_eq!(rb.len().get(), 3);
}§Panics
- This will panic if
len > isize::MAX. - This will panic if allocation fails due to being out of memory.
Sourcepub unsafe fn new_exact_uninit(len: NonZeroUsize) -> Self
pub unsafe fn new_exact_uninit(len: NonZeroUsize) -> Self
Creates a new SliceRB with an allocated capacity equal to exactly the
given length. No data will be initialized.
len- The length of the ring buffer.
§Safety
- Undefined behavior may occur if uninitialized data is read from. By using this you assume the responsibility of making sure any data is initialized before it is read.
§Example
unsafe {
let rb = SliceRB::<u32>::new_exact_uninit(NonZeroUsize::new(3).unwrap());
assert_eq!(rb.len().get(), 3);
}§Panics
- This will panic if
len > isize::MAX. - This will panic if allocation fails due to being out of memory.
Sourcepub unsafe fn with_capacity_uninit(len: NonZeroUsize, capacity: usize) -> Self
pub unsafe fn with_capacity_uninit(len: NonZeroUsize, capacity: usize) -> Self
Creates a new SliceRB without initializing data, while reserving extra
capacity for future changes to len.
len- The length of the ring buffer.capacity- The allocated capacity of the ring buffer. If this is less thanlen, then it will be ignored.
§Safety
- Undefined behavior may occur if uninitialized data is read from. By using this you assume the responsibility of making sure any data is initialized before it is read.
§Example
unsafe {
let rb = SliceRB::<u32>::with_capacity_uninit(NonZeroUsize::new(3).unwrap(), 10);
assert_eq!(rb.len().get(), 3);
assert!(rb.capacity().get() >= 10);
}§Panics
- This will panic if
len > isize::MAXorcapacity > isize::MAX. - This will panic if allocation fails due to being out of memory.
Sourcepub fn len(&self) -> NonZeroUsize
pub fn len(&self) -> NonZeroUsize
Returns the length of the ring buffer.
§Example
let rb = SliceRB::<u32>::new(NonZeroUsize::new(4).unwrap(), 0);
assert_eq!(rb.len().get(), 4);Sourcepub fn capacity(&self) -> NonZeroUsize
pub fn capacity(&self) -> NonZeroUsize
Returns the allocated capacity of the internal vector.
Please note this is not the same as the length of the buffer.
For that use SliceRB::len().
§Example
let rb = SliceRB::<u32>::new(NonZeroUsize::new(4).unwrap(), 0);
assert!(rb.capacity().get() >= 4);Sourcepub fn constrain(&self, i: isize) -> isize
pub fn constrain(&self, i: isize) -> isize
Returns the actual index of the ring buffer from the given
i index.
- First, a bounds check will be performed. If it is within bounds, then it is simply returned.
- If it is not in bounds, then performance will
be limited by the modulo (remainder) operation on an
isizevalue.
§Performance
Prefer to manipulate data in bulk with methods that return slices. If you
need to index multiple elements one at a time, prefer to use
SliceRB::at(&mut i) over SliceRB[i] to reduce the number of
modulo operations to perform.
§Example
let rb = SliceRB::<u32>::new(NonZeroUsize::new(4).unwrap(), 0);
assert_eq!(rb.constrain(2), 2);
assert_eq!(rb.constrain(4), 0);
assert_eq!(rb.constrain(-3), 1);
assert_eq!(rb.constrain(7), 3);Sourcepub unsafe fn set_len_uninit(&mut self, len: NonZeroUsize)
pub unsafe fn set_len_uninit(&mut self, len: NonZeroUsize)
Sets the length of the ring buffer without initializing any newly allocated data.
- If
lenis less than the current length, then the data will be truncated. - If
lenis larger than the current length, then all newly allocated elements appended to the end will be unitialized.
§Safety
- Undefined behavior may occur if uninitialized data is read from. By using this you assume the responsibility of making sure any data is initialized before it is read.
§Example
let mut rb = SliceRB::<u32>::new(NonZeroUsize::new(2).unwrap(), 0);
rb[0] = 1;
rb[1] = 2;
unsafe {
rb.set_len_uninit(NonZeroUsize::new(4).unwrap());
assert_eq!(rb.len().get(), 4);
assert_eq!(rb[0], 1);
assert_eq!(rb[1], 2);
}§Panics
- This will panic if
len > isize::MAX. - This will panic if allocation fails due to being out of memory.
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 internal Vec. This is equivalant to Vec::reserve().
The collection may reserve more space to avoid frequent reallocations. After calling reserve, capacity will be greater than or equal to self.len() + additional. Does nothing if capacity is already sufficient.
§Example
let mut rb = SliceRB::<u32>::new(NonZeroUsize::new(2).unwrap(), 0);
rb.reserve(8);
assert!(rb.capacity().get() >= 10);§Panics
- Panics if out of memory.
Sourcepub fn reserve_exact(&mut self, additional: usize)
pub fn reserve_exact(&mut self, additional: usize)
Reserves capacity for exactly additional more elements to be inserted
in the internal Vec. This is equivalant to Vec::reserve_exact().
The collection may reserve more space to avoid frequent reallocations. After calling reserve, capacity will be greater than or equal to self.len() + additional. Does nothing if capacity is already sufficient.
Note that the allocator may give the collection more space than it requests. Therefore,
capacity can not be relied upon to be precisely minimal. Prefer reserve if future
insertions are expected.
§Example
let mut rb = SliceRB::<u32>::new(NonZeroUsize::new(2).unwrap(), 0);
rb.reserve_exact(8);
assert!(rb.capacity().get() >= 10);§Panics
- Panics if out of memory.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the internal Vec as much as possible. This is equivalant to
Vec::shrink_to_fit.
It will drop down as close as possible to the length but the allocator may still inform the vector that there is space for a few more elements.
§Example
let mut rb = SliceRB::<u32>::new(NonZeroUsize::new(2).unwrap(), 0);
rb.reserve(8);
assert!(rb.capacity().get() >= 10);
rb.shrink_to_fit();
assert!(rb.capacity().get() >= 2);Sourcepub fn as_slices(&self, start: isize) -> (&[T], &[T])
pub fn as_slices(&self, start: isize) -> (&[T], &[T])
Returns two slices that contain 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.
§Performance
Prefer to use this to manipulate data in bulk over indexing one element at a time.
§Example
let mut rb = SliceRB::<u32>::new(NonZeroUsize::new(4).unwrap(), 0);
rb[0] = 1;
rb[1] = 2;
rb[2] = 3;
rb[3] = 4;
let (s1, s2) = rb.as_slices(-4);
assert_eq!(s1, &[1, 2, 3, 4]);
assert_eq!(s2, &[]);
let (s1, s2) = rb.as_slices(3);
assert_eq!(s1, &[4]);
assert_eq!(s2, &[1, 2, 3]);Sourcepub fn as_slices_len(&self, start: isize, len: usize) -> (&[T], &[T])
pub fn as_slices_len(&self, start: isize, len: usize) -> (&[T], &[T])
Returns two slices of data in the ring buffer
starting at the index start and with length len.
start- The starting indexlen- The length of data to read. Iflenis greater than the length of the ring buffer, then that length 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.
§Performance
Prefer to use this to manipulate data in bulk over indexing one element at a time.
§Example
let mut rb = SliceRB::<u32>::new(NonZeroUsize::new(4).unwrap(), 0);
rb[0] = 1;
rb[1] = 2;
rb[2] = 3;
rb[3] = 4;
let (s1, s2) = rb.as_slices_len(-4, 3);
assert_eq!(s1, &[1, 2, 3]);
assert_eq!(s2, &[]);
let (s1, s2) = rb.as_slices_len(3, 5);
assert_eq!(s1, &[4]);
assert_eq!(s2, &[1, 2, 3]);Sourcepub fn as_slices_latest(&self, start: isize, len: usize) -> (&[T], &[T])
pub fn as_slices_latest(&self, start: isize, len: usize) -> (&[T], &[T])
Returns two slices of data in the ring buffer
starting at the index start and with length len. If len is greater
than the length of the ring buffer, then the buffer’s length will be used
instead, while still preserving the position of the last element.
start- The starting indexlen- The length of data to read. Iflenis greater than the length of the ring buffer, then the buffer’s length will be used instead, while still preserving the position of the last element.
§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.
§Performance
Prefer to use this to manipulate data in bulk over indexing one element at a time.
§Example
let mut rb = SliceRB::<u32>::new(NonZeroUsize::new(4).unwrap(), 0);
rb[0] = 1;
rb[1] = 2;
rb[2] = 3;
rb[3] = 4;
let (s1, s2) = rb.as_slices_latest(-4, 3);
assert_eq!(s1, &[1, 2, 3]);
assert_eq!(s2, &[]);
let (s1, s2) = rb.as_slices_latest(0, 5);
assert_eq!(s1, &[2, 3, 4]);
assert_eq!(s2, &[1]);Sourcepub fn as_mut_slices(&mut self, start: isize) -> (&mut [T], &mut [T])
pub fn as_mut_slices(&mut self, start: isize) -> (&mut [T], &mut [T])
Returns two mutable slices that contain 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.
§Performance
Prefer to use this to manipulate data in bulk over indexing one element at a time.
§Example
let mut rb = SliceRB::<u32>::new(NonZeroUsize::new(4).unwrap(), 0);
rb[0] = 1;
rb[1] = 2;
rb[2] = 3;
rb[3] = 4;
let (s1, s2) = rb.as_mut_slices(-4);
assert_eq!(s1, &mut [1, 2, 3, 4]);
assert_eq!(s2, &mut []);
let (s1, s2) = rb.as_mut_slices(3);
assert_eq!(s1, &mut [4]);
assert_eq!(s2, &mut [1, 2, 3]);Sourcepub fn as_mut_slices_len(
&mut self,
start: isize,
len: usize,
) -> (&mut [T], &mut [T])
pub fn as_mut_slices_len( &mut self, start: isize, len: usize, ) -> (&mut [T], &mut [T])
Returns two mutable slices of data in the ring buffer
starting at the index start and with length len.
start- The starting indexlen- The length of data to read. Iflenis greater than the length of the ring buffer, then that length 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.
§Performance
Prefer to use this to manipulate data in bulk over indexing one element at a time.
§Example
let mut rb = SliceRB::<u32>::new(NonZeroUsize::new(4).unwrap(), 0);
rb[0] = 1;
rb[1] = 2;
rb[2] = 3;
rb[3] = 4;
let (s1, s2) = rb.as_mut_slices_len(-4, 3);
assert_eq!(s1, &mut [1, 2, 3]);
assert_eq!(s2, &mut []);
let (s1, s2) = rb.as_mut_slices_len(3, 5);
assert_eq!(s1, &mut [4]);
assert_eq!(s2, &mut [1, 2, 3]);Sourcepub fn as_mut_slices_latest(
&mut self,
start: isize,
len: usize,
) -> (&mut [T], &mut [T])
pub fn as_mut_slices_latest( &mut self, start: isize, len: usize, ) -> (&mut [T], &mut [T])
Returns two mutable slices of data in the ring buffer
starting at the index start and with length len. If len is greater
than the length of the ring buffer, then the buffer’s length will be used
instead, while still preserving the position of the last element.
start- The starting indexlen- The length of data to read. Iflenis greater than the length of the ring buffer, then the buffer’s length will be used instead, while still preserving the position of the last element.
§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.
§Performance
Prefer to use this to manipulate data in bulk over indexing one element at a time.
§Example
let mut rb = SliceRB::<u32>::new(NonZeroUsize::new(4).unwrap(), 0);
rb[0] = 1;
rb[1] = 2;
rb[2] = 3;
rb[3] = 4;
let (s1, s2) = rb.as_mut_slices_latest(-4, 3);
assert_eq!(s1, &mut [1, 2, 3]);
assert_eq!(s2, &mut []);
let (s1, s2) = rb.as_mut_slices_latest(0, 5);
assert_eq!(s1, &mut [2, 3, 4]);
assert_eq!(s2, &mut [1]);Sourcepub fn raw_data(&self) -> &[T]
pub fn raw_data(&self) -> &[T]
Returns all the data in the buffer. The starting index will
always be 0.
§Example
let mut rb = SliceRB::<u32>::new(NonZeroUsize::new(4).unwrap(), 0);
rb[0] = 1;
rb[1] = 2;
rb[2] = 3;
rb[3] = 4;
let raw_data = rb.raw_data();
assert_eq!(raw_data, &[1u32, 2, 3, 4]);Sourcepub fn raw_data_mut(&mut self) -> &mut [T]
pub fn raw_data_mut(&mut self) -> &mut [T]
Returns all the data in the buffer as mutable. The starting
index will always be 0.
§Example
let mut rb = SliceRB::<u32>::new(NonZeroUsize::new(4).unwrap(), 0);
rb[0] = 1;
rb[1] = 2;
rb[2] = 3;
rb[3] = 4;
let raw_data = rb.raw_data_mut();
assert_eq!(raw_data, &mut [1u32, 2, 3, 4]);Sourcepub fn get(&self, i: isize) -> &T
pub fn get(&self, i: isize) -> &T
Returns an immutable reference the element at the index of type isize.
This struct is gauranteed to have at least one element.
§Performance
Prefer to manipulate data in bulk with methods that return slices. If you
need to index multiple elements one at a time, prefer to use
this over SliceRB[i] to reduce the number of
modulo operations to perform.
§Example
let mut rb = SliceRB::<u32>::new(NonZeroUsize::new(4).unwrap(), 0);
rb[0] = 1;
rb[1] = 2;
rb[2] = 3;
rb[3] = 4;
assert_eq!(*rb.get(-3), 2);Sourcepub fn get_mut(&mut self, i: isize) -> &mut T
pub fn get_mut(&mut self, i: isize) -> &mut T
Returns a mutable reference the element at the index of type isize.
This struct is gauranteed to have at least one element.
§Performance
Prefer to manipulate data in bulk with methods that return slices. If you
need to index multiple elements one at a time, prefer to use
this over SliceRB[i] to reduce the number of
modulo operations to perform.
§Example
let mut rb = SliceRB::<u32>::new(NonZeroUsize::new(4).unwrap(), 0);
*rb.get_mut(-3) = 5;
assert_eq!(*rb.get(-3), 5);Sourcepub fn constrain_and_get(&self, i: &mut isize) -> &T
pub fn constrain_and_get(&self, i: &mut isize) -> &T
Returns an immutable reference to the element at the index of type isize
while also constraining the index i. This is more efficient than calling
both methods individually.
This struct is gauranteed to have at least one element.
§Performance
Prefer to manipulate data in bulk with methods that return slices. If you
need to index multiple elements one at a time, prefer to use
this over SliceRB[i] to reduce the number of
modulo operations to perform.
§Example
let mut rb = SliceRB::<u32>::new(NonZeroUsize::new(4).unwrap(), 0);
rb[0] = 1;
rb[1] = 2;
rb[2] = 3;
rb[3] = 4;
let mut i = -3;
assert_eq!(*rb.constrain_and_get(&mut i), 2);
assert_eq!(i, 1);Sourcepub fn constrain_and_get_mut(&mut self, i: &mut isize) -> &mut T
pub fn constrain_and_get_mut(&mut self, i: &mut isize) -> &mut T
Returns a mutable reference to the element at the index of type isize as
mutable while also constraining the index i. This is more efficient than
calling both methods individually.
This struct is gauranteed to have at least one element.
§Performance
Prefer to manipulate data in bulk with methods that return slices. If you
need to index multiple elements one at a time, prefer to use
this over SliceRBRef[i] to reduce the number of
modulo operations to perform.
§Example
let mut rb = SliceRB::<u32>::new(NonZeroUsize::new(4).unwrap(), 0);
let mut i = -3;
*rb.constrain_and_get_mut(&mut i) = 2;
assert_eq!(rb[1], 2);
assert_eq!(i, 1);Source§impl<T: Clone> SliceRB<T>
impl<T: Clone> SliceRB<T>
Sourcepub fn new(len: NonZeroUsize, value: T) -> Self
pub fn new(len: NonZeroUsize, value: T) -> Self
Creates a new SliceRB. All data will be initialized with the given value.
len- The length of the ring buffer.
§Example
let rb = SliceRB::<u32>::new(NonZeroUsize::new(3).unwrap(), 0);
assert_eq!(rb.len().get(), 3);
assert_eq!(rb[0], 0);
assert_eq!(rb[1], 0);
assert_eq!(rb[2], 0);§Panics
- This will panic if
len > isize::MAX. - This will panic if allocation fails due to being out of memory.
Sourcepub fn new_exact(len: NonZeroUsize, value: T) -> Self
pub fn new_exact(len: NonZeroUsize, value: T) -> Self
Creates a new SliceRB with an allocated capacity equal to exactly the
given length. All data will be initialized with the given value.
len- The length of the ring buffer.
§Example
let rb = SliceRB::<u32>::new_exact(NonZeroUsize::new(3).unwrap(), 0);
assert_eq!(rb.len().get(), 3);
assert_eq!(rb[0], 0);
assert_eq!(rb[1], 0);
assert_eq!(rb[2], 0);§Panics
- This will panic if
len > isize::MAX. - This will panic if allocation fails due to being out of memory.
Sourcepub fn with_capacity(len: NonZeroUsize, capacity: usize, value: T) -> Self
pub fn with_capacity(len: NonZeroUsize, capacity: usize, value: T) -> Self
Creates a new SliceRB, while reserving extra capacity for future changes
to len. All data from [0..len) will be initialized with the given value.
len- The length of the ring buffer.capacity- The allocated capacity of the ring buffer. If this is less thanlen, then it will be ignored.
§Example
let rb = SliceRB::<u32>::with_capacity(NonZeroUsize::new(3).unwrap(), 10, 0);
assert_eq!(rb.len().get(), 3);
assert!(rb.capacity().get() >= 10);§Panics
- This will panic if
len > isize::MAXorcapacity > isize::MAX. - This will panic if allocation fails due to being out of memory.
Sourcepub fn clear_set_len(&mut self, len: NonZeroUsize, value: T)
pub fn clear_set_len(&mut self, len: NonZeroUsize, value: T)
Sets the length of the ring buffer while clearing all values to the given value.
§Example
let mut rb = SliceRB::<u32>::new(NonZeroUsize::new(2).unwrap(), 0);
rb[0] = 1;
rb[1] = 2;
rb.clear_set_len(NonZeroUsize::new(4).unwrap(), 5);
assert_eq!(rb.len().get(), 4);
assert_eq!(rb[0], 5);
assert_eq!(rb[1], 5);
assert_eq!(rb[2], 5);
assert_eq!(rb[3], 5);§Panics
- This will panic if
len > isize::MAX. - This will panic if allocation fails due to being out of memory.
Sourcepub fn set_len(&mut self, len: NonZeroUsize, value: T)
pub fn set_len(&mut self, len: NonZeroUsize, value: T)
Sets the length of the ring buffer.
- If
lenis less than the current length, then the data will be truncated. - If
lenis larger than the current length, then all newly allocated elements appended to the end will be initialized with the given value.
§Example
let mut rb = SliceRB::<u32>::new(NonZeroUsize::new(2).unwrap(), 0);
rb[0] = 1;
rb[1] = 2;
rb.set_len(NonZeroUsize::new(4).unwrap(), 5);
assert_eq!(rb.len().get(), 4);
assert_eq!(rb[0], 1);
assert_eq!(rb[1], 2);
assert_eq!(rb[2], 5);
assert_eq!(rb[3], 5);§Panics
- This will panic if
len > isize::MAX. - This will panic if allocation fails due to being out of memory.
Source§impl<T: Clone + Copy> SliceRB<T>
impl<T: Clone + Copy> SliceRB<T>
Sourcepub fn read_into(&self, slice: &mut [T], start: isize)
pub fn read_into(&self, slice: &mut [T], start: isize)
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
length 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.
§Performance
Prefer to use this to manipulate data in bulk over indexing one element at a time.
§Example
let mut rb = SliceRB::<u32>::new(NonZeroUsize::new(4).unwrap(), 0);
rb[0] = 1;
rb[1] = 2;
rb[2] = 3;
rb[3] = 4;
let mut read_buf = [0u32; 3];
rb.read_into(&mut read_buf[..], -3);
assert_eq!(read_buf, [2, 3, 4]);
let mut read_buf = [0u32; 9];
rb.read_into(&mut read_buf[..], 2);
assert_eq!(read_buf, [3, 4, 1, 2, 3, 4, 1, 2, 3]);Sourcepub fn write_latest(&mut self, slice: &[T], start: isize)
pub fn write_latest(&mut self, slice: &[T], start: isize)
Copies data from the given slice into the ring buffer starting from
the index start.
Earlier data will not be copied if it will be overwritten by newer data, avoiding unecessary memcpy’s. The correct placement of the newer data will still be preserved.
slice- This slice to copy data from.start- The index of the ring buffer to start copying from.
§Performance
Prefer to use this to manipulate data in bulk over indexing one element at a time.
§Example
let mut rb = SliceRB::<u32>::new(NonZeroUsize::new(4).unwrap(), 0);
let input = [1u32, 2, 3];
rb.write_latest(&input[..], -3);
assert_eq!(rb[0], 0);
assert_eq!(rb[1], 1);
assert_eq!(rb[2], 2);
assert_eq!(rb[3], 3);
let input = [1u32, 2, 3, 4, 5, 6, 7, 8, 9];
rb.write_latest(&input[..], 2);
assert_eq!(rb[0], 7);
assert_eq!(rb[1], 8);
assert_eq!(rb[2], 9);
assert_eq!(rb[3], 6);Sourcepub fn write_latest_2(&mut self, first: &[T], second: &[T], start: isize)
pub fn write_latest_2(&mut self, first: &[T], second: &[T], start: isize)
Copies data from two given slices into the ring buffer starting from
the index start. The first slice will be copied first then second
will be copied next.
Earlier data will not be copied if it will be overwritten by newer data, avoiding unecessary memcpy’s. The correct placement of the newer data will still be preserved.
first- This first slice to copy data from.second- This second slice to copy data from.start- The index of the ring buffer to start copying from.
§Performance
Prefer to use this to manipulate data in bulk over indexing one element at a time.
§Example
let mut input_rb = SliceRB::<u32>::new(NonZeroUsize::new(4).unwrap(), 0);
input_rb[0] = 1;
input_rb[1] = 2;
input_rb[2] = 3;
input_rb[3] = 4;
let mut output_rb = SliceRB::<u32>::new(NonZeroUsize::new(4).unwrap(), 0);
// s1 == &[1, 2], s2 == &[]
let (s1, s2) = input_rb.as_slices_len(0, 2);
output_rb.write_latest_2(s1, s2, -3);
assert_eq!(output_rb[0], 0);
assert_eq!(output_rb[1], 1);
assert_eq!(output_rb[2], 2);
assert_eq!(output_rb[3], 0);
let mut output_rb = SliceRB::<u32>::new(NonZeroUsize::new(2).unwrap(), 0);
// s1 == &[4], s2 == &[1, 2, 3]
let (s1, s2) = input_rb.as_slices_len(3, 4);
// rb[1] = 4 -> rb[0] = 1 -> rb[1] = 2 -> rb[0] = 3
output_rb.write_latest_2(s1, s2, 1);
assert_eq!(output_rb[0], 3);
assert_eq!(output_rb[1], 2);