TypeErased

Struct TypeErased 

Source
#[repr(C, align(8))]
pub struct TypeErased<const C: usize> { /* private fields */ }
Expand description

Type-erased data on the stack

See the crate-level docs for more info

Implementations§

Source§

impl<const C: usize> TypeErased<C>

Source

pub fn new<T: 'static>(value: T) -> Self

Creates a new TypeErased by erasing the type of value

§Panics

Panics if size_of::<T>() is greater than C or align_of::<T>() is greater than 8 (eventually these will become compile-time restrictions)

Source

pub const fn empty() -> Self

Creates a new TypeErased containing no value

This is effectively equivalent to TypeErased::new::<()>(())

Source

pub unsafe fn new_unchecked<T>(value: T) -> Self

Creates a new TypeErased by erasing the type of value

§Safety

size_of::<T>() must be less than or equal to C and align_of::<T>() must be less than or equal to 8.

This function will be deprecated once it is possible to ensure these at compile-time.

Source

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

Gets a pointer to some type T contained in this TypeErased

§Dereferencability

The returned pointer is valid to dereference if:

  • The size of T is less than or equal to C
  • The alignment of T is less than or equal to 8
  • The data in this TypeErased is a valid instance of T (expired references are not valid)
Source

pub fn as_mut_ptr<T>(&mut self) -> *mut T

Gets a mutable pointer to some type T contained in this TypeErased

§Dereferencability

The returned pointer is valid to dereference if:

  • The size of T is less than or equal to C
  • The alignment of T is less than or equal to 8
  • The data in this TypeErased is a valid instance of T (expired references are not valid)
Source

pub unsafe fn assume_type_ref<T>(&self) -> &T

Assumes this TypeErased contains a valid T and returns a reference to it

§Safety

size_of::<T>() must be less than or equal to C, align_of::<T>() must be less than or equal to 8, and this must contain a valid instance of T (expired references are not valid).

Source

pub unsafe fn assume_type_mut<T>(&mut self) -> &mut T

Assumes this TypeErased contains a valid T and returns a mutable reference to it

§Safety

size_of::<T>() must be less than or equal to C, align_of::<T>() must be less than or equal to 8, and this must contain a valid instance of T (expired references are not valid).

Source

pub unsafe fn assume_type_take<T>(self) -> T

Assumes this TypeErased contains a valid T and takes ownership of it

§Safety

size_of::<T>() must be less than or equal to C, align_of::<T>() must be less than or equal to 8, and this must contain a valid instance of T (expired references are not valid).

Source

pub const fn raw(&self) -> &[MaybeUninit<u8>; C]

Gets the buffer of raw bytes inside

Source

pub fn raw_mut(&mut self) -> &mut [MaybeUninit<u8>; C]

Gets a mutable reference to the buffer of raw bytes inside

Auto Trait Implementations§

§

impl<const C: usize> Freeze for TypeErased<C>

§

impl<const C: usize> RefUnwindSafe for TypeErased<C>

§

impl<const C: usize> !Send for TypeErased<C>

§

impl<const C: usize> !Sync for TypeErased<C>

§

impl<const C: usize> Unpin for TypeErased<C>

§

impl<const C: usize> UnwindSafe for TypeErased<C>

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.