pub struct ConstBuffer<const TSIZE: usize, T: Sized + Copy> { /* private fields */ }Expand description
This struct allows for iterating over a fixed-length blocks of arrays, given a big chunk of arbitrary length array.
§Examples
Lets say that we have an array of 20 elements:
let data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];and now we want to process it in a block-by-block fashion, with each block having 7 elements. We can do this:
use osom_lib_arrays::fixed_array::{ConstBuffer, ConstFixedArray};
let data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
let mut bufferer = ConstBuffer::<7, _>::new();
let mut iterator = bufferer.buffer_const(&data);
assert_eq!(iterator.next(), Some(&[1, 2, 3, 4, 5, 6, 7]));
assert_eq!(iterator.next(), Some(&[8, 9, 10, 11, 12, 13, 14]));
assert_eq!(iterator.next(), None);
// The iterator is drained, but we still have remaining data:
let remaining: ConstFixedArray<7, _> = bufferer.release_const();
assert_eq!(remaining.as_slice_const(), &[15, 16, 17, 18, 19, 20]);§Notes
The ConstBuffer is additionally const friendly, and thus is defined
for T: Copy only. In particular it isn’t Drop.
Implementations§
Source§impl<const TSIZE: usize, T: Sized + Copy> ConstBuffer<TSIZE, T>
impl<const TSIZE: usize, T: Sized + Copy> ConstBuffer<TSIZE, T>
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates a new, empty ConstBufferer.
Sourcepub const fn buffer_const<'a>(
&'a mut self,
data: &'a [T],
) -> ConstBufferer<'a, TSIZE, T>
pub const fn buffer_const<'a>( &'a mut self, data: &'a [T], ) -> ConstBufferer<'a, TSIZE, T>
Returns a new ConstBufferer for the given data.
§Panics
The function will panic if the data length exceeds u32::MAX.
Sourcepub const fn length(&self) -> Length
pub const fn length(&self) -> Length
Returns the length of the data buffered in the ConstBuffer.
Sourcepub const fn clone_const(&self) -> Self
pub const fn clone_const(&self) -> Self
Clones the ConstBuffer.
Sourcepub const fn current_state_const(&self) -> &ConstFixedArray<TSIZE, T>
pub const fn current_state_const(&self) -> &ConstFixedArray<TSIZE, T>
Returns a reference to the currently buffered data.
Sourcepub const fn release_const(self) -> ConstFixedArray<TSIZE, T>
pub const fn release_const(self) -> ConstFixedArray<TSIZE, T>
Releases the remaining data from the bufferer.
Trait Implementations§
Auto Trait Implementations§
impl<const TSIZE: usize, T> Freeze for ConstBuffer<TSIZE, T>where
T: Freeze,
impl<const TSIZE: usize, T> RefUnwindSafe for ConstBuffer<TSIZE, T>where
T: RefUnwindSafe,
impl<const TSIZE: usize, T> Send for ConstBuffer<TSIZE, T>where
T: Send,
impl<const TSIZE: usize, T> Sync for ConstBuffer<TSIZE, T>where
T: Sync,
impl<const TSIZE: usize, T> Unpin for ConstBuffer<TSIZE, T>where
T: Unpin,
impl<const TSIZE: usize, T> UnsafeUnpin for ConstBuffer<TSIZE, T>where
T: UnsafeUnpin,
impl<const TSIZE: usize, T> UnwindSafe for ConstBuffer<TSIZE, T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more