[−][src]Enum lain::types::UnsafeEnum
Represents an enum that can contain unsafe values.
These are enums which may potentially be used as indices, offsets, or used in some other
calculation. This wrapper type exists since the Rust compiler makes strong assumptions about how
enums are used, and if you attempt to unsafely (either through a union or pointers) set the
value of an enum to an indiscriminant value, you will regularly hit issues with illegal
instructions being executed while in debug mode. See, Rust will emit certain LLVM IR code like
unreachable;
to give LLVM certain hints. The problem is that Rust believes (and rightfully so)
that enums have discrete values unless they are programmed to contain custom discriminant
values. So if you have ane num like:
enum MyEnum { Foo = 1, Bar, Baz, // ... }
Rust expects in some scenarios that all possible values have been accounted for so the following is emitted:
let my_enum_instance = MyEnum::Foo; match my_enum_instance { MyEnum::Foo | MyEnum::Bar | MyEnum::Baz => println!("All possibilities accounted for :)"), // your code _ => unreachable(), // the compiler will insert this branch in some scenarios }
But what if you set the value of your instance to something other than 1, 2, or 3 via unsafe
code? That unreachable()
block is hit in debug builds only and suddenly your code doesn't
work. In release mode, sometimes the _
(default) path is actually used to hold the first item
of the enum, so your "all other values" code path actually represents a very real value.
TL;DR Rust makes too many assumptions about how enums are used to make doing unsafe things with them worthwhile. This wrapper enum works around that.
Variants
Trait Implementations
impl<T, I> BinarySerialize for UnsafeEnum<T, I> where
T: BinarySerialize,
I: BinarySerialize + Clone,
[src]
T: BinarySerialize,
I: BinarySerialize + Clone,
impl<T: Clone, I: Clone> Clone for UnsafeEnum<T, I>
[src]
fn clone(&self) -> UnsafeEnum<T, I>
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<T: Copy, I: Copy> Copy for UnsafeEnum<T, I>
[src]
impl<T: Debug, I: Debug> Debug for UnsafeEnum<T, I>
[src]
impl<T, I> Default for UnsafeEnum<T, I> where
T: Default,
[src]
T: Default,
impl<T, I> Mutatable for UnsafeEnum<T, I> where
T: ToPrimitive<Output = I>,
I: BitXor<Output = I> + NumCast + Bounded + Copy + Debug + Default + DangerousNumber<I> + Display + WrappingAdd + WrappingSub,
[src]
T: ToPrimitive<Output = I>,
I: BitXor<Output = I> + NumCast + Bounded + Copy + Debug + Default + DangerousNumber<I> + Display + WrappingAdd + WrappingSub,
type RangeType = I
fn mutate<R: Rng>(
&mut self,
mutator: &mut Mutator<R>,
_constraints: Option<&Constraints<Self::RangeType>>
)
[src]
&mut self,
mutator: &mut Mutator<R>,
_constraints: Option<&Constraints<Self::RangeType>>
)
impl<T, I> NewFuzzed for UnsafeEnum<T, I> where
T: NewFuzzed,
I: NewFuzzed<RangeType = I> + Bounded + Debug + Default,
[src]
T: NewFuzzed,
I: NewFuzzed<RangeType = I> + Bounded + Debug + Default,
type RangeType = I
fn new_fuzzed<R: Rng>(
mutator: &mut Mutator<R>,
constraints: Option<&Constraints<Self::RangeType>>
) -> Self
[src]
mutator: &mut Mutator<R>,
constraints: Option<&Constraints<Self::RangeType>>
) -> Self
impl<E, T> ToPrimitive for UnsafeEnum<E, T> where
E: ToPrimitive<Output = T>,
T: Copy,
[src]
E: ToPrimitive<Output = T>,
T: Copy,
type Output = T
fn to_primitive(&self) -> T
[src]
Auto Trait Implementations
impl<T, I> RefUnwindSafe for UnsafeEnum<T, I> where
I: RefUnwindSafe,
T: RefUnwindSafe,
I: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, I> Send for UnsafeEnum<T, I> where
I: Send,
T: Send,
I: Send,
T: Send,
impl<T, I> Sync for UnsafeEnum<T, I> where
I: Sync,
T: Sync,
I: Sync,
T: Sync,
impl<T, I> Unpin for UnsafeEnum<T, I> where
I: Unpin,
T: Unpin,
I: Unpin,
T: Unpin,
impl<T, I> UnwindSafe for UnsafeEnum<T, I> where
I: UnwindSafe,
T: UnwindSafe,
I: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
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<T> Fixup for T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> SerializedSize for T where
T: ToPrimitive<Output = U>,
[src]
T: ToPrimitive<Output = U>,
default fn serialized_size(&Self) -> usize
[src]
default fn min_nonzero_elements_size() -> usize
[src]
default fn max_default_object_size() -> usize
[src]
default fn min_enum_variant_size(&Self) -> usize
[src]
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
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<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,