pub struct Cursor<'a, T, const N: usize> { /* private fields */ }
Expand description
A fixed-size cursor for initializing MaybeUninit
arrays
The cursor will guarantee that all values have been
initialized when the value is dropped, which means
that it is safe to call MaybeUninit::assume_init()
.
NOTE: This guarantee only holds as long as Drop::drop()
is called.
If the value goes out of scope without drop being called (e.g. because
of core::mem::forget()
), then this guarantee no longer applies.
Implementations§
Source§impl<'a, T, const N: usize> Cursor<'a, T, N>
impl<'a, T, const N: usize> Cursor<'a, T, N>
Sourcepub fn new(slice: &'a mut [MaybeUninit<T>; N]) -> Self
pub fn new(slice: &'a mut [MaybeUninit<T>; N]) -> Self
Creates a new cursor.
Sourcepub fn finish(self, value: [T; N])
pub fn finish(self, value: [T; N])
Finishes the buffer by writing the remaining values.
This is equivalent to calling self.write::<N, 0>(value)
, except it is slightly
more ergonomic.
Sourcepub fn write<const L: usize, const R: usize>(
self,
value: [T; L],
) -> Cursor<'a, T, R>
pub fn write<const L: usize, const R: usize>( self, value: [T; L], ) -> Cursor<'a, T, R>
Writes L
values to the buffer and returns a new cursor for the remaining R
values.
This function cannot compile unless L + R == N
, however it will be able to pass through
cargo check
, since the error is not discovered by rustc
until it tries to instantiate
the code.
Sourcepub fn split<const L: usize, const R: usize>(
self,
) -> (Cursor<'a, T, L>, Cursor<'a, T, R>)
pub fn split<const L: usize, const R: usize>( self, ) -> (Cursor<'a, T, L>, Cursor<'a, T, R>)
Splits the cursor in two.
This function cannot compile unless L + R == N
, however it will be able to pass through
cargo check
, since the error is not discovered by rustc
until it tries to instantiate
the code.
Sourcepub fn assert_size<const M: usize>(self) -> Cursor<'a, T, M>
pub fn assert_size<const M: usize>(self) -> Cursor<'a, T, M>
Compile-time assertion that N == M
to work-around limitations in rust generics.
This is useful if a type-signature requires the function to have a generic size argument, but you want compile-time errors when called with the wrong parameter.
§Examples
fn example<const N: usize>(cursor: array_init_cursor::Cursor<'_, u8, N>) {
let cursor: array_init_cursor::Cursor<u8, 10> = cursor.assert_size();
}