cobs_codec_rs
#![no_std], dependency-free Consistent Overhead Byte Stuffing (COBS) and
COBS/R for Rust — the Rust member of the Firechip COBS family (alongside the
Dart cobs_codec and the Kotlin
cobs_codec_kt), verified
byte-identical against the shared
conformance vectors.
COBS encodes an arbitrary byte sequence into one that contains no zero (0x00)
byte, at a small and predictable cost: at most one extra byte per 254 bytes,
plus one. That makes a single 0x00 a reliable packet delimiter for serial/UART,
USB, TCP and other byte streams — ideal for embedded and robotics protocols.
Features
- Basic COBS and COBS/R (Reduced) encode/decode.
no_stdand zero dependencies — the coreencode/decodework on caller-provided slices, allocating nothing.allocconveniences (*_to_vec) and a streamingFrameDecoderfor0x00-delimited serial links, behind the defaultstd/allocfeatures.const fnsize helpers (max_encoded_len,encoding_overhead) for compile-time buffer sizing.
Install
[]
= "1.0"
# no_std, no allocator:
# cobs_codec_rs = { version = "1.0", default-features = false }
Usage
use ;
// With alloc (default):
let encoded = encode_to_vec;
assert_eq!; // no 0x00
assert_eq!;
// COBS/R often saves the trailing overhead byte:
assert_eq!; // same length as input
no_std, into a fixed buffer:
use ;
let src = ;
let mut buf = ;
let n = encode;
assert_eq!;
Reading a delimited serial stream (needs alloc):
use ;
let mut rx = new.max_frame_len;
// `chunk` is any &[u8] read from the link; chunks need not align with frames.
# let chunk = frame_to_vec;
rx.push;
Overhead
COBS overhead is data-independent: at most ⌈n / 254⌉ extra bytes for n input
bytes (and always ≥ 1). Compare escape-based schemes (PPP/SLIP), whose worst case
doubles the packet.
Background
Stuart Cheshire and Mary Baker, "Consistent Overhead Byte Stuffing", IEEE/ACM Transactions on Networking, Vol. 7, No. 2, April 1999. COBS/R is a variant by Craig McQueen.
License
MIT © 2026 Alexander Salas Bastidas (Firechip). See LICENSE.