Skip to main content

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 = be)]
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 = be | 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.
147*/
148
149// Every public item must be documented (the `uN` aliases are the one self-
150// evident exception, allowed at their module).
151#![deny(missing_docs)]
152// On docs.rs (which sets `--cfg docsrs` and builds on nightly), annotate every
153// feature-gated item with an "Available on crate feature …" badge. A no-op on
154// stable, where `docsrs` is never set.
155#![cfg_attr(docsrs, feature(doc_cfg))]
156// `bnb` is `no_std` when built without the (default) `std` feature; `alloc` is
157// always required — the codec produces `Vec<u8>` and owns variable-length
158// payloads/error messages. The `std` feature re-enables the `std::io` ladder
159// (`StreamBitReader`/`BufSource`/`SeekReader`, `encode(writer)`).
160#![cfg_attr(not(feature = "std"), no_std)]
161
162extern crate alloc;
163// Macro-generated code references the runtime by the path `proc-macro-crate`
164// resolves — `::bnb` for the crate itself. This self-alias makes that path
165// resolve inside the crate's own modules (the lib name is `bnb`).
166extern crate self as bnb;
167
168pub mod bitstream;
169pub mod builder;
170/// Async framing — a [`tokio_util::codec`] adapter (the `tokio` feature).
171#[cfg(feature = "tokio")]
172pub mod codec;
173pub mod codecs;
174pub mod error;
175mod field;
176pub mod guide;
177pub mod int;
178/// Ergonomic `std` socket helpers — [`MessageStream`] + [`MessageDatagram`] (the `net` feature).
179#[cfg(feature = "net")]
180pub mod net;
181pub mod wirelen;
182
183pub use bitstream::{
184    BitAmount, BitBuf, BitDecode, BitEncode, BitError, BitReader, BitWriter, CapacityError,
185    DecodeWith, EncodeMode, EncodeWith, ErrorKind, FixedBitLen, Layout, SeekSource, Sink, Source,
186};
187pub use wirelen::WireLen;
188
189/// The `std::io` I/O ladder and writer conveniences — only with the (default)
190/// `std` feature. Without it, `bnb` is `no_std + alloc`: decode from a `&[u8]`
191/// (`BitReader`), encode to a `Vec<u8>` (`to_bytes`/`to_canonical_bytes`).
192#[cfg(feature = "std")]
193pub use bitstream::{BufSource, EncodeExt, SeekReader, SinkWriter, SourceReader, StreamBitReader};
194
195/// Zero-copy `bytes`-crate adapters (the `bytes` feature).
196#[cfg(feature = "bytes")]
197pub use bitstream::{BytesReader, BytesWriter};
198
199/// The async `tokio_util` codec adapter (the `tokio` feature).
200#[cfg(feature = "tokio")]
201pub use codec::BinCodec;
202
203/// Ergonomic `std` socket helpers (the `net` feature).
204#[cfg(feature = "net")]
205pub use net::{DatagramSocket, MessageDatagram, MessageStream};
206#[cfg(feature = "mock")]
207pub use net::{MockDatagramSocket, MockStream};
208
209/// Common imports for the codec — the typed positioning amounts (`4.bits()`,
210/// `3.bytes()`) used by `#[br(pad_before = …)]` etc., plus the [`EncodeExt`] trait that
211/// carries `encode(writer)` (the `std` feature).
212pub mod prelude {
213    pub use crate::BitAmount;
214    #[cfg(feature = "std")]
215    pub use crate::EncodeExt;
216}
217pub use builder::BuilderError;
218pub use error::{Error, Result, UnknownDiscriminant};
219pub use field::{BitOrder, Bitfield, Bits, ByteOrder};
220pub use int::{UInt, *};
221
222// Re-export the macros so users depend only on `bnb`. A derive macro and a
223// trait may share a name (like `Debug`) — they live in different namespaces —
224// so `BitEnum`/`BitDecode`/`BitEncode` are each both a derive and a trait.
225pub use bnb_macros::{BitDecode, BitEncode, BitEnum, BitsBuilder, bin, bitfield, bitflags};
226
227/// Marker trait implemented by `#[derive(BitEnum)]` enums: a [`Bits`] value
228/// whose representation is an integer discriminant of a fixed width.
229pub trait BitEnum: Bits {}
230
231/// Implementation details referenced by macro-generated code. Not a stable API.
232#[doc(hidden)]
233pub mod __private {
234    /// The `std::io::Write`-based encode helpers, behind the `std` feature.
235    #[cfg(feature = "std")]
236    pub use crate::bitstream::encode_to_writer_with;
237    pub use crate::bitstream::{
238        BitDecode, BitEncode, BitError, BitReader, BitWriter, CountPrefix, FixedBitLen, Layout,
239        SeekSource, Sink, Source, align_read, align_write, bits_of, decode_all, decode_exact,
240        decode_exact_with, decode_iter, decode_mapped_msg, decode_peek, decode_peek_with,
241        decode_try_mapped_msg, encode_mapped_msg, encode_to_vec, encode_to_vec_with, peek_bytes,
242        read_byte_array, read_mapped, read_try_mapped, skip_read, skip_write, verify_magic,
243        write_byte_array, write_mapped,
244    };
245    pub use crate::error::UnknownDiscriminant;
246    pub use crate::field::{BitOrder, Bitfield, Bits, ByteOrder};
247    pub use crate::wirelen::WireLen;
248    /// Owned-collection re-exports so macro-generated code names neither `std` nor
249    /// `alloc` directly (the user crate need not declare `extern crate alloc`).
250    pub use ::alloc::{format, string::String, vec, vec::Vec};
251    /// Re-exported for the `#[br(dbg)]` directive's generated `trace!` call, so a user
252    /// of the directive needs no direct `tracing` dependency. `#[br(dbg)]` is a
253    /// `std`-only debugging aid (see the `tracing` dependency note in `Cargo.toml`).
254    #[cfg(feature = "std")]
255    pub use ::tracing;
256
257    /// Adaptive byte-buffer formatter for the `#[bin]` `#[try_str]` field hint: renders a
258    /// buffer that is **valid UTF-8** as a quoted, escaped string (`"hello"`, `"a\nb"`) and any
259    /// buffer with a non-UTF-8 byte as **hex bytes** (`[de, ad, be, ef]`). All-or-nothing — it
260    /// never replaces bytes (no lossy `�`), so the rendering can't misrepresent the wire.
261    pub struct TryStr<'a, T: ?Sized>(pub &'a T);
262
263    impl<T: AsRef<[u8]> + ?Sized> ::core::fmt::Debug for TryStr<'_, T> {
264        fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
265            let bytes = self.0.as_ref();
266            match ::core::str::from_utf8(bytes) {
267                ::core::result::Result::Ok(s) => ::core::fmt::Debug::fmt(s, f),
268                ::core::result::Result::Err(_) => write!(f, "{bytes:02x?}"),
269            }
270        }
271    }
272}