bnb/lib.rs
1/*!
2`bnb` — an owned, bit-aware binary codec: ergonomic, fast bit/byte field types and
3the unified `#[bin]` whole-message codec.
4
5It provides, designed to *compose*:
6
7- **Arbitrary-width integers** ([`u1`]..[`u127`], via [`UInt`]) for sub-byte
8 fields — a dependency-free replacement for `arbitrary-int`.
9- **`#[bitfield]`** — an attribute macro that packs typed fields into a single
10 backing integer with **explicit, independent control of bit order**
11 (MSB/LSB-first) **and byte order** (big/little). It generates getters,
12 immutable `with_*` setters, raw access, and allocation-free `*_bytes` conversions.
13- **`#[derive(BitEnum)]`** — enum ⇄ integer at a chosen width, with an optional
14 `#[catch_all]` variant that preserves unknown values (the dual-use convention).
15- **`#[bin]`** — the unified whole-message codec (see below): reads/writes a struct
16 at arbitrary bit offsets, with a rich, `binrw`-inspired directive surface.
17
18The aim is to collapse a whole stack of overlapping helpers —
19`modular-bitfield`(`-msb`), `bitfield-struct`, `bitbybit`, `arbitrary-int`,
20`num_enum`, and a `binrw`-style codec — into one fast, integer-backed
21(shift/mask, no `bitvec`) crate. See [Inspiration](#inspiration).
22
23# Example — a DNS-style 16-bit header field
24
25```
26use bnb::{bitfield, BitEnum, u4, u5, u7};
27
28# #[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
29# struct Flags(bnb::u7);
30# impl bnb::Bits for Flags {
31# const BITS: u32 = 7;
32# fn into_bits(self) -> u128 { self.0.into_bits() }
33# fn from_bits(raw: u128) -> Self { Flags(u7::from_bits(raw)) }
34# }
35#[derive(BitEnum, Clone, Copy, Debug, PartialEq, Eq)]
36#[bit_enum(u4)]
37enum RCode {
38 NoError, // 0
39 FormErr, // 1
40 ServFail, // 2
41 #[catch_all]
42 Other(u4), // any other 4-bit value
43}
44
45// MSB-first packing (network/RFC order), big-endian on the wire.
46#[bitfield(u16, bits = msb, bytes = big)]
47#[derive(Clone, Copy, Debug, PartialEq, Eq)]
48struct State {
49 opcode: u5, // first field -> high bits
50 flags: Flags,
51 rcode: RCode, // last field -> low bits
52}
53
54let s = State::new()
55 .with_opcode(u5::new(2))
56 .with_rcode(RCode::ServFail);
57assert_eq!(s.rcode(), RCode::ServFail);
58// opcode occupies bits 11..=15 (the high 5 of the u16).
59assert_eq!(s.to_be_bytes()[0] >> 3, 2);
60```
61
62# Bit order vs. byte order
63
64These are independent knobs, which is the whole point:
65
66- `bits = msb | lsb` — does the **first** declared field land in the high or low
67 bits of the backing integer. Default: `msb` (matches RFC ASCII-art layouts).
68- `bytes = big | le` — endianness of the backing integer when serialized.
69 Default: `be`.
70
71# The `#[bin]` codec
72
73Whole-message bit-aware codec: `#[bin]` (magic/count/ctx/map/if/calc·temp/reserved/
74positioning/validate) over a `Source`/`SeekSource`/`BufSource`/`SeekReader` I/O
75ladder, with an opt-in `bytes` feature for async framing. Common field codecs —
76LEB128 varints, NUL-terminated and length-prefixed strings — ship ready-made in
77[`codecs`] (referenced via `parse_with`/`write_with`).
78
79# Feature flags & `no_std`
80
81`bnb` is `no_std`-compatible — it always needs `alloc` (the codec produces
82`Vec<u8>` and owns variable-length payloads), but not `std`. Build with
83`default-features = false` for an embedded target.
84
85- **`std`** *(default)* — the `std::io` ladder ([`StreamBitReader`], [`BufSource`],
86 [`SeekReader`], [`Source::as_read`]/[`Sink::as_write`]), the `From<std::io::Error>`
87 bridge, and the `encode(writer)` convenience ([`EncodeExt`]). The `#[br(dbg)]`
88 directive (which emits a `tracing` event) is also `std`-only.
89- **`bytes`** — the zero-copy `bytes`-crate adapters; implies `std` (async/tokio framing).
90- **`tokio`** — [`BinCodec`], a `tokio_util::codec` `Decoder`/`Encoder` for any `#[bin]`
91 message: `Framed::new(tcp, BinCodec::<T>::new())` (a stream) or `UdpFramed::new(udp, …)` (a
92 datagram `Stream + Sink` of `(T, addr)`) — one codec, both async transports. Implies `bytes`.
93 This **is** bnb's async support: a native async `Source`/`Sink` family is deliberately out of
94 scope (the codec is in-memory and fast; framing is the async boundary, and `BinCodec` covers it).
95- **`net`** — ergonomic `std` socket helpers: [`MessageStream`] (whole-message read/write over
96 any `Read + Write`, e.g. a `TcpStream`, no `try_clone`) and [`MessageDatagram`] (`send_message`/
97 `recv_message` over a sealed [`DatagramSocket`] — `UdpSocket` or `UnixDatagram`). Implies `std`.
98- **`mock`** — test-only in-memory transports for exercising `net` code without a real socket:
99 [`MockDatagramSocket`] (a [`DatagramSocket`]) and [`MockStream`] (a `Read + Write`, with chunked
100 delivery to drive the read-more path). Put it in your `[dev-dependencies]`. Implies `net`.
101
102Without `std` you still get the full macro surface plus: decode from a `&[u8]`
103([`BitReader`], `Type::decode`/`decode_all`/`decode_iter`/`decode_exact`/`peek`), encode to a
104`Vec<u8>` (`Type::to_bytes`/`to_canonical_bytes`, or [`BitEncode::bit_encode`] over a [`Sink`]),
105and **incremental framing with [`BitBuf`]** — push bytes from any transport (a UART ISR, a
106radio, a channel), pull whole messages; [`BitBuf::bounded`] gives an alloc-once fixed
107footprint. You lose only the streaming `std::io` adapters and `encode(&mut impl Write)`; on
108`no_std`, encode with `to_bytes()` and write the bytes to your transport yourself. That is the
109deliberate `no_std` boundary: streaming reader adapters without `std` are out of scope for 1.0
110(if demanded later, they'd arrive as additive adapters over the ecosystem's `embedded-io`
111traits — see `ROADMAP.md`).
112
113# Inspiration
114
115`bnb` stands on the shoulders of several excellent crates, collapsing their
116capabilities into one: the arbitrary-width integers of `arbitrary-int`; the
117bitfield packing of `modular-bitfield`, `bitfield-struct`, and `bitbybit`; the
118enum ⇄ integer mapping of `num_enum`; and — most of all — the declarative,
119bidirectional codec design of [`binrw`](https://github.com/jam1garner/binrw),
120whose `#[br]`/`#[bw]` attribute vocabulary `#[bin]` deliberately echoes so the two
121feel like one toolkit. `bnb` shares no code with these crates; it is a from-scratch
122implementation, extended to do the one thing a byte-oriented `Read + Seek` codec
123cannot: read and write fields at arbitrary **bit** offsets. See `ACKNOWLEDGMENTS.md`
124for the full credit.
125
126# Guide
127
128The [`guide`] module is a set of worked, runnable walkthroughs — start there for a
129tour of the crate and the rationale behind each piece. Reading order:
130
1311. [`guide::quick_start`] — a five-minute tour of every macro.
1322. [`guide::numbers`] — the arbitrary-width integers (`u1`..`u127`) and the [`Bits`]
133 trait that lets everything compose.
1343. [`guide::bitfields`] — `#[bitfield]`: bit order, byte order, widths and ranges,
135 nesting.
1364. [`guide::enums`] — `#[derive(BitEnum)]`: catch-all vs. closed, `num_enum` parity.
1375. [`guide::flags`] — `#[bitflags]`: single-bit flag sets with set algebra.
1386. [`guide::builders`] — `#[derive(BitsBuilder)]`: the required-by-default builder.
1397. [`guide::bin_codec`] — `#[bin]`: a whole protocol header, end to end.
1408. [`guide::directives`] — the field-directive reference, one example each.
1419. [`guide::mapping`] — `#[bin(map/bw_map = …)]`: a whole struct mapped to/from a wire type.
14210. [`guide::dispatch`] — `#[bin]` on an enum: tagged-union dispatch by wire `magic` or off-wire `tag`.
14311. [`guide::io`] — the `Source`/`Sink` I/O ladder (slice, stream, socket, file, `bytes`).
14412. [`guide::errors`] — position-aware errors and the streaming `Incomplete` signal.
14513. [`guide::dual_use`] — the compliant-by-default-but-violatable philosophy.
14614. [`guide::composition`] — how the pieces nest and size each other.
14715. [`guide::migrating`] — coming from `binrw` / `modular-bitfield` / `num_enum`.
148*/
149
150// Every public item must be documented (the `uN` aliases are the one self-
151// evident exception, allowed at their module).
152#![forbid(unsafe_code)]
153#![deny(missing_docs)]
154// On docs.rs (which sets `--cfg docsrs` and builds on nightly), annotate every
155// feature-gated item with an "Available on crate feature …" badge. A no-op on
156// stable, where `docsrs` is never set.
157#![cfg_attr(docsrs, feature(doc_cfg))]
158// `bnb` is `no_std` when built without the (default) `std` feature; `alloc` is
159// always required — the codec produces `Vec<u8>` and owns variable-length
160// payloads/error messages. The `std` feature re-enables the `std::io` ladder
161// (`StreamBitReader`/`BufSource`/`SeekReader`, `encode(writer)`).
162#![cfg_attr(not(feature = "std"), no_std)]
163
164extern crate alloc;
165// Macro-generated code references the runtime by the path `proc-macro-crate`
166// resolves — `::bnb` for the crate itself. This self-alias makes that path
167// resolve inside the crate's own modules (the lib name is `bnb`).
168extern crate self as bnb;
169
170pub mod bitstream;
171pub mod builder;
172pub mod codecs;
173pub mod error;
174mod field;
175/// Async framing — a [`tokio_util::codec`] adapter (the `tokio` feature).
176#[cfg(feature = "tokio")]
177pub mod framing;
178pub mod guide;
179pub mod int;
180/// Ergonomic `std` socket helpers — [`MessageStream`] + [`MessageDatagram`] (the `net` feature).
181#[cfg(feature = "net")]
182pub mod net;
183mod wirelen;
184
185pub use bitstream::{
186 BitAmount, BitBuf, BitDecode, BitEncode, BitError, BitReader, BitWriter, CapacityError,
187 DecodeWith, EncodeWith, ErrorKind, FixedBitLen, Layout, SeekSource, Sink, Source,
188};
189pub use wirelen::WireLen;
190
191/// The `std::io` I/O ladder and writer conveniences — only with the (default)
192/// `std` feature. Without it, `bnb` is `no_std + alloc`: decode from a `&[u8]`
193/// (`BitReader`), encode to a `Vec<u8>` (`to_bytes`/`to_canonical_bytes`).
194#[cfg(feature = "std")]
195pub use bitstream::{BufSource, EncodeExt, SeekReader, SinkWriter, SourceReader, StreamBitReader};
196
197/// Zero-copy `bytes`-crate adapters (the `bytes` feature).
198#[cfg(feature = "bytes")]
199pub use bitstream::{BytesReader, BytesWriter};
200
201/// The async `tokio_util` codec adapter (the `tokio` feature).
202#[cfg(feature = "tokio")]
203pub use framing::BinCodec;
204
205/// Ergonomic `std` socket helpers (the `net` feature).
206#[cfg(feature = "net")]
207pub use net::{DatagramSocket, MessageDatagram, MessageStream};
208#[cfg(feature = "mock")]
209pub use net::{MockDatagramSocket, MockStream};
210
211/// Common imports for the codec — the typed positioning amounts (`4.bits()`,
212/// `3.bytes()`) used by `#[br(pad_before = …)]` etc., plus the [`EncodeExt`] trait that
213/// carries `encode(writer)` (the `std` feature).
214pub mod prelude {
215 pub use crate::BitAmount;
216 #[cfg(feature = "std")]
217 pub use crate::EncodeExt;
218}
219pub use builder::BuilderError;
220pub use error::{UnknownDiscriminant, WidthError};
221pub use field::{BitOrder, Bitfield, Bits, ByteOrder};
222pub use int::{UInt, *};
223
224// Re-export the macros so users depend only on `bnb`. A derive macro and a
225// trait may share a name (like `Debug`) — they live in different namespaces —
226// so `BitEnum`/`BitDecode`/`BitEncode` are each both a derive and a trait.
227pub use bnb_macros::{BitDecode, BitEncode, BitEnum, BitsBuilder, bin, bitfield, bitflags};
228
229/// Marker trait implemented by `#[derive(BitEnum)]` enums: a [`Bits`] value
230/// whose representation is an integer discriminant of a fixed width.
231pub trait BitEnum: Bits {}
232
233/// Implementation details referenced by macro-generated code. Not a stable API.
234#[doc(hidden)]
235pub mod __private {
236 /// The `std::io::Write`-based encode helpers, behind the `std` feature.
237 #[cfg(feature = "std")]
238 pub use crate::bitstream::encode_to_writer_with;
239 pub use crate::bitstream::{
240 BitDecode, BitEncode, BitError, BitReader, BitWriter, CountPrefix, FixedBitLen, Layout,
241 SeekSource, Sink, Source, align_read, align_write, bits_of, decode_all, decode_exact,
242 decode_exact_with, decode_iter, decode_mapped_msg, decode_peek, decode_peek_with,
243 decode_try_mapped_msg, encode_mapped_msg, encode_to_vec, encode_to_vec_with, peek_bytes,
244 read_byte_array, read_mapped, read_try_mapped, skip_read, skip_write, verify_magic,
245 write_byte_array, write_mapped,
246 };
247 pub use crate::error::UnknownDiscriminant;
248 pub use crate::field::{BitOrder, Bitfield, Bits, ByteOrder};
249 pub use crate::wirelen::WireLen;
250 /// Owned-collection re-exports so macro-generated code names neither `std` nor
251 /// `alloc` directly (the user crate need not declare `extern crate alloc`).
252 pub use ::alloc::{format, string::String, vec, vec::Vec};
253 /// Re-exported for the `#[br(dbg)]` directive's generated `trace!` call, so a user
254 /// of the directive needs no direct `tracing` dependency. `#[br(dbg)]` is a
255 /// `std`-only debugging aid (see the `tracing` dependency note in `Cargo.toml`).
256 #[cfg(feature = "std")]
257 pub use ::tracing;
258
259 /// Adaptive byte-buffer formatter for the `#[bin]` `#[try_str]` field hint: renders a
260 /// buffer that is **valid UTF-8** as a quoted, escaped string (`"hello"`, `"a\nb"`) and any
261 /// buffer with a non-UTF-8 byte as **hex bytes** (`[de, ad, be, ef]`). All-or-nothing — it
262 /// never replaces bytes (no lossy `�`), so the rendering can't misrepresent the wire.
263 pub struct TryStr<'a, T: ?Sized>(pub &'a T);
264
265 impl<T: AsRef<[u8]> + ?Sized> ::core::fmt::Debug for TryStr<'_, T> {
266 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
267 let bytes = self.0.as_ref();
268 match ::core::str::from_utf8(bytes) {
269 ::core::result::Result::Ok(s) => ::core::fmt::Debug::fmt(s, f),
270 ::core::result::Result::Err(_) => write!(f, "{bytes:02x?}"),
271 }
272 }
273 }
274}