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>
impl<T> MaybeInvalid<T>
sourcepub fn as_bytes(&self) -> &[u8] ⓘ
pub fn as_bytes(&self) -> &[u8] ⓘ
Gets a shared reference to the value in this container as a byte slice.
sourcepub unsafe fn assume_valid(self) -> T
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() };
sourcepub unsafe fn assume_valid_ref(&self) -> &T
pub unsafe fn assume_valid_ref(&self) -> &T
sourcepub unsafe fn assume_valid_mut(&mut self) -> &mut T
pub unsafe fn assume_valid_mut(&mut self) -> &mut T
source§impl<T: Nicheless> MaybeInvalid<T>
impl<T: Nicheless> MaybeInvalid<T>
sourcepub fn into_inner(self) -> T
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)
sourcepub fn get(&self) -> &T
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
.
sourcepub fn get_mut(&mut self) -> &mut T
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>
impl<T> Debug for MaybeInvalid<T>
source§impl Iffi<MaybeInvalid<NonZeroI128>> for NonZeroI128
impl Iffi<MaybeInvalid<NonZeroI128>> for NonZeroI128
source§fn can_transmute(superset: &MaybeInvalid<NonZeroI128>) -> Result<(), Error>
fn can_transmute(superset: &MaybeInvalid<NonZeroI128>) -> Result<(), Error>
U
into Self
.
Returns Ok(())
if the value is safe, and Err(iffi::Error)
otherwise.source§impl Iffi<MaybeInvalid<NonZeroI16>> for NonZeroI16
impl Iffi<MaybeInvalid<NonZeroI16>> for NonZeroI16
source§fn can_transmute(superset: &MaybeInvalid<NonZeroI16>) -> Result<(), Error>
fn can_transmute(superset: &MaybeInvalid<NonZeroI16>) -> Result<(), Error>
U
into Self
.
Returns Ok(())
if the value is safe, and Err(iffi::Error)
otherwise.source§impl Iffi<MaybeInvalid<NonZeroI32>> for NonZeroI32
impl Iffi<MaybeInvalid<NonZeroI32>> for NonZeroI32
source§fn can_transmute(superset: &MaybeInvalid<NonZeroI32>) -> Result<(), Error>
fn can_transmute(superset: &MaybeInvalid<NonZeroI32>) -> Result<(), Error>
U
into Self
.
Returns Ok(())
if the value is safe, and Err(iffi::Error)
otherwise.source§impl Iffi<MaybeInvalid<NonZeroI64>> for NonZeroI64
impl Iffi<MaybeInvalid<NonZeroI64>> for NonZeroI64
source§fn can_transmute(superset: &MaybeInvalid<NonZeroI64>) -> Result<(), Error>
fn can_transmute(superset: &MaybeInvalid<NonZeroI64>) -> Result<(), Error>
U
into Self
.
Returns Ok(())
if the value is safe, and Err(iffi::Error)
otherwise.source§impl Iffi<MaybeInvalid<NonZeroI8>> for NonZeroI8
impl Iffi<MaybeInvalid<NonZeroI8>> for NonZeroI8
source§fn can_transmute(superset: &MaybeInvalid<NonZeroI8>) -> Result<(), Error>
fn can_transmute(superset: &MaybeInvalid<NonZeroI8>) -> Result<(), Error>
U
into Self
.
Returns Ok(())
if the value is safe, and Err(iffi::Error)
otherwise.source§impl Iffi<MaybeInvalid<NonZeroIsize>> for NonZeroIsize
impl Iffi<MaybeInvalid<NonZeroIsize>> for NonZeroIsize
source§fn can_transmute(superset: &MaybeInvalid<NonZeroIsize>) -> Result<(), Error>
fn can_transmute(superset: &MaybeInvalid<NonZeroIsize>) -> Result<(), Error>
U
into Self
.
Returns Ok(())
if the value is safe, and Err(iffi::Error)
otherwise.source§impl Iffi<MaybeInvalid<NonZeroU128>> for NonZeroU128
impl Iffi<MaybeInvalid<NonZeroU128>> for NonZeroU128
source§fn can_transmute(superset: &MaybeInvalid<NonZeroU128>) -> Result<(), Error>
fn can_transmute(superset: &MaybeInvalid<NonZeroU128>) -> Result<(), Error>
U
into Self
.
Returns Ok(())
if the value is safe, and Err(iffi::Error)
otherwise.source§impl Iffi<MaybeInvalid<NonZeroU16>> for NonZeroU16
impl Iffi<MaybeInvalid<NonZeroU16>> for NonZeroU16
source§fn can_transmute(superset: &MaybeInvalid<NonZeroU16>) -> Result<(), Error>
fn can_transmute(superset: &MaybeInvalid<NonZeroU16>) -> Result<(), Error>
U
into Self
.
Returns Ok(())
if the value is safe, and Err(iffi::Error)
otherwise.source§impl Iffi<MaybeInvalid<NonZeroU32>> for NonZeroU32
impl Iffi<MaybeInvalid<NonZeroU32>> for NonZeroU32
source§fn can_transmute(superset: &MaybeInvalid<NonZeroU32>) -> Result<(), Error>
fn can_transmute(superset: &MaybeInvalid<NonZeroU32>) -> Result<(), Error>
U
into Self
.
Returns Ok(())
if the value is safe, and Err(iffi::Error)
otherwise.source§impl Iffi<MaybeInvalid<NonZeroU64>> for NonZeroU64
impl Iffi<MaybeInvalid<NonZeroU64>> for NonZeroU64
source§fn can_transmute(superset: &MaybeInvalid<NonZeroU64>) -> Result<(), Error>
fn can_transmute(superset: &MaybeInvalid<NonZeroU64>) -> Result<(), Error>
U
into Self
.
Returns Ok(())
if the value is safe, and Err(iffi::Error)
otherwise.source§impl Iffi<MaybeInvalid<NonZeroU8>> for NonZeroU8
impl Iffi<MaybeInvalid<NonZeroU8>> for NonZeroU8
source§fn can_transmute(superset: &MaybeInvalid<NonZeroU8>) -> Result<(), Error>
fn can_transmute(superset: &MaybeInvalid<NonZeroU8>) -> Result<(), Error>
U
into Self
.
Returns Ok(())
if the value is safe, and Err(iffi::Error)
otherwise.source§impl Iffi<MaybeInvalid<NonZeroUsize>> for NonZeroUsize
impl Iffi<MaybeInvalid<NonZeroUsize>> for NonZeroUsize
source§fn can_transmute(superset: &MaybeInvalid<NonZeroUsize>) -> Result<(), Error>
fn can_transmute(superset: &MaybeInvalid<NonZeroUsize>) -> Result<(), Error>
U
into Self
.
Returns Ok(())
if the value is safe, and Err(iffi::Error)
otherwise.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<U> Iffi<MaybeInvalid<U>> for Uwhere
U: Nicheless,
impl<U> Iffi<MaybeInvalid<U>> for Uwhere U: Nicheless,
source§fn can_transmute(_: &MaybeInvalid<U>) -> Result<(), Error>
fn can_transmute(_: &MaybeInvalid<U>) -> Result<(), Error>
U
into Self
.
Returns Ok(())
if the value is safe, and Err(iffi::Error)
otherwise.source§impl<U> Iffi<U> for Uwhere
U: Nicheless,
impl<U> Iffi<U> for Uwhere U: Nicheless,
source§fn can_transmute(_: &U) -> Result<(), Error>
fn can_transmute(_: &U) -> Result<(), Error>
U
into Self
.
Returns Ok(())
if the value is safe, and Err(iffi::Error)
otherwise.