https://github.com/wojciech-graj/bin-proto
Simple bit-level protocol definitions in Rust.
https://github.com/wojciech-graj/bin-proto
binary-protocol bit bitfield bitstream codec protocol rust
Last synced: about 2 months ago
JSON representation
Simple bit-level protocol definitions in Rust.
- Host: GitHub
- URL: https://github.com/wojciech-graj/bin-proto
- Owner: wojciech-graj
- License: mit
- Created: 2024-03-25T23:04:49.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2025-04-15T13:31:26.000Z (2 months ago)
- Last Synced: 2025-04-28T10:03:01.465Z (about 2 months ago)
- Topics: binary-protocol, bit, bitfield, bitstream, codec, protocol, rust
- Language: Rust
- Homepage:
- Size: 655 KB
- Stars: 22
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# bin-proto
[](https://crates.io/crates/bin-proto)
[](https://github.com/wojciech-graj/bin-proto/actions/workflows/ci.yml)
[](https://docs.rs/bin-proto)

[](./LICENSE.txt)Simple & fast structured bit-level binary co/dec in Rust.
An improved and modernized fork of
[protocol](https://crates.io/crates/protocol). A more efficient but (slightly)
less feature-rich alternative to [deku](https://crates.io/crates/deku).This crate adds a trait (and a custom derive for ease-of-use) that can be
implemented on types, allowing structured data to be sent and received from any
binary stream. It is recommended to use
[bitstream_io](https://docs.rs/bitstream-io/latest/bitstream_io/) if you need
bit streams, as their `BitRead` and `BitWrite` traits are being used internally.## Example
Add this to your `Cargo.toml`:
```toml
[dependencies]
bin-proto = "0.8"
```And then define a type with the `#[derive(bin_proto::BitDecode, bin_proto::BitEncode)]` attributes.
```rust
use bin_proto::{BitDecode, BitEncode, BitCodec};#[derive(Debug, BitDecode, BitEncode, PartialEq)]
#[codec(discriminant_type = u8)]
#[codec(bits = 4)]
enum E {
V1 = 1,
#[codec(discriminant = 4)]
V4,
}#[derive(Debug, BitDecode, BitEncode, PartialEq)]
struct S {
#[codec(bits = 1)]
bitflag: bool,
#[codec(bits = 3)]
bitfield: u8,
enum_: E,
#[codec(write_value = self.arr.len() as u8)]
arr_len: u8,
#[codec(tag = arr_len as usize)]
arr: Vec,
#[codec(tag_type = u16, tag_value = self.prefixed_arr.len() as u16)]
prefixed_arr: Vec,
#[codec(flexible_array_member)]
read_to_end: Vec,
}assert_eq!(
S::decode_bytes(&[
0b1000_0000 // bitflag: true (1)
| 0b101_0000 // bitfield: 5 (101)
| 0b0001, // enum_: V1 (0001)
0x02, // arr_len: 2
0x21, 0x37, // arr: [0x21, 0x37]
0x00, 0x01, 0x33, // prefixed_arr: [0x33]
0x01, 0x02, 0x03, // read_to_end: [0x01, 0x02, 0x03]
], bin_proto::BigEndian).unwrap(),
S {
bitflag: true,
bitfield: 5,
enum_: E::V1,
arr_len: 2,
arr: vec![0x21, 0x37],
prefixed_arr: vec![0x33],
read_to_end: vec![0x01, 0x02, 0x03],
}
);
```You can implement `BitEncode` and `BitDecode` on your own types, and parse with context:
```rust
use bin_proto::{BitDecode, BitEncode};pub struct Ctx;
pub struct NeedsCtx;
impl BitDecode for NeedsCtx {
fn decode(
_read: &mut R,
_ctx: &mut Ctx,
_tag: (),
) -> bin_proto::Result
where
R: bin_proto::BitRead,
E: bin_proto::Endianness,
{
// Use ctx here
Ok(Self)
}
}impl BitEncode for NeedsCtx {
fn encode(
&self,
_write: &mut W,
_ctx: &mut Ctx,
_tag: (),
) -> bin_proto::Result<()>
where
W: bin_proto::BitWrite,
E: bin_proto::Endianness,
{
// Use ctx here
Ok(())
}
}#[derive(BitDecode, BitEncode)]
#[codec(ctx = Ctx)]
pub struct WithCtx(NeedsCtx);WithCtx(NeedsCtx)
.encode_bytes_ctx(bin_proto::BigEndian, &mut Ctx, ())
.unwrap();
```## Performance / Alternatives
This crate's main alternative is [deku](https://crates.io/crates/deku), and [binrw](https://crates.io/crates/binrw) for byte-level protocols.
See [GitHub Actions](https://github.com/wojciech-graj/bin-proto/actions) for latest benchmark results with comparison against deku.