Skip to main content

Crate wire_codec

Crate wire_codec 

Source
Expand description

§wire-codec

A runtime-agnostic toolkit for binary framing and codec composition.

wire-codec provides the primitives you need to build a binary protocol: zero-copy buffer cursors, variable-length integer codecs, bit-level cursors, and frame extraction strategies. It allocates nothing on the encode or decode path, depends on nothing at runtime, and compiles on no_std targets when the std feature is disabled.

§When to reach for this crate

Pick wire-codec when you need to:

  • Implement a custom binary protocol over any transport that delivers contiguous byte slices (TCP, UDP, in-memory queue, shared memory, serial link).
  • Frame a byte stream into discrete messages using a length prefix or a delimiter, with bounded memory per frame.
  • Encode and decode integers compactly via LEB128 varint and zigzag.
  • Pack tagged fields into a record with bit-level precision.

You probably want a higher-level serialization crate (serde, prost, bincode) if you need a derive macro, schema evolution, or introspection. This crate is the layer underneath.

§Module map

ModuleProvides
bufReadBuf and WriteBuf, the zero-copy byte cursors every other module is built on.
errorError and Result, the crate-wide error contract.
traitsEncode and Decode, the codec trait pair.
varintUnsigned LEB128 varint for u32 and u64.
zigzagSigned-to-unsigned mapping for compact signed varints.
bitfieldBitReader and BitWriter for MSB-first packed bits.
framingThe Framer trait plus LengthPrefixed and Delimited strategies.

§Example

Length-prefix a payload, then read it back:

use wire_codec::WriteBuf;
use wire_codec::framing::{Endian, Framer, LengthPrefixed, LengthWidth};

let framer = LengthPrefixed::new(LengthWidth::U16, Endian::Big);

let mut out = [0u8; 32];
let mut buf = WriteBuf::new(&mut out);
framer.write_frame(b"ping", &mut buf).unwrap();
let n = buf.position();

let frame = framer.next_frame(&out[..n]).unwrap().unwrap();
assert_eq!(frame.payload(), b"ping");

§Stability

From 1.0.0 onward, the public API is frozen. Subsequent 1.x releases add only bug fixes, performance improvements, and non-breaking additions:

Any breaking change requires a 2.0 release.

§Feature flags

FeatureDefaultEffect
stdyesAdds impl std::error::Error for Error. Drop this feature for no_std targets.

§License

Dual-licensed under Apache-2.0 OR MIT.

Re-exports§

pub use bitfield::BitReader;
pub use bitfield::BitWriter;
pub use buf::ReadBuf;
pub use buf::WriteBuf;
pub use error::Error;
pub use error::Result;
pub use traits::Decode;
pub use traits::Encode;

Modules§

bitfield
Bit-level read and write cursors.
buf
Zero-copy cursor primitives over borrowed byte slices.
error
Error type returned by codec and framing operations.
framing
Frame extraction strategies.
traits
Core traits implemented by every encodable value.
varint
Unsigned LEB128 varint encoding.
zigzag
Zigzag encoding for signed integers.

Constants§

VERSION
Crate version string, populated by Cargo at build time.