Crate bin_proto

source ·
Expand description

Simple & fast bit-level binary co/dec in Rust.

For more information about #[derive(Protocol)] and its attributes, see Protocol.

§Example

#[derive(Debug, Protocol, PartialEq)]
#[protocol(discriminant_type = "u8")]
#[protocol(bits = 4)]
enum E {
    V1 = 1,
    #[protocol(discriminant = "4")]
    V4,
}

#[derive(Debug, Protocol, PartialEq)]
struct S {
    #[protocol(bits = 1)]
    bitflag: bool,
    #[protocol(bits = 3)]
    bitfield: u8,
    enum_: E,
    #[protocol(write_value = "self.arr.len() as u8")]
    arr_len: u8,
    #[protocol(length = "arr_len as usize")]
    arr: Vec<u8>,
    #[protocol(flexible_array_member)]
    read_to_end: Vec<u8>,
}

assert_eq!(
    S::from_bytes(&[
        0b1000_0000 // bitflag: true (1)
       | 0b101_0000 // bitfield: 5 (101)
           | 0b0001, // enum_: V1 (0001)
        0x02, // arr_len: 2
        0x21, 0x37, // arr: [0x21, 0x37]
        0x01, 0x02, 0x03, // read_to_end: [0x01, 0x02, 0x03]
    ], bin_proto::ByteOrder::BigEndian).unwrap(),
    S {
        bitflag: true,
        bitfield: 5,
        enum_: E::V1,
        arr_len: 2,
        arr: vec![0x21, 0x37],
        read_to_end: vec![0x01, 0x02, 0x03],
    }
);

Re-exports§

Enums§

Traits§

  • A trait for variable-width bit-level co/dec.
  • A bit-level equivalent of std::io::Read. An object-safe wrapper over bitstream_io::BitRead.
  • A bit-level equivalent of std::io::Write. An object-safe wrapper over bitstream_io::BitWrite.
  • A trait for variable-length types with a disjoint length prefix.
  • A trait for variable-length types without a length prefix.
  • A trait for bit-level co/dec.
  • A trait with helper functions for contextless Protocols

Type Aliases§

Derive Macros§