bin-proto
Simple & fast structured bit-level binary co/dec in Rust.
An improved and modernized fork of protocol. A more efficient but (slightly) less feature-rich alternative to 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 if you need
bit streams, as their BitRead and BitWrite traits are being used internally.
Example
Add this to your Cargo.toml:
[]
= "0.4"
And then define a type with the #[derive(bin_proto::Protocol)] attribute.
use ;
assert_eq!;
You can implement Protocol on your own types, and parse with context:
use Protocol;
;
;
;
WithCtx
.bytes_ctx
.unwrap;
Performance / Alternatives
This crate's main alternative is deku, and binrw for byte-level protocols.
bin-proto is significantly faster than deku in all of the tested scenarios.
The units for the below table are ns/iter, taken from
github CI.
You can find the benchmarks in the bench directory.
Read enum |
Write enum |
Read Vec |
Write Vec |
Read IPv4 header | Write IPv4 header | |
|---|---|---|---|---|---|---|
bin-proto |
21 | 93 | 738 | 821 | 151 | 141 |
deku |
67 | 227 | 3,041 | 9,705 | 2,468 | 821 |
Roadmap
The following features are planned:
- Bit/byte alignment
no_stdsupport (only afterbitstream_iosupports it)