BitFieldCompatible

Trait BitFieldCompatible 

Source
pub unsafe trait BitFieldCompatible: Copy {
    const SHIFT: u32;
    const BITS_LEN: u32;

    // Required methods
    fn into_raw(self) -> u128;
    unsafe fn from_raw(v: u128) -> Self;
}
Expand description

A type that can be used as a bitfield. This is usually derived for enums. Structs and unions can implement this explicitly, as the safety requirements cannot be guaranteed for them with #[derive()] (at least not easily).

§Safety

into_raw() must provide a value within the range specified by SHIFT and BITS_LEN.

Required Associated Constants§

Source

const SHIFT: u32

The number we need to left-shift with to reach a valid value from a compressed value which has all trailing zeros trimmed.

For example, in an enum with the discriminants 0b01000, 0b10000 and 0b11000, the value of BITS_LEN will be 2 and the value of SHIFT will be 3, because we need to left-shift three times to go from 0b01, 0b10 and 0b11 to the corresponding variants.

Source

const BITS_LEN: u32

The minimum number of bits required to represent any bit pattern that is valid for this type.

Note that enums with only one variant may still have this value greater than zero if the discriminant of this variant is not zero, for example if the discriminant is 0b11 it needs two bits to be stored.

Required Methods§

Source

fn into_raw(self) -> u128

Retrieves the raw int representation of a value.

Source

unsafe fn from_raw(v: u128) -> Self

Converts a raw int representation to this type.

§Safety

v must be a valid value for this type.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§