[−][src]Struct rfc::core::convert::transmute::options::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]
Lhs: AlignLtEq<Rhs, ()>,
Neglect: TransmuteOptions,
Rhs: AlignLtEq<Lhs, ()>,
impl<Lhs, Rhs, Neglect> AlignLtEq<Rhs, Neglect> for Lhs where
Neglect: TransmuteOptions,
&'a [Lhs; 0]: for<'a> TransmuteFrom<&'a [Rhs; 0], Neglect>,
[src]
Neglect: TransmuteOptions,
&'a [Lhs; 0]: for<'a> TransmuteFrom<&'a [Rhs; 0], Neglect>,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<Src, Dst, Neglect> CastInto<Dst, Neglect> for Src where
Dst: CastFrom<Src, Neglect>,
Neglect: CastOptions,
[src]
Dst: CastFrom<Src, Neglect>,
Neglect: CastOptions,
fn cast_into(self) -> Dst where
Self: Sized,
Dst: Sized,
Neglect: SafeCastOptions,
[src]
Self: Sized,
Dst: Sized,
Neglect: SafeCastOptions,
unsafe fn unsafe_cast_into(self) -> Dst where
Self: Sized,
Dst: Sized,
Neglect: CastOptions,
[src]
Self: Sized,
Dst: Sized,
Neglect: CastOptions,
impl<Neglect> CastOptions for Neglect where
Neglect: SliceCastOptions,
[src]
Neglect: SliceCastOptions,
impl<Neglect> CastOptions for Neglect where
Neglect: VecCastOptions,
[src]
Neglect: VecCastOptions,
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<Neglect> SliceCastOptions for Neglect where
Neglect: TransmuteOptions,
[src]
Neglect: TransmuteOptions,
impl<T> TransmuteFrom<T, NeglectStability> for T
[src]
fn transmute_from(src: Src) -> Self where
Src: Sized,
Self: Sized,
Neglect: SafeTransmuteOptions,
[src]
Src: Sized,
Self: Sized,
Neglect: SafeTransmuteOptions,
unsafe fn unsafe_transmute_from(src: Src) -> Self where
Src: Sized,
Self: Sized,
Neglect: TransmuteOptions,
[src]
Src: Sized,
Self: Sized,
Neglect: TransmuteOptions,
impl<Src, Dst, Neglect> TransmuteInto<Dst, Neglect> for Src where
Dst: TransmuteFrom<Src, Neglect> + ?Sized,
Neglect: TransmuteOptions,
Src: ?Sized,
[src]
Dst: TransmuteFrom<Src, Neglect> + ?Sized,
Neglect: TransmuteOptions,
Src: ?Sized,
fn transmute_into(Self) -> Dst where
Neglect: SafeTransmuteOptions,
[src]
Neglect: SafeTransmuteOptions,
unsafe fn unsafe_transmute_into(Self) -> Dst where
Neglect: TransmuteOptions,
[src]
Neglect: TransmuteOptions,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<Neglect> VecCastOptions for Neglect where
Neglect: TransmuteOptions,
[src]
Neglect: TransmuteOptions,