Skip to main content

PackedField

Trait PackedField 

Source
pub trait PackedField: Sized + Copy {
    const MAX_BITS: u32;

    // Required methods
    fn to_bits(self, bits: u32) -> Result<u64>;
    fn from_bits(raw: u64, bits: u32) -> Result<Self>;
}
Expand description

A type that can be stored as a fixed-width bit field in a packed_struct!.

§Example

use wire_codec::packed::PackedField;
use wire_codec::{packed_struct, Decode, Error, ReadBuf, Result};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Kind { Data, Ack, Nak }

impl PackedField for Kind {
    const MAX_BITS: u32 = 2;
    fn to_bits(self, _bits: u32) -> Result<u64> {
        Ok(self as u64)
    }
    fn from_bits(raw: u64, _bits: u32) -> Result<Self> {
        match raw {
            0 => Ok(Kind::Data),
            1 => Ok(Kind::Ack),
            2 => Ok(Kind::Nak),
            _ => Err(Error::InvalidEncoding),
        }
    }
}

packed_struct! {
    #[derive(Debug, PartialEq, Eq)]
    struct Header {
        kind: Kind = 2,
        seq: u8 = 6,
    }
}

let header = Header::decode(&mut ReadBuf::new(&[0b01_000011])).unwrap();
assert_eq!(header, Header { kind: Kind::Ack, seq: 3 });

Required Associated Constants§

Source

const MAX_BITS: u32

The widest field, in bits, this type can be packed into.

Required Methods§

Source

fn to_bits(self, bits: u32) -> Result<u64>

Convert self into the raw contents of a bits-wide field.

§Errors

Returns Error::BitOverflow if the value does not fit in bits bits.

Source

fn from_bits(raw: u64, bits: u32) -> Result<Self>

Rebuild a value from the raw contents of a bits-wide field.

§Errors

Returns an error (typically Error::InvalidEncoding) if raw is not a valid representation.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl PackedField for bool

Source§

const MAX_BITS: u32 = 1

Source§

fn to_bits(self, _bits: u32) -> Result<u64>

Source§

fn from_bits(raw: u64, _bits: u32) -> Result<Self>

Source§

impl PackedField for i8

Source§

const MAX_BITS: u32 = i8::BITS

Source§

fn to_bits(self, bits: u32) -> Result<u64>

Source§

fn from_bits(raw: u64, bits: u32) -> Result<Self>

Source§

impl PackedField for i16

Source§

const MAX_BITS: u32 = i16::BITS

Source§

fn to_bits(self, bits: u32) -> Result<u64>

Source§

fn from_bits(raw: u64, bits: u32) -> Result<Self>

Source§

impl PackedField for i32

Source§

const MAX_BITS: u32 = i32::BITS

Source§

fn to_bits(self, bits: u32) -> Result<u64>

Source§

fn from_bits(raw: u64, bits: u32) -> Result<Self>

Source§

impl PackedField for i64

Source§

const MAX_BITS: u32 = i64::BITS

Source§

fn to_bits(self, bits: u32) -> Result<u64>

Source§

fn from_bits(raw: u64, bits: u32) -> Result<Self>

Source§

impl PackedField for u8

Source§

const MAX_BITS: u32 = u8::BITS

Source§

fn to_bits(self, bits: u32) -> Result<u64>

Source§

fn from_bits(raw: u64, _bits: u32) -> Result<Self>

Source§

impl PackedField for u16

Source§

const MAX_BITS: u32 = u16::BITS

Source§

fn to_bits(self, bits: u32) -> Result<u64>

Source§

fn from_bits(raw: u64, _bits: u32) -> Result<Self>

Source§

impl PackedField for u32

Source§

const MAX_BITS: u32 = u32::BITS

Source§

fn to_bits(self, bits: u32) -> Result<u64>

Source§

fn from_bits(raw: u64, _bits: u32) -> Result<Self>

Source§

impl PackedField for u64

Source§

const MAX_BITS: u32 = u64::BITS

Source§

fn to_bits(self, bits: u32) -> Result<u64>

Source§

fn from_bits(raw: u64, _bits: u32) -> Result<Self>

Implementors§