#bit-field #layout #serialization

no-std sorbit

A serialization framework to define bit-accurate binary formats

1 unstable release

Uses new Rust 2024

0.1.0 Mar 1, 2026

#811 in Encoding

MIT license

145KB
2.5K SLoC

Sorbit

Language License Build & test

Notice: the API is not yet stable.

Sorbit is a binary serialization framework that gives you complete control over the layout of the serialized data. Sorbit helps you define network packets, firmware messages, or other binary data structures that are governed by an external specification.

For further documentation, head to docs.rs.

Example

You can define the layout of data structure using attributes. This example shows the "inquiry" data format from the SCSI (hard drives, etc.) standard:

use sorbit::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
#[sorbit(byte_order=big_endian)]
struct Inquiry {
    #[sorbit(bit_field=_byte0, repr=u8, bit_numbering=LSB0)]
    #[sorbit(bits=5..=7)]
    peripheral_qualifier: u8,
    #[sorbit(bit_field=_byte0, bits=0..=4)]
    peripheral_device_type: u8,

    #[sorbit(bit_field=_byte1, repr=u8, bit_numbering=LSB0, bits=7)]
    rmb: bool,

    version: u8,

    #[sorbit(bit_field=_byte3, repr=u8, bit_numbering=LSB0)]
    #[sorbit(bits = 5)]
    norm_aca: bool,
    #[sorbit(bit_field=_byte3, bits=4)]
    hi_sup: bool,
    #[sorbit(bit_field=_byte3, bits=0..=3)]
    response_data_format: u8,

    additional_length: u8,

    // The rest of the members hidden for brevity.
    // ...
}

You can then convert the structure into bytes that can be sent to the recipient (in this case a SCSI hard drive):

use sorbit::io::FixedMemoryStream;
use sorbit::serialize::{Serialize, StreamSerializer};

let inquiry = Inquiry::default();
let mut buffer = [0u8; 36];
let stream = FixedMemoryStream::new(&mut buffer); // no_std fixed size stream.
let mut serializer = StreamSerializer::new(stream);
inquiry.serialize(&mut serializer).unwrap();

License

Sorbit is distributed under the MIT license, like most Rust libraries. Feel free to use sorbit both commercially and non-commercially.

Dependencies

~6MB
~93K SLoC