Struct BufferInitializer

Source
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>

Source

pub const fn uninit(inner: T) -> Self

Wrap a possibly-uninitialized buffer into the initializer, with the current initialization cursor set to zero.

Source

pub fn into_inner(self) -> T

Source

pub const fn items_initialized(&self) -> usize

Source§

impl<T, Item> BufferInitializer<AsUninit<T>>
where T: Deref<Target = [Item]> + DerefMut + TrustedDeref,

Source

pub fn new(init: T) -> Self

Construct an initializer to a container that is already initialized. This ensures that no bytes will be filled with zeroes, as they do not have to.

Source§

impl<T> BufferInitializer<T>
where T: Initialize,

Source

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.

Source

pub unsafe fn advance_to_end(&mut self)

Advance the initialization counter to the end.

§Safety

While this eliminates the need for the caller to bounds check manually, unlike with advance, the caller must uphold the initialization invariant.

Source

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.

Source

pub fn into_raw_parts(self) -> (T, usize)

Source

pub fn all_uninit(&self) -> &[MaybeUninit<T::Item>]

Retrieve a slice of a possibly uninitialized items, over the entire buffer.

Source

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.

Source

pub fn capacity(&self) -> usize

Get the total size of the buffer that is being initialized.

Source

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]>).

Source

pub fn is_completely_init(&self) -> bool

Check whether the buffer is completely initialized. Note that this is unrelated to it being filled.

Source

pub fn is_completely_uninit(&self) -> bool

Check whether no single item of the buffer has been initialized.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn finish_init_by_filling(self, item: T::Item) -> AssertInit<T>
where T::Item: Copy,

Finish the initialization by writing item to the uninitialized region, and then get the final initialized type.

Source

pub fn fill_uninit_part(&mut self, item: T::Item)
where T::Item: Copy,

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.

Source

pub fn partially_fill_uninit_part(&mut self, count: usize, item: T::Item)
where T::Item: Copy,

Source

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.

Source

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>,

Source

pub fn finish_init_by_zeroing(self) -> AssertInit<T>

Finish the initialization by zeroing uninitialized region, and then get the final initialized type.

Source

pub fn partially_zero_uninit_part(&mut self, count: usize)

Source

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.

Trait Implementations§

Source§

impl<T: Debug> Debug for BufferInitializer<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for BufferInitializer<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for BufferInitializer<T>
where T: RefUnwindSafe,

§

impl<T> Send for BufferInitializer<T>
where T: Send,

§

impl<T> Sync for BufferInitializer<T>
where T: Sync,

§

impl<T> Unpin for BufferInitializer<T>
where T: Unpin,

§

impl<T> UnwindSafe for BufferInitializer<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.