[][src]Struct rfc::core::convert::transmute::options::NeglectValidity

pub struct NeglectValidity;

Partially neglect the static validity check.

By default, TransmuteFrom and TransmuteInto's methods require that all instantiations of the source type are guaranteed to be valid instantiations of the destination type. This precludes transmutations which might be valid depending on the source value:

#[derive(PromiseTransmutableFrom, PromiseTransmutableInto)]
#[repr(u8)]
enum Bool {
    True = 1,
    False = 0,
}
 
/* ⚠️ This example intentionally does not compile. */
let _ : Bool  = some_u8_value.transmute_into(); // Compile Error!

The NeglectValidity option disables this check.

By using the NeglectValidity option, you are committing to ensure dynamically source value is a valid instance of the destination type. For instance:

#[derive(PromiseTransmutableFrom, PromiseTransmutableInto)]
#[repr(u8)]
enum Bool {
    True = 1,
    False = 0,
}
 
pub trait TryIntoBool
{
    fn try_into_bool(self) -> Option<Bool>;
}
 
impl<T> TryIntoBool for T
where
    T: TransmuteInto<u8>,
    u8: TransmuteInto<Bool, NeglectValidity>
{
    fn try_into_bool(self) -> Option<Bool> {
        let val: u8 = self.transmute_into();
 
        if val > 1 {
            None
        } else {
            // Safe, because we've first verified that
            // `val` is a bit-valid instance of a boolean.
            Some(unsafe {val.unsafe_transmute_into()})
        }
    }
}

Even with NeglectValidity, the compiler will still statically reject transmutations that cannot possibly be valid:

This example deliberately fails to compile
#[derive(PromiseTransmutableInto)]
#[repr(C)] enum Foo { A = 24 }
 
#[derive(PromiseTransmutableFrom)]
#[repr(C)] enum Bar { Z = 42 }
 
let _ = <Bar as TransmuteFrom<Foo, NeglectValidity>::unsafe_transmute_from(Foo::N) // Compile error!

Trait Implementations

impl TransmuteOptions for NeglectValidity[src]

Auto Trait Implementations

impl RefUnwindSafe for NeglectValidity

impl Send for NeglectValidity

impl Sync for NeglectValidity

impl Unpin for NeglectValidity

impl UnwindSafe for NeglectValidity

Blanket Implementations

impl<Lhs, Rhs, Neglect> AlignEq<Rhs, Neglect> for Lhs where
    Lhs: AlignLtEq<Rhs, ()>,
    Neglect: TransmuteOptions,
    Rhs: AlignLtEq<Lhs, ()>, 
[src]

impl<Lhs, Rhs, Neglect> AlignLtEq<Rhs, Neglect> for Lhs where
    Neglect: TransmuteOptions,
    &'a [Lhs; 0]: for<'a> TransmuteFrom<&'a [Rhs; 0], Neglect>, 
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<Src, Dst, Neglect> CastInto<Dst, Neglect> for Src where
    Dst: CastFrom<Src, Neglect>,
    Neglect: CastOptions
[src]

impl<Neglect> CastOptions for Neglect where
    Neglect: SliceCastOptions
[src]

impl<Neglect> CastOptions for Neglect where
    Neglect: VecCastOptions
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<Neglect> SliceCastOptions for Neglect where
    Neglect: TransmuteOptions
[src]

impl<T> TransmuteFrom<T, NeglectStability> for T[src]

impl<Src, Dst, Neglect> TransmuteInto<Dst, Neglect> for Src where
    Dst: TransmuteFrom<Src, Neglect> + ?Sized,
    Neglect: TransmuteOptions,
    Src: ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<Neglect> VecCastOptions for Neglect where
    Neglect: TransmuteOptions
[src]