pub struct BufferInitializer<T> { /* private fields */ }
Expand description
An initialized tracking a container type that dereferences into a slice of possibly-uninitialized items, and how many items have been initialized, respectively. The inner data can always be moved out as uninitialized, but when the buffer has been fully initialized, the buffer can be turned into the initialized equivalent.
Implementations§
Source§impl<T> BufferInitializer<T>
impl<T> BufferInitializer<T>
Sourcepub const fn uninit(inner: T) -> Self
pub const fn uninit(inner: T) -> Self
Wrap a possibly-uninitialized buffer into the initializer, with the current initialization cursor set to zero.
pub fn into_inner(self) -> T
pub const fn items_initialized(&self) -> usize
Source§impl<T, Item> BufferInitializer<AsUninit<T>>
impl<T, Item> BufferInitializer<AsUninit<T>>
Source§impl<T> BufferInitializer<T>where
T: Initialize,
impl<T> BufferInitializer<T>where
T: Initialize,
Sourcepub unsafe fn advance(&mut self, count: usize)
pub unsafe fn advance(&mut self, count: usize)
Advance the initialization counter by count
items.
§Safety
For this to be safe, the caller must be sure that another count
items from the previous
initialization offset, are initialized.
This method does not do any bounds checking. Ergo, count
can never be larger than
the value returned by remaining
.
Sourcepub unsafe fn advance_to_end(&mut self)
pub unsafe fn advance_to_end(&mut self)
Sourcepub unsafe fn assume_init(self) -> AssertInit<T>
pub unsafe fn assume_init(self) -> AssertInit<T>
Assume that the inner value is fully initialized, finalizing the original type into its initialized counterpart.
§Safety
The caller must uphold the initialization invariant.
pub fn into_raw_parts(self) -> (T, usize)
Sourcepub fn all_uninit(&self) -> &[MaybeUninit<T::Item>]
pub fn all_uninit(&self) -> &[MaybeUninit<T::Item>]
Retrieve a slice of a possibly uninitialized items, over the entire buffer.
Sourcepub unsafe fn all_uninit_mut(&mut self) -> &mut [MaybeUninit<T::Item>]
pub unsafe fn all_uninit_mut(&mut self) -> &mut [MaybeUninit<T::Item>]
Retrieve a mutable slice of a possibly uninitialized items, over the entire buffer.
§Safety
This is unsafe, because the caller must not de-initialize the slice as the API also promises the initialized region to always actually be initialized.
Sourcepub fn remaining(&self) -> usize
pub fn remaining(&self) -> usize
Get the number of items that must be filled before the buffer gets fully initialized, and
can be turned into an initialized type (e.g. Box<[U]>
).
Sourcepub fn is_completely_init(&self) -> bool
pub fn is_completely_init(&self) -> bool
Check whether the buffer is completely initialized. Note that this is unrelated to it being filled.
Sourcepub fn is_completely_uninit(&self) -> bool
pub fn is_completely_uninit(&self) -> bool
Check whether no single item of the buffer has been initialized.
Sourcepub fn uninit_part(&self) -> &[MaybeUninit<T::Item>]
pub fn uninit_part(&self) -> &[MaybeUninit<T::Item>]
Retrieve a shared reference to the uninitialized part of the buffer. This is only included for completeness, since apart from some corner cases where one does not have exclusive access to the buffer but still wants to initialize it, is rather useless.
Sourcepub fn init_part(&self) -> &[T::Item]
pub fn init_part(&self) -> &[T::Item]
Retrieve a shared slice to the initialized part of the buffer. Note that this is different from the filled part, as a buffer can be fully initialized but not filled.
Sourcepub fn uninit_part_mut(&mut self) -> &mut [MaybeUninit<T::Item>]
pub fn uninit_part_mut(&mut self) -> &mut [MaybeUninit<T::Item>]
Get a mutable slice to the uninitialized part of the buffer. Note that this is different from the unfilled part of it.
Sourcepub fn init_part_mut(&mut self) -> &mut [T::Item]
pub fn init_part_mut(&mut self) -> &mut [T::Item]
Retrieve a mutable slice to the initialized part of the buffer. Note that this is not the same as the filled part.
Sourcepub fn try_into_init(self) -> Result<AssertInit<T>, Self>
pub fn try_into_init(self) -> Result<AssertInit<T>, Self>
Try to transform the initializing type, into its initialized counterpart, provided that the it has been fully initialized.
Sourcepub fn finish_init_by_filling(self, item: T::Item) -> AssertInit<T>
pub fn finish_init_by_filling(self, item: T::Item) -> AssertInit<T>
Finish the initialization by writing item
to the uninitialized region, and then get the
final initialized type.
Sourcepub fn fill_uninit_part(&mut self, item: T::Item)
pub fn fill_uninit_part(&mut self, item: T::Item)
Fill the uninitialized part with copies of item
(memset).
After this method has been called, it is safe to assume_init
. try_into_init
will
then also succeed.
pub fn partially_fill_uninit_part(&mut self, count: usize, item: T::Item)
Sourcepub fn init_uninit_parts(&self) -> (&[T::Item], &[MaybeUninit<T::Item>])
pub fn init_uninit_parts(&self) -> (&[T::Item], &[MaybeUninit<T::Item>])
Get both the initialized and uninitialized parts simultaneously. This method is nothing but a shorthand for the individual methods, but included for completeness.
This is because the mutable counterpart init_uninit_parts_mut
cannot be done separately
by calling the init_part_mut
and uninit_part_mut
methods.
Sourcepub fn init_uninit_parts_mut(
&mut self,
) -> (&mut [T::Item], &mut [MaybeUninit<T::Item>])
pub fn init_uninit_parts_mut( &mut self, ) -> (&mut [T::Item], &mut [MaybeUninit<T::Item>])
Borrow both the initialized as well as the uninitialized parts, mutably.
Source§impl<T> BufferInitializer<T>where
T: Initialize<Item = u8>,
impl<T> BufferInitializer<T>where
T: Initialize<Item = u8>,
Sourcepub fn finish_init_by_zeroing(self) -> AssertInit<T>
pub fn finish_init_by_zeroing(self) -> AssertInit<T>
Finish the initialization by zeroing uninitialized region, and then get the final initialized type.
pub fn partially_zero_uninit_part(&mut self, count: usize)
Sourcepub fn zero_uninit_part(&mut self)
pub fn zero_uninit_part(&mut self)
Zero the uninitialized part.
After this method has been called, it is safe to assume_init
. try_into_init
will
then also succeed.