Enum UnsafeEnum

Source
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§

Source§

impl<T, I> BinarySerialize for UnsafeEnum<T, I>

Source§

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

Pushes all fields in self to a buffer
Source§

impl<T: Clone, I: Clone> Clone for UnsafeEnum<T, I>

Source§

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

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug, I: Debug> Debug for UnsafeEnum<T, I>

Source§

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

Formats the value using the given formatter. Read more
Source§

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

Source§

fn default() -> Self

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

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,

Source§

type RangeType = I

Source§

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

Source§

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

Source§

type RangeType = I

Source§

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

Picks a random variant of Self
Source§

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

Source§

type Output = T

Source§

fn to_primitive(&self) -> T

Source§

impl<T: Copy, I: Copy> Copy for UnsafeEnum<T, I>

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Fixup for T

Source§

default fn fixup<R>(&mut self, _mutator: &mut Mutator<R>)
where R: Rng,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

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

Source§

default fn serialized_size(&self) -> usize

Serialized size in bytes of this data type
Source§

default fn min_nonzero_elements_size() -> usize

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
Source§

default fn max_default_object_size() -> usize

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.
Source§

default fn min_enum_variant_size(&self) -> usize

Minimum size of the selected enum variant.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

fn vzip(self) -> V

Source§

impl<T> VariableSizeObject for T