Enum lain::types::UnsafeEnum[][src]

pub enum UnsafeEnum<T, I> {
    Valid(T),
    Invalid(I),
}
Expand description

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

Valid(T)
Invalid(I)

Trait Implementations

impl<T, I> BinarySerialize for UnsafeEnum<T, I> where
    T: BinarySerialize,
    I: BinarySerialize + Clone
[src]

default fn binary_serialize<W: Write, E: ByteOrder>(
    &self,
    buffer: &mut W
) -> usize
[src]

Pushes all fields in self to a buffer

impl<T: Clone, I: Clone> Clone for UnsafeEnum<T, I>[src]

fn clone(&self) -> UnsafeEnum<T, I>[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<T: Debug, I: Debug> Debug for UnsafeEnum<T, I>[src]

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

Formats the value using the given formatter. Read more

impl<T, I> Default for UnsafeEnum<T, I> where
    T: Default
[src]

fn default() -> Self[src]

Returns the “default value” for a type. Read more

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]

type RangeType = I

fn mutate<R: Rng>(
    &mut self,
    mutator: &mut Mutator<R>,
    _constraints: Option<&Constraints<Self::RangeType>>
)
[src]

impl<T, I> NewFuzzed for UnsafeEnum<T, I> where
    T: NewFuzzed,
    I: NewFuzzed<RangeType = I> + Bounded + Debug + Default
[src]

type RangeType = I

fn new_fuzzed<R: Rng>(
    mutator: &mut Mutator<R>,
    constraints: Option<&Constraints<Self::RangeType>>
) -> Self
[src]

Picks a random variant of Self

impl<E, T> ToPrimitive for UnsafeEnum<E, T> where
    E: ToPrimitive<Output = T>,
    T: Copy
[src]

type Output = T

fn to_primitive(&self) -> T[src]

impl<T: Copy, I: Copy> Copy for UnsafeEnum<T, I>[src]

Auto Trait Implementations

impl<T, I> RefUnwindSafe for UnsafeEnum<T, I> where
    I: RefUnwindSafe,
    T: RefUnwindSafe

impl<T, I> Send for UnsafeEnum<T, I> where
    I: Send,
    T: Send

impl<T, I> Sync for UnsafeEnum<T, I> where
    I: Sync,
    T: Sync

impl<T, I> Unpin for UnsafeEnum<T, I> where
    I: Unpin,
    T: Unpin

impl<T, I> UnwindSafe for UnsafeEnum<T, I> where
    I: UnwindSafe,
    T: UnwindSafe

Blanket Implementations

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

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

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

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

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

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> Fixup for T[src]

pub default fn fixup<R>(&mut Self, &mut Mutator<R>) where
    R: Rng
[src]

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

pub fn from(t: T) -> T[src]

Performs the conversion.

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

pub fn into(self) -> U[src]

Performs the conversion.

impl<T, U> SerializedSize for T where
    T: ToPrimitive<Output = U>, 
[src]

pub default fn serialized_size(&Self) -> usize[src]

Serialized size in bytes of this data type

pub default fn min_nonzero_elements_size() -> usize[src]

Minimum size in bytes of this data type. This is useful for determining the smallest size that a data type with a dynamic-sized member (e.g. Vec or String) may be Read more

pub default fn max_default_object_size() -> usize[src]

Maximum size in bytes of this data type with the minimum amount of elements. This is useful for determining the maximum size that a data type with a dynamic-sized member (e.g. Vec or String) may be within an enum with struct members. Read more

pub default fn min_enum_variant_size(&Self) -> usize[src]

Minimum size of the selected enum variant.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

Creates owned data from borrowed data, usually by cloning. Read more

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

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.

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

Performs the conversion.

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.

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

Performs the conversion.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>, 

pub fn vzip(self) -> V

impl<T> VariableSizeObject for T[src]

pub default fn is_variable_size() -> bool[src]