Rust library for asynchronous stream (de)serialization.
Built-in fixed-length sequence visitors, including Duration, require exact cardinality
and sequence completion. Invalid duration nanoseconds reject before completion
validation.
Use FromStream, ToStream, and IntoStream for ordinary typed values;
fixed-schema types can implement visitors and delegate their fields to these traits.
For data whose nesting is determined by input, use iterative traversal with an explicit frame stack. This avoids call-stack exhaustion through uncontrolled recursion (CWE-674). Depth and allocation limits remain necessary to bound heap usage and work. Recursive value owners must also make destruction and other structural operations stack-safe.
- Decoding uses a container cursor:
peek_kind,open_container, andfinish_childlet the owner drive its frame stack and delegate typed leaves throughFromStream. Complete each child before advancing its cursor; discard the decoder after failed or interrupted traversal. - Encoding uses a pull-based structural event stream:
encode_eventsconsumes sequence/map starts, leaves, and container ends. For example,[[1], 2]emitsSeqStart, SeqStart, Value(1), End, Value(2), End(size hints omitted).Event::Valuedelegates a leaf's ordinary encoding; recursive containers must be emitted as events. The codec retains one active leaf stream, accepting borrowed input without requiringUnpinor'static.
Events describe traversal; stack safety comes from the iterative implementation. Arbitrary recursive visitors or leaf encoders do not become stack-safe automatically.
Each codec owns root/container validation, byte framing, and leaf encoding.
inspect_any supplies structural observations
without constructing decoded payloads; value owners supply allocation policy.
Encoder::encode_str accepts borrowed or owned AsRef<str> + Send inputs for
the encoding lifetime, including &str, String, and shared strings. An encoder
may retain the input without copying it. collect_str instead formats immediately;
its input need not outlive the returned stream, and formatting errors are returned
synchronously. Its default implementation owns the formatted text; codecs may
format directly into their output buffer.
Encoder output is format-specific: Encoder::Ok and the container builders'
matching output types may be streams, futures, or completed values. Generic
consumers that poll output as a stream declare that bound explicitly; input
streams and structural events remain pull-driven.