pub trait FromBits<B>: Sized {
    type Error: Display;

    const BITS: u32;

    // Required methods
    fn try_from_bits(bits: B) -> Result<Self, Self::Error>;
    fn into_bits(self) -> B;
}
Expand description

Trait implemented by values which can be converted to and from raw bits.

This trait is implemented by default for all signed and unsigned integer types, as well as for bools. It can be implemented manually for any user-defined type which has a well-defined bit-pattern representation. For enum types with unsigned integer reprs, it may also be implemented automatically using the enum_from_bits! macro.

Required Associated Types§

source

type Error: Display

The error type returned by Self::try_from_bits when an invalid bit pattern is encountered.

If all bit patterns possible in Self::BITS bits are valid bit patterns for a Self-typed value, this should generally be core::convert::Infallible.

Required Associated Constants§

source

const BITS: u32

The number of bits required to represent a value of this type.

Required Methods§

source

fn try_from_bits(bits: B) -> Result<Self, Self::Error>

Attempt to convert bits into a value of this type.

Returns
  • Ok(Self) if bits contained a valid bit pattern for a value of this type.
  • Err(Self::Error) if bits is an invalid bit pattern for a value of this type.
source

fn into_bits(self) -> B

Convert self into a raw bit representation.

In general, this will be a low-cost conversion (e.g., for enums, this is generally an as cast).

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl FromBits<u8> for bool

source§

const BITS: u32 = 1u32

§

type Error = Infallible

source§

fn try_from_bits(f: u8) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u8

source§

impl FromBits<u8> for i8

source§

const BITS: u32 = 8u32

§

type Error = Infallible

source§

fn try_from_bits(f: u8) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u8

source§

impl FromBits<u8> for u8

source§

const BITS: u32 = 8u32

§

type Error = Infallible

source§

fn try_from_bits(f: u8) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u8

source§

impl FromBits<u16> for bool

source§

const BITS: u32 = 1u32

§

type Error = Infallible

source§

fn try_from_bits(f: u16) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u16

source§

impl FromBits<u16> for i8

source§

const BITS: u32 = 8u32

§

type Error = Infallible

source§

fn try_from_bits(f: u16) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u16

source§

impl FromBits<u16> for i16

source§

const BITS: u32 = 16u32

§

type Error = Infallible

source§

fn try_from_bits(f: u16) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u16

source§

impl FromBits<u16> for u8

source§

const BITS: u32 = 8u32

§

type Error = Infallible

source§

fn try_from_bits(f: u16) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u16

source§

impl FromBits<u16> for u16

source§

const BITS: u32 = 16u32

§

type Error = Infallible

source§

fn try_from_bits(f: u16) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u16

source§

impl FromBits<u32> for bool

source§

const BITS: u32 = 1u32

§

type Error = Infallible

source§

fn try_from_bits(f: u32) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u32

source§

impl FromBits<u32> for i8

source§

const BITS: u32 = 8u32

§

type Error = Infallible

source§

fn try_from_bits(f: u32) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u32

source§

impl FromBits<u32> for i16

source§

const BITS: u32 = 16u32

§

type Error = Infallible

source§

fn try_from_bits(f: u32) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u32

source§

impl FromBits<u32> for i32

source§

const BITS: u32 = 32u32

§

type Error = Infallible

source§

fn try_from_bits(f: u32) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u32

source§

impl FromBits<u32> for u8

source§

const BITS: u32 = 8u32

§

type Error = Infallible

source§

fn try_from_bits(f: u32) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u32

source§

impl FromBits<u32> for u16

source§

const BITS: u32 = 16u32

§

type Error = Infallible

source§

fn try_from_bits(f: u32) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u32

source§

impl FromBits<u32> for u32

source§

const BITS: u32 = 32u32

§

type Error = Infallible

source§

fn try_from_bits(f: u32) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u32

source§

impl FromBits<u64> for bool

source§

const BITS: u32 = 1u32

§

type Error = Infallible

source§

fn try_from_bits(f: u64) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u64

source§

impl FromBits<u64> for i8

source§

const BITS: u32 = 8u32

§

type Error = Infallible

source§

fn try_from_bits(f: u64) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u64

source§

impl FromBits<u64> for i16

source§

const BITS: u32 = 16u32

§

type Error = Infallible

source§

fn try_from_bits(f: u64) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u64

source§

impl FromBits<u64> for i32

source§

const BITS: u32 = 32u32

§

type Error = Infallible

source§

fn try_from_bits(f: u64) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u64

source§

impl FromBits<u64> for i64

source§

const BITS: u32 = 64u32

§

type Error = Infallible

source§

fn try_from_bits(f: u64) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u64

source§

impl FromBits<u64> for isize

source§

const BITS: u32 = 64u32

§

type Error = Infallible

source§

fn try_from_bits(f: u64) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u64

source§

impl FromBits<u64> for u8

source§

const BITS: u32 = 8u32

§

type Error = Infallible

source§

fn try_from_bits(f: u64) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u64

source§

impl FromBits<u64> for u16

source§

const BITS: u32 = 16u32

§

type Error = Infallible

source§

fn try_from_bits(f: u64) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u64

source§

impl FromBits<u64> for u32

source§

const BITS: u32 = 32u32

§

type Error = Infallible

source§

fn try_from_bits(f: u64) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u64

source§

impl FromBits<u64> for u64

source§

const BITS: u32 = 64u32

§

type Error = Infallible

source§

fn try_from_bits(f: u64) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u64

source§

impl FromBits<u64> for usize

source§

const BITS: u32 = 64u32

§

type Error = Infallible

source§

fn try_from_bits(f: u64) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u64

source§

impl FromBits<u128> for bool

source§

const BITS: u32 = 1u32

§

type Error = Infallible

source§

fn try_from_bits(f: u128) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u128

source§

impl FromBits<u128> for i8

source§

const BITS: u32 = 8u32

§

type Error = Infallible

source§

fn try_from_bits(f: u128) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u128

source§

impl FromBits<u128> for i16

source§

const BITS: u32 = 16u32

§

type Error = Infallible

source§

fn try_from_bits(f: u128) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u128

source§

impl FromBits<u128> for i32

source§

const BITS: u32 = 32u32

§

type Error = Infallible

source§

fn try_from_bits(f: u128) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u128

source§

impl FromBits<u128> for i64

source§

const BITS: u32 = 64u32

§

type Error = Infallible

source§

fn try_from_bits(f: u128) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u128

source§

impl FromBits<u128> for u8

source§

const BITS: u32 = 8u32

§

type Error = Infallible

source§

fn try_from_bits(f: u128) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u128

source§

impl FromBits<u128> for u16

source§

const BITS: u32 = 16u32

§

type Error = Infallible

source§

fn try_from_bits(f: u128) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u128

source§

impl FromBits<u128> for u32

source§

const BITS: u32 = 32u32

§

type Error = Infallible

source§

fn try_from_bits(f: u128) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u128

source§

impl FromBits<u128> for u64

source§

const BITS: u32 = 64u32

§

type Error = Infallible

source§

fn try_from_bits(f: u128) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u128

source§

impl FromBits<u128> for u128

source§

const BITS: u32 = 128u32

§

type Error = Infallible

source§

fn try_from_bits(f: u128) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u128

source§

impl FromBits<u128> for usize

source§

const BITS: u32 = 64u32

§

type Error = Infallible

source§

fn try_from_bits(f: u128) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> u128

source§

impl FromBits<usize> for bool

source§

const BITS: u32 = 1u32

§

type Error = Infallible

source§

fn try_from_bits(f: usize) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> usize

source§

impl FromBits<usize> for i8

source§

const BITS: u32 = 8u32

§

type Error = Infallible

source§

fn try_from_bits(f: usize) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> usize

source§

impl FromBits<usize> for i16

source§

const BITS: u32 = 16u32

§

type Error = Infallible

source§

fn try_from_bits(f: usize) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> usize

source§

impl FromBits<usize> for i32

source§

const BITS: u32 = 32u32

§

type Error = Infallible

source§

fn try_from_bits(f: usize) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> usize

source§

impl FromBits<usize> for i64

source§

const BITS: u32 = 64u32

§

type Error = Infallible

source§

fn try_from_bits(f: usize) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> usize

source§

impl FromBits<usize> for isize

source§

const BITS: u32 = 64u32

§

type Error = Infallible

source§

fn try_from_bits(f: usize) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> usize

source§

impl FromBits<usize> for u8

source§

const BITS: u32 = 8u32

§

type Error = Infallible

source§

fn try_from_bits(f: usize) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> usize

source§

impl FromBits<usize> for u16

source§

const BITS: u32 = 16u32

§

type Error = Infallible

source§

fn try_from_bits(f: usize) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> usize

source§

impl FromBits<usize> for u32

source§

const BITS: u32 = 32u32

§

type Error = Infallible

source§

fn try_from_bits(f: usize) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> usize

source§

impl FromBits<usize> for u64

source§

const BITS: u32 = 64u32

§

type Error = Infallible

source§

fn try_from_bits(f: usize) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> usize

source§

impl FromBits<usize> for usize

source§

const BITS: u32 = 64u32

§

type Error = Infallible

source§

fn try_from_bits(f: usize) -> Result<Self, Self::Error>

source§

fn into_bits(self) -> usize

Implementors§

source§

impl FromBits<u64> for AnotherTestEnum

Available on trace_macros only.
source§

const BITS: u32 = 4u32

§

type Error = &'static str

source§

impl FromBits<u64> for TestEnum

Available on trace_macros only.
source§

const BITS: u32 = 2u32

§

type Error = Infallible