wire_codec/lib.rs
1//! # wire-codec
2//!
3//! A runtime-agnostic toolkit for binary framing and codec composition.
4//!
5//! `wire-codec` provides the primitives you need to build a binary protocol:
6//! zero-copy buffer cursors, variable-length integer codecs, bit-level
7//! cursors, bit-packed structs, checksums, byte stuffing, and frame
8//! extraction strategies. Its core allocates nothing on the encode or decode
9//! path (only the optional `codec` adapters grow a `bytes::BytesMut`), it
10//! has no required runtime dependencies, and compiles on `no_std` targets
11//! when the default `std` feature is disabled.
12//!
13//! # When to reach for this crate
14//!
15//! Pick `wire-codec` when you need to:
16//!
17//! - Implement a custom binary protocol over any transport that delivers
18//! contiguous byte slices (TCP, UDP, in-memory queue, shared memory,
19//! serial link).
20//! - Frame a byte stream into discrete messages using a fixed-width or
21//! varint length prefix, or a delimiter, with a bounded payload size and
22//! linear-time streaming.
23//! - Encode and decode integers compactly via LEB128 varint and zigzag.
24//! - Pack fields into a record with bit-level precision, by hand with
25//! [`BitWriter`] / [`BitReader`] or declaratively with
26//! [`packed_struct!`].
27//! - Add an integrity check (CRC-16, CRC-32, CRC-32C) to every frame, or
28//! COBS-stuff payloads for a serial link.
29//!
30//! You probably want a higher-level serialization crate (`serde`, `prost`,
31//! `bincode`) if you need schema evolution or introspection. This crate is
32//! the layer underneath.
33//!
34//! # Module map
35//!
36//! | Module | Provides |
37//! | ------ | -------- |
38//! | [`buf`] | [`ReadBuf`] and [`WriteBuf`], the zero-copy byte cursors every other module is built on. |
39//! | [`error`] | [`Error`] and [`Result`], the crate-wide error contract. |
40//! | [`traits`] | [`Encode`] and [`Decode`], the codec trait pair, with impls for primitives, arrays, tuples, and the [`Be`] / [`Le`] / [`Varint`] wrappers. |
41//! | [`varint`] | LEB128 varint for `u16`, `u32`, `u64`, `usize`, and zigzag `i32` / `i64`, with canonical (strict) decoders. |
42//! | [`zigzag`] | Signed-to-unsigned mapping for compact signed varints. |
43//! | [`bitfield`] | [`BitReader`] and [`BitWriter`] for MSB-first packed bits. |
44//! | [`packed`] | [`packed_struct!`] and the [`PackedField`][`packed::PackedField`] trait for bit-packed structs. |
45//! | [`framing`] | The [`Framer`][`framing::Framer`] trait, [`StreamDecoder`][`framing::StreamDecoder`], and the [`LengthPrefixed`][`framing::LengthPrefixed`], [`VarintPrefixed`][`framing::VarintPrefixed`], [`Delimited`][`framing::Delimited`], and [`Checksummed`][`framing::Checksummed`] strategies. |
46//! | [`checksum`] | The [`Checksum`][`checksum::Checksum`] trait and CRC-16/CCITT-FALSE, CRC-32, CRC-32C. |
47//! | [`cobs`] | Consistent Overhead Byte Stuffing. |
48//! | `codec` | `FrameCodec`, a `bytes::BytesMut` adapter with `tokio-util` and `asynchronous-codec` trait impls (optional features). |
49//!
50//! # Example
51//!
52//! Length-prefix a payload, then read it back:
53//!
54//! ```
55//! use wire_codec::WriteBuf;
56//! use wire_codec::framing::{Endian, Framer, LengthPrefixed, LengthWidth};
57//!
58//! let framer = LengthPrefixed::bounded(LengthWidth::U16, Endian::Big);
59//!
60//! let mut out = [0u8; 32];
61//! let mut buf = WriteBuf::new(&mut out);
62//! framer.write_frame(b"ping", &mut buf).unwrap();
63//! let n = buf.position();
64//!
65//! let frame = framer.next_frame(&out[..n]).unwrap().unwrap();
66//! assert_eq!(frame.payload(), b"ping");
67//! ```
68//!
69//! # Error contract
70//!
71//! Every fallible operation returns [`Result`] and leaves the cursor it was
72//! given at its original position on error. The crate's own writers
73//! ([`WriteBuf`] methods, varint encoders, every built-in framer's
74//! `write_frame`, [`cobs`], and [`packed_struct!`] encoding) also check
75//! room and validity before writing, so on error nothing past the cursor is
76//! modified either. Composites that run other code can still leave bytes
77//! behind the restored cursor: a tuple whose later element fails for a
78//! reason other than lack of room, and
79//! [`Checksummed`][`framing::Checksummed`] around a custom framer that
80//! reports no [`max_frame_len`][`framing::Framer::max_frame_len`].
81//!
82//! Malformed or hostile input never causes a panic, including on 32-bit
83//! targets, where every length taken from the wire is converted with
84//! checked arithmetic. The only runtime assertion reachable through the
85//! public API is a `debug_assert!` in [`Frame::new`][`framing::Frame::new`]
86//! that catches a custom framer reporting fewer consumed bytes than its
87//! payload. The other assertions are internal invariant checks
88//! (`debug_assert!`s in crate-private cursor helpers) and compile-time
89//! checks (the checksum width in `Checksummed::new` and the field widths in
90//! `packed_struct!`).
91//!
92//! # Stability
93//!
94//! From `1.0.0` onward, the public API is frozen. Subsequent `1.x` releases
95//! add only bug fixes, performance improvements, and non-breaking additions:
96//!
97//! - New [`Error`] variants ([`Error`] is `#[non_exhaustive]`).
98//! - New [`framing::LengthWidth`] widths ([`framing::LengthWidth`] is
99//! `#[non_exhaustive]`).
100//! - New methods on existing types, and new provided methods on traits.
101//! - Additional features behind feature flags.
102//!
103//! Any breaking change requires a `2.0` release.
104//!
105//! The minimum supported Rust version (MSRV) is 1.85. Raising it is not
106//! treated as a breaking change: it happens only in a minor release, is
107//! called out in the changelog, and is never done in a patch release.
108//!
109//! # Feature flags
110//!
111//! | Feature | Default | Effect |
112//! | ------- | ------- | ------ |
113//! | `std` | yes | Links the standard library and adds `From<Error> for std::io::Error`. Drop it for `no_std` targets; [`Error`] implements `core::error::Error` either way. |
114//! | `memchr` | no | Delimiter search through the `memchr` crate (SIMD, linear-time multi-byte search) instead of the built-in SWAR scan. |
115//! | `bytes` | no | The `codec` module: `FrameCodec` over `bytes::BytesMut`. Works without `std`. |
116//! | `tokio-util` | no | `tokio_util::codec::{Decoder, Encoder}` for `FrameCodec`. Implies `bytes` and `std`. |
117//! | `asynchronous-codec` | no | `asynchronous_codec::{Decoder, Encoder}` for `FrameCodec`. Implies `bytes` and `std`. |
118//!
119//! # License
120//!
121//! Dual-licensed under Apache-2.0 OR MIT.
122
123#![doc(html_root_url = "https://docs.rs/wire-codec")]
124#![cfg_attr(docsrs, feature(doc_cfg))]
125// Unit tests always link `std` (for `Vec`, `String`, formatting); the library
126// itself is `no_std` whenever the `std` feature is off.
127#![cfg_attr(not(any(feature = "std", test)), no_std)]
128// REPS-mandated lint set. Keep aligned with REPS.md "Compiler & Lint Configuration".
129#![deny(warnings)]
130#![deny(missing_docs)]
131#![deny(unsafe_op_in_unsafe_fn)]
132#![deny(unused_must_use)]
133#![deny(unused_results)]
134#![deny(clippy::unwrap_used)]
135#![deny(clippy::expect_used)]
136#![deny(clippy::todo)]
137#![deny(clippy::unimplemented)]
138#![deny(clippy::unreachable)]
139#![deny(clippy::print_stdout)]
140#![deny(clippy::print_stderr)]
141#![deny(clippy::dbg_macro)]
142#![deny(clippy::undocumented_unsafe_blocks)]
143#![deny(clippy::missing_safety_doc)]
144// The crate contains no `unsafe` code; `forbid` makes that machine-checked.
145#![forbid(unsafe_code)]
146
147pub mod bitfield;
148pub mod buf;
149pub mod checksum;
150pub mod cobs;
151#[cfg(feature = "bytes")]
152#[cfg_attr(docsrs, doc(cfg(feature = "bytes")))]
153pub mod codec;
154pub mod error;
155pub mod framing;
156pub mod packed;
157pub mod traits;
158pub mod varint;
159pub mod zigzag;
160
161pub use bitfield::{BitReader, BitWriter};
162pub use buf::{ReadBuf, WriteBuf};
163pub use error::{Error, Result};
164pub use traits::{Be, Decode, Encode, Le, Varint};
165
166/// Crate version string, populated by Cargo at build time.
167///
168/// # Example
169///
170/// ```
171/// assert!(wire_codec::VERSION.starts_with("1."));
172/// ```
173pub const VERSION: &str = env!("CARGO_PKG_VERSION");
174
175/// Compiles the README code blocks as doctests so they cannot drift.
176#[cfg(doctest)]
177#[doc = include_str!("../README.md")]
178pub struct ReadmeDoctests;
179
180/// Compiles the `docs/API.md` code blocks as doctests so they cannot drift.
181#[cfg(doctest)]
182#[doc = include_str!("../docs/API.md")]
183pub struct ApiDoctests;