Skip to main content

cu_fec/
lib.rs

1//! Recover lost symbols without retransmission.
2//!
3//! Real-time and one-way streams often cannot wait for a lost packet to be sent
4//! again. Forward erasure correction adds repair symbols that let a receiver
5//! reconstruct missing source symbols from the packets that do arrive.
6//!
7//! This crate implements the systematic sliding-window random linear code from
8//! [RFC 8681] over GF(2) and GF(2^8). It suits lossy radio and UDP telemetry,
9//! logging, sensor, and media streams where recovery latency and memory use must
10//! remain bounded. It is transport agnostic, allocator-free, dependency-free,
11//! and usable from `no_std` targets.
12//!
13//! `cu-fec` was developed within the [Copper] robotics project for deterministic
14//! log streaming, but has no Copper dependencies, types, or protocol assumptions.
15//! Packet integrity, framing, pacing, and transport remain the caller's
16//! responsibility.
17//!
18//! # Example
19//!
20//! ```
21//! use cu_fec::{
22//!     DensityThreshold, EncodingSymbolId, Field, RepairParameters, RlcConfig,
23//!     RlcDecoder, RlcEncoder,
24//! };
25//!
26//! # fn main() -> Result<(), cu_fec::Error> {
27//! let config = RlcConfig::new(4, 16, Field::Gf256)?;
28//! let mut encoder = RlcEncoder::<1200, 64>::new(config, EncodingSymbolId::new(0))?;
29//! let mut decoder = RlcDecoder::<1200, 64, 64>::new(config, 16)?;
30//!
31//! let esi = encoder.push_source(&[1, 2, 3, 4])?;
32//! let _ = decoder.receive_source(esi, &[1, 2, 3, 4])?;
33//!
34//! let mut repair = [0_u8; 1200];
35//! let id = encoder.encode_repair(
36//!     RepairParameters::new(7, DensityThreshold::FULL),
37//!     &mut repair[..config.symbol_size()],
38//! )?;
39//! let _ = decoder.receive_repair(id, &repair[..config.symbol_size()])?;
40//! # Ok(())
41//! # }
42//! ```
43//!
44//! [Copper]: https://github.com/copper-project/copper-rs
45//! [RFC 8681]: https://www.rfc-editor.org/rfc/rfc8681.html
46
47#![no_std]
48#![forbid(unsafe_code)]
49#![warn(missing_docs)]
50
51mod field;
52/// Sliding-window random linear coding from RFC 8681.
53pub mod rlc;
54mod tinymt;
55/// RFC 8681 payload identifiers and wire metadata.
56pub mod wire;
57
58pub use field::Field;
59pub use rlc::{
60    DecodeReport, Decoder as RlcDecoder, DensityThreshold, Encoder as RlcEncoder, EncodingSymbolId,
61    Error, KnownSymbols, RepairParameters, RlcConfig, SourceReport, SourceStatus,
62    generate_coding_coefficients,
63};
64pub use wire::{FecSchemeSpecificInfo, RepairPayloadId, SourcePayloadId, WireError};