Struct iffi::MaybeInvalid

source ·
#[repr(transparent)]
pub struct MaybeInvalid<T>(_);
Expand description

Represents a value of the type T that may not be well-defined.

This type guarantees that MaybeInvalid<T> and T have the same, size, alignment and ABI.

Despite that this type uses MaybeUninit internally, the value must always be initialized.

MaybeInvalid<T> is always well-defined if T: Nicheless.

Implementations§

source§

impl<T> MaybeInvalid<T>

source

pub fn new(valid: T) -> Self

Creates a valid value.

source

pub fn zeroed() -> Self

Creates a potentially invalid value.

source

pub fn as_bytes(&self) -> &[u8]

Gets a shared reference to the value in this container as a byte slice.

source

pub unsafe fn assume_valid(self) -> T

Extracts the value from this container.

Safety

It is up to the caller to ensure the value in this container is valid. For example, the following code causes UB:

let invalid = MaybeInvalid::<NonZeroU8>::zeroed();
// UB because NonZeroU8 cannot be zero.
let zero = unsafe { invalid.assume_valid() };
source

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

Gets a shared reference to the value in this container.

Safety

See assume_valid.

source

pub unsafe fn assume_valid_mut(&mut self) -> &mut T

Gets a mutable (unique) reference to the value in this container.

Safety

See assume_valid.

source

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

Gets a pointer to the contained value

source§

impl<T: Nicheless> MaybeInvalid<T>

source

pub fn into_inner(self) -> T

Extracts the value in this container.

Only implemented for Nicheless types.

If the type is not nicheless, use assume_valid.

Example
let valid = MaybeInvalid::new(5u8);
assert_eq!(valid.into_inner(), 5)
source

pub fn get(&self) -> &T

Gets a shared reference to the value in this container.

Only implemented for Nicheless types.

If the type is not nicheless, use assume_valid_ref.

source

pub fn get_mut(&mut self) -> &mut T

Gets a mutable (unique) reference to the value in this container.

Only implemented for Nicheless types.

If the type is not nicheless, use assume_valid_mut.

Trait Implementations§

source§

impl<T> Debug for MaybeInvalid<T>

source§

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

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

impl Iffi<MaybeInvalid<NonZeroI128>> for NonZeroI128

source§

fn can_transmute(superset: &MaybeInvalid<NonZeroI128>) -> Result<(), Error>

Checks for the safety of transmuting U into Self. Returns Ok(()) if the value is safe, and Err(iffi::Error) otherwise.
source§

impl Iffi<MaybeInvalid<NonZeroI16>> for NonZeroI16

source§

fn can_transmute(superset: &MaybeInvalid<NonZeroI16>) -> Result<(), Error>

Checks for the safety of transmuting U into Self. Returns Ok(()) if the value is safe, and Err(iffi::Error) otherwise.
source§

impl Iffi<MaybeInvalid<NonZeroI32>> for NonZeroI32

source§

fn can_transmute(superset: &MaybeInvalid<NonZeroI32>) -> Result<(), Error>

Checks for the safety of transmuting U into Self. Returns Ok(()) if the value is safe, and Err(iffi::Error) otherwise.
source§

impl Iffi<MaybeInvalid<NonZeroI64>> for NonZeroI64

source§

fn can_transmute(superset: &MaybeInvalid<NonZeroI64>) -> Result<(), Error>

Checks for the safety of transmuting U into Self. Returns Ok(()) if the value is safe, and Err(iffi::Error) otherwise.
source§

impl Iffi<MaybeInvalid<NonZeroI8>> for NonZeroI8

source§

fn can_transmute(superset: &MaybeInvalid<NonZeroI8>) -> Result<(), Error>

Checks for the safety of transmuting U into Self. Returns Ok(()) if the value is safe, and Err(iffi::Error) otherwise.
source§

impl Iffi<MaybeInvalid<NonZeroIsize>> for NonZeroIsize

source§

fn can_transmute(superset: &MaybeInvalid<NonZeroIsize>) -> Result<(), Error>

Checks for the safety of transmuting U into Self. Returns Ok(()) if the value is safe, and Err(iffi::Error) otherwise.
source§

impl Iffi<MaybeInvalid<NonZeroU128>> for NonZeroU128

source§

fn can_transmute(superset: &MaybeInvalid<NonZeroU128>) -> Result<(), Error>

Checks for the safety of transmuting U into Self. Returns Ok(()) if the value is safe, and Err(iffi::Error) otherwise.
source§

impl Iffi<MaybeInvalid<NonZeroU16>> for NonZeroU16

source§

fn can_transmute(superset: &MaybeInvalid<NonZeroU16>) -> Result<(), Error>

Checks for the safety of transmuting U into Self. Returns Ok(()) if the value is safe, and Err(iffi::Error) otherwise.
source§

impl Iffi<MaybeInvalid<NonZeroU32>> for NonZeroU32

source§

fn can_transmute(superset: &MaybeInvalid<NonZeroU32>) -> Result<(), Error>

Checks for the safety of transmuting U into Self. Returns Ok(()) if the value is safe, and Err(iffi::Error) otherwise.
source§

impl Iffi<MaybeInvalid<NonZeroU64>> for NonZeroU64

source§

fn can_transmute(superset: &MaybeInvalid<NonZeroU64>) -> Result<(), Error>

Checks for the safety of transmuting U into Self. Returns Ok(()) if the value is safe, and Err(iffi::Error) otherwise.
source§

impl Iffi<MaybeInvalid<NonZeroU8>> for NonZeroU8

source§

fn can_transmute(superset: &MaybeInvalid<NonZeroU8>) -> Result<(), Error>

Checks for the safety of transmuting U into Self. Returns Ok(()) if the value is safe, and Err(iffi::Error) otherwise.
source§

impl Iffi<MaybeInvalid<NonZeroUsize>> for NonZeroUsize

source§

fn can_transmute(superset: &MaybeInvalid<NonZeroUsize>) -> Result<(), Error>

Checks for the safety of transmuting U into Self. Returns Ok(()) if the value is safe, and Err(iffi::Error) otherwise.
source§

impl<T> Nicheless for MaybeInvalid<T>

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<U> Iffi<MaybeInvalid<U>> for Uwhere U: Nicheless,

source§

fn can_transmute(_: &MaybeInvalid<U>) -> Result<(), Error>

Checks for the safety of transmuting U into Self. Returns Ok(()) if the value is safe, and Err(iffi::Error) otherwise.
source§

impl<U> Iffi<U> for Uwhere U: Nicheless,

source§

fn can_transmute(_: &U) -> Result<(), Error>

Checks for the safety of transmuting U into Self. Returns Ok(()) if the value is safe, and Err(iffi::Error) otherwise.
source§

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

const: unstable · 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.