Trait BitstructFromValue

Source
pub unsafe trait BitstructFromValue {
    type StoreType;

    const BIT_WIDTH: u32;

    // Required method
    unsafe fn from_value(value: Self::StoreType) -> Self;
}
Expand description

A trait that must be implemented for non-inline field enum types. 非内联的字段枚举类型必须实现的trait。

It is recommended to use the bitstruct_field_enum macro to generate non-inline field enum types instead of implementing them manually.

推荐使用bitstruct_field_enum宏来生成非内联的字段枚举类型。而不是手动实现。

use bitstructs::{bitstruct_field_enum};
#[bitstruct_field_enum(2)]
pub enum TwoBitEnum {
    Zero = 0,
    One = 1,
    Two = 2,
    Three = 3,
}

§Safety

  1. Must be able to handle all values from 0 to 2.pow(BIT_WIDTH) - 1.

  2. Must implement both BitstructToValue and BitstructFromValue.

  3. The StoreType must be the smallest unsigned integer type that can contain all possible values, which can be one of u8, u16, u32, u64, or u128.

  4. The operations of BitstructFromValue::from_value and BitstructToValue::to_value must be inverse of each other.

  5. The BIT_WIDTH of BitstructFromValue and BitstructToValue must be the same.

  6. 必须能处理 0 到 2.pow(BIT_WIDTH) - 1 的所有取值。

  7. 必须同时实现BitstructToValueBitstructFromValue

  8. StoreType必须是能包含所有可能取值范围的最小的无符号整数类型,可使用u8,u16,u32,u64,u128

  9. BitstructFromValue::from_valueBitstructToValue::to_value必须是互为逆操作。

  10. BitstructFromValueBitstructToValueBIT_WIDTH必须相同。

Required Associated Constants§

Required Associated Types§

Required Methods§

Source

unsafe fn from_value(value: Self::StoreType) -> Self

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§