Struct UninitView

Source
pub struct UninitView<'a, T: ?Sized> { /* private fields */ }
Expand description

A non-mutable view on a region used in an Uninit.

Makes it possible to utilize the traversal methods (split*, cast*, ..) without requiring a mutable reference to the original Uninit. It will also never expose mutable pointers or accidentally offer an aliased mutable reference. Prefer this to instead avoiding the borrow of the Uninit and manually managing pointers to the region.

Implementations§

Source§

impl UninitView<'_, ()>

Source

pub unsafe fn from_memory(ptr: NonNull<u8>, len: usize) -> Self

Create a uninit view from raw memory.

§Safety

A valid allocation must exist at the pointer with length at least len.

In particular, it is UB to create this from a reference to a variable of a type for which a completely uninitialized content is not valid. The standard type for avoiding the UB is core::mem::MaybeUninit.

When in doubt, refactor code such that utilization of from_maybe_uninit is possible.

Source

pub fn split_layout(&mut self, layout: Layout) -> Option<Self>

Split so that the second part fits the layout.

See Uninit::split_layout for more details.

Source§

impl<'a> UninitView<'a, ()>

Source

pub fn split_cast<U>(&mut self) -> Option<UninitView<'a, U>>

Split so that the tail is aligned and valid for a U.

Source

pub fn split_slice<U>(&mut self) -> Option<UninitView<'a, [U]>>

Split so that the tail is aligned for a slice [U].

Source§

impl<T> UninitView<'_, T>

Source

pub fn invent_for_zst() -> Self

Invent a new uninit allocation for a zero-sized type (ZST).

§Panics

This method panics when the type parameter is not a zero sized type.

Source§

impl<'a, T> UninitView<'a, T>

Source

pub fn split_at_byte(&mut self, at: usize) -> Option<UninitView<'a, ()>>

Split the uninit view at a byte boundary.

See Uninit::split_at_byte for more details.

Source

pub fn from_maybe_uninit(mem: &'a MaybeUninit<T>) -> Self

Create an view to the inner bytes of a MaybeUninit.

This is hardly useful on its own but since UninitView mirrors the traversal methods of Uninit it can be used to get pointers to already initialized elements in an immutable context.

Source

pub fn cast_slice<U>(self) -> Result<UninitView<'a, [U]>, Self>

Try to cast to an UninitView for a slice type.

Source

pub fn split_to_fit(&mut self) -> UninitView<'a, ()>

Split off the tail that is not required for holding an instance of T.

Source

pub fn into_maybe_uninit(self) -> &'a MaybeUninit<T>

Turn this into a reference to standard MaybeUninit.

This is mainly useful for interfacing with other consumers which expect standard library types and to mirror Uninit.

Note that the sequence from_maybe_uninit, into_maybe_uninit is a no-op. The converse is however not the case, as it will potentially discard unused padding present in the original Uninit.

Source§

impl<'a, T: ?Sized> UninitView<'a, T>

Source

pub unsafe fn new(ptr: NonNull<T>, len: usize) -> Self

Create a reference to typed uninitialized memory.

It is given a capacity of memory to which it refers in bytes.

§Safety

The ptr must describe a valid, sized region. Refer to Layout::for_value_raw for details. This criteria is trivially fulfilled for any sized T.

A valid allocation must exist at the pointer with length at least len.

In particular, it is UB to create this from a reference to a variable of a type for which a completely uninitialized content is not valid. The standard type for avoiding the UB is core::mem::MaybeUninit.

When in doubt, refactor code such that utilization of from_maybe_uninit is possible.

Source

pub fn byte_capacity(&self) -> usize

Return the number of bytes this may refer to.

Source

pub fn cast<U>(self) -> Result<UninitView<'a, U>, Self>

Try to cast to an UninitView for another type.

Source

pub const fn as_ptr(&self) -> *const T

Acquires the underlying *const T pointer.

Source

pub fn as_non_null(&self) -> NonNull<T>

Acquires the underlying pointer as a NonNull.

Source

pub unsafe fn as_ref(&self) -> &T

Dereferences the content.

The resulting lifetime is bound to self so this behaves “as if” it were actually an instance of T that is getting borrowed. If a longer lifetime is needed, use into_ref.

§Safety

The caller must ensure that the content has already been initialized.

Source

pub unsafe fn into_ref(self) -> &'a T

Turn this into a reference to the content.

§Safety

The caller must ensure that the content has already been initialized.

Source§

impl<'a, T> UninitView<'a, [T]>

Source

pub fn empty() -> Self

Creates a pointer to an empty slice.

Note that it will not be a mutable empty slice which means that it would be UB to use it as an Uninit.

Source

pub fn from_maybe_uninit_slice(mem: &'a [MaybeUninit<T>]) -> Self

Create an view on potentially uninitialized memory bytes of a slice of MaybeUninit.

Source

pub fn as_begin_ptr(&self) -> *const T

Get the pointer to the first element of the slice.

Source

pub fn capacity(&self) -> usize

Calculate the theoretical capacity of a slice in the pointed-to allocation.

Source

pub fn split_at(&mut self, at: usize) -> Option<Self>

Split the slice at an index.

Source

pub fn shrink_to_fit(&mut self) -> UninitView<'a, ()>

Get the trailing bytes behind the slice.

The underlying allocation need not be a multiple of the slice element size which may leave unusable bytes. This splits these unusable bytes into an untyped Uninit which can be reused arbitrarily.

This operation is idempotent.

Source

pub fn split_first(&mut self) -> Option<UninitView<'a, T>>

Split the first element from the slice.

Source

pub fn split_last(&mut self) -> Option<UninitView<'a, T>>

Split the last element from the slice.

Source

pub fn into_maybe_uninit_slice(self) -> &'a [MaybeUninit<T>]

Turn this into a slice of standard MaybeUninits.

This is mainly useful for interfacing with other consumers which expect standard library types and to mirror Uninit.

Note that the sequence from_maybe_uninit_slice, into_maybe_uninit_slice is a no-op. The converse is however not the case, as it will potentially discard unused padding present in the original Uninit.

Source§

impl<'a, T: ?Sized> UninitView<'a, T>

Source

pub fn fits(&self, layout: Layout) -> bool

Check if the view fits some layout.

The cast to a type of the provided layout will work without error.

Source

pub fn borrow(&self) -> UninitView<'_, T>

Borrow another view of the Uninit region.

Source

pub const fn size(&self) -> usize

Get the byte size of the total allocation.

Trait Implementations§

Source§

impl<T: ?Sized> Clone for UninitView<'_, T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a, T, U: ?Sized> CoerciblePtr<U> for UninitView<'a, T>

Source§

type Pointee = T

The type we point to. This influences which kinds of unsizing are possible.
Source§

type Output = UninitView<'a, U>

The output type when unsizing the pointee to U.
Source§

fn as_sized_ptr(&mut self) -> *mut T

Get the raw inner pointer.
Source§

unsafe fn replace_ptr(self, new: *mut U) -> UninitView<'a, U>

Replace the container inner pointer with an unsized version. Read more
Source§

impl<T: ?Sized> Debug for UninitView<'_, T>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T> Default for UninitView<'_, [T]>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'a, T> From<&'a [MaybeUninit<T>]> for UninitView<'a, [T]>

Source§

fn from(mem: &'a [MaybeUninit<T>]) -> Self

Converts to this type from the input type.
Source§

impl<'a, T> From<&'a MaybeUninit<T>> for UninitView<'a, T>

Source§

fn from(mem: &'a MaybeUninit<T>) -> Self

Converts to this type from the input type.
Source§

impl<'a, T> From<Uninit<'a, T>> for UninitView<'a, T>

Source§

fn from(uninit: Uninit<'a, T>) -> Self

Converts to this type from the input type.
Source§

impl<T: ?Sized> Copy for UninitView<'_, T>

Auto Trait Implementations§

§

impl<'a, T> Freeze for UninitView<'a, T>
where T: ?Sized,

§

impl<'a, T> RefUnwindSafe for UninitView<'a, T>
where T: RefUnwindSafe + ?Sized,

§

impl<'a, T> !Send for UninitView<'a, T>

§

impl<'a, T> !Sync for UninitView<'a, T>

§

impl<'a, T> Unpin for UninitView<'a, T>
where T: ?Sized,

§

impl<'a, T> UnwindSafe for UninitView<'a, T>
where T: RefUnwindSafe + ?Sized,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T, U> CoerceUnsize<U> for T
where T: CoerciblePtr<U>, U: ?Sized,

Source§

fn unsize<F>(self, with: Coercion<Self::Pointee, U, F>) -> Self::Output
where F: FnOnce(*const Self::Pointee) -> *const U,

Convert a pointer, as if with unsize coercion. 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.