Crate collum

Source
Expand description

§Collum

Collum’s goal is to make parsing packed bit-level protocols as easy as describing the data structure, just like we would do in C; except in this case we get to use the beauty of Rust’s design and not an ambiguous and unsafe cast.

To go over some of collum’s bit-level parsing capabilities we’ll go over an example of parsing 2-bit integers from byte-slices. We’ll simply define the two bit integer as a tuple struct with two booleans (true or false representing the bit state).


#[derive(Collum, Sized, Debug, PartialEq)]
struct U2(bool, bool);

let bits = BitSlice::from(&[0b11000110]);

let (first4bits, bits) = <[U2; 2]>::take_from_bit_slice(bits)?;
assert_eq!(first4bits, [U2(true, true), U2(false, false)]);

let (last4bits, bits) = <[U2; 2]>::take_from_bit_slice(bits)?;
assert_eq!(last4bits, [U2(false, true), U2(true, false)]);

// `bits` gives the remaining size of the bit slice.
assert_eq!(bits.bits(), 0);

Modules§

endian
A sub-module exposing type information for endianness. In most cases defining the type using the type itself is not needed and using #[collum(endian(..))] should suffice.

Structs§

BitSlice
A borrowed [[u8]] offsetable at bit-precision.

Enums§

BitOffset
Safely defines an offset within a single u8, such that any variant will denote a bit within a u8 value.

Traits§

Collum
Trait for a type parsable from a BitSlice while returning the left of the slice.
Sized
Similarly to core::marker::Sized defines a type which can be said to have a given amount of bits.