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
Trait Implementations
impl<T, I> BinarySerialize for UnsafeEnum<T, I> where
T: BinarySerialize,
I: BinarySerialize + Clone,
[src]
impl<T, I> BinarySerialize for UnsafeEnum<T, I> where
T: BinarySerialize,
I: BinarySerialize + Clone,
[src]impl<T: Clone, I: Clone> Clone for UnsafeEnum<T, I>
[src]
impl<T: Clone, I: Clone> Clone for UnsafeEnum<T, I>
[src]fn clone(&self) -> 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]
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]
impl<T: Debug, I: Debug> Debug for UnsafeEnum<T, I>
[src]impl<T, I> Default for UnsafeEnum<T, I> where
T: Default,
[src]
impl<T, I> Default for UnsafeEnum<T, I> where
T: Default,
[src]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]
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]impl<T, I> NewFuzzed for UnsafeEnum<T, I> where
T: NewFuzzed,
I: NewFuzzed<RangeType = I> + Bounded + Debug + Default,
[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]
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]
impl<E, T> ToPrimitive for UnsafeEnum<E, T> where
E: ToPrimitive<Output = T>,
T: Copy,
[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,
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> BorrowMut<T> for T where
T: ?Sized,
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]pub fn borrow_mut(&mut self) -> &mut T
[src]
pub fn borrow_mut(&mut self) -> &mut T
[src]Mutably borrows from an owned value. Read more
impl<T, U> SerializedSize for T where
T: ToPrimitive<Output = U>,
[src]
impl<T, U> SerializedSize for T where
T: ToPrimitive<Output = U>,
[src]pub default fn serialized_size(&Self) -> usize
[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]
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]
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]
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]
impl<T> ToOwned for T where
T: Clone,
[src]type Owned = T
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
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]
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<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
pub fn vzip(self) -> V
impl<T> VariableSizeObject for T
[src]
impl<T> VariableSizeObject for T
[src]