Qubit Codec
qubit-codec gives Rust codec and adapter authors one set of contracts for
single-value codecs, owned convenience APIs, and caller-buffered streaming.
Format crates keep their binary, text, or character-set rules local while this
crate supplies checked capacity handling, explicit lifecycle semantics, and
policy-ready conversion loops.
Installation
[]
= "0.13"
The default feature set is empty. Enable io only for the qubit-io buffered
bridges:
[]
= { = "0.13", = ["io"] }
The minimum supported Rust version is 1.94.
Quick Start
Start by choosing the smallest public contract that matches the format:
| Format requirement | Start with |
|---|---|
| One meaningful logical value maps to encoded units | Implement Codec |
| Only the complete input is a meaningful operation | Implement ValueEncoder / ValueDecoder directly |
An existing Codec needs owned output |
Use CodecValueEncoder / CodecValueDecoder |
An existing Codec needs a strict buffered stream |
Use a CodecTranscode* adapter |
| Invalid or unencodable values need policy | Use a transcode engine with hooks |
| EOF or framing behavior does not fit the supplied engines | Implement Transcoder |
For example, a crate that owns a fixed-width big-endian u16 codec implements
Codec once. It can then expose an owned operation without duplicating bounds
or lifecycle handling:
use ;
use ;
;
let mut encoder = new;
let bytes = encoder.encode.expect;
assert_eq!;
The user guide continues this exact scenario with owned decode, caller-buffered streaming, incomplete input, lifecycle output, and policy hooks.
Why This Project Exists
A format crate often begins with one value-to-unit operation and later grows
owned helpers, streaming adapters, malformed-input policy, and I/O integration.
If every layer reimplements indices, capacity, EOF, and reset/finish behavior,
the same format acquires incompatible contracts. qubit-codec keeps those
mechanics shared while leaving domain rules in the format crate that owns them.
What It Provides
| Need | Public API |
|---|---|
| Low-level value/unit contract | Codec, DecodeFailure |
| Owned whole-value conversion | ValueEncoder, ValueDecoder, CodecValueEncoder, CodecValueDecoder |
| Strict caller-buffered conversion | Transcoder, CodecTranscodeEncoder, CodecTranscodeDecoder, CodecTranscodeConverter |
| Policy-aware conversion | engine::TranscodeEncodeEngine, engine::TranscodeDecodeEngine, engine::TranscodeConvertEngine, and hooks including DecodeIncompleteAction |
| Progress and backpressure | TranscodeProgress, TranscodeStatus |
| Runtime or static byte order | ByteOrder, ByteOrderSpec, BigEndian, LittleEndian, NativeEndian |
qubit-io bridges with io |
Sync and partial-I/O async transcode input/output adapters |
Boundaries and Guarantees
- The crate does not implement concrete binary formats, character sets,
Base64, hex, percent encoding, or
std::ioextensions. Codec::decodeseparates an incomplete visible prefix from invalid domain input throughDecodeFailure; an incomplete prefix is not an EOF decision.Codec::encode_lenmust equal the units written by the following successfulCodec::encodein the same state, including intentional zero-output buffering.- A
Transcoderfollowsreset -> transcode/transcode_eof -> finish.NeedInputleaves its tail with the caller;NeedOutputrequires more destination capacity;Completemeans all visible input was consumed. TranscodeDecodeHooks::handle_incomplete_decodelets decode engines chooseReject,Skip, orEmitfor an incomplete tail only after EOF is known.- Capacity bounds must cover every reachable transient state. Owned adapters may allocate; streaming APIs use caller-provided buffers.
Learn More
- User guide: implement a codec, expose adapters, drive a stream, and diagnose failures.
- 中文用户手册
- API documentation
- Lifecycle-aware decode example
- 中文 README
Testing
# Run tests with the default feature set
# Run tests with all declared features
# Project CI checks
# Check code coverage
License
Copyright (c) 2025 - 2026. Haixing Hu. All rights reserved.
Licensed under the Apache License, Version 2.0. See LICENSE for the full license text.
Contributing
Contributions are welcome. Please follow the Rust API guidelines, keep public
API documentation and tests current, and run ./align-ci.sh to format code and
./ci-check.sh to satisfy CI requirements before submitting a pull request.
Author
Haixing Hu - Qubit Co. Ltd.
Repository: https://github.com/qubit-ltd/rs-codec