qubit-codec-binary

Buffer-oriented binary codecs for Rust

Rust CI Coverage Crates.io Rust License

Buffer-oriented binary codecs for Rust.

Overview

Qubit Binary Codec provides low-level codecs for caller-managed byte buffers. It does not depend on std::io; stream reader and writer adapters live in qubit-io-binary.

This crate provides:

Design Goals

Features

Fixed-Width Binary Scalars

LEB128 Values

ZigZag Values

Focused Public API

Documentation

Installation

Add this to your Cargo.toml:

[dependencies]
qubit-codec-binary = "0.1"

Quick Start

use qubit_codec_binary::{
    BigEndian,
    BinaryCodec,
    Leb128Codec,
    NonStrict,
};

let mut fixed = [0_u8; BinaryCodec::<u32, BigEndian>::MAX_UNITS_PER_VALUE];
unsafe {
    BinaryCodec::<u32, BigEndian>::encode_unchecked(0x0102_0304, &mut fixed, 0);
}
assert_eq!([1, 2, 3, 4], fixed);

let mut compact = [0_u8; Leb128Codec::<u64, NonStrict>::MAX_UNITS_PER_VALUE];
let written = unsafe { Leb128Codec::<u64, NonStrict>::encode_unchecked(300, &mut compact, 0) };
assert_eq!(2, written);

Unchecked API Contracts

The low-level codec methods are intentionally unsafe. Callers must validate buffer bounds before using them:

Higher-level code should wrap these unsafe calls behind safe APIs after checking the appropriate bounds. Import owned-value adapters and buffered engines directly from qubit-codec; this crate does not re-export generic codec adapters. See the User Guide for binary codec examples.

API Reference

BinaryCodec Operations

ItemDescription
Codec (Unit = u8)Decode and encode one fixed-width scalar through the core trait
MIN_UNITS_PER_VALUEMinimum bytes required for the scalar type
MAX_UNITS_PER_VALUEMaximum bytes required for the scalar type
decode_unchecked(input, index)Decode one fixed-width scalar without bounds checks
encode_unchecked(value, output, index)Encode one fixed-width scalar without bounds checks

Leb128Codec Operations

ItemDescription
Codec (Unit = u8)Decode and encode one LEB128 value through the core trait
MIN_UNITS_PER_VALUEMinimum readable bytes that can contain a complete value
MAX_UNITS_PER_VALUEMaximum bytes needed for the integer type
decode_unchecked(input, index)Decode one complete LEB128 value
encode_unchecked(value, output, index)Encode one canonical LEB128 value

ZigZagCodec Operations

ItemDescription
Codec (Unit = u8)Decode and encode one ZigZag LEB128 value through the core trait
MIN_UNITS_PER_VALUEMinimum readable bytes that can contain a complete value
MAX_UNITS_PER_VALUEMaximum bytes needed for the signed integer type
decode_unchecked(input, index)Decode ZigZag over unsigned LEB128
encode_unchecked(value, output, index)Encode signed integer as ZigZag plus unsigned LEB128

LEB128 Decode Policies

PolicyMeaning
StrictReject non-canonical LEB128 encodings
NonStrictAccept compatible encodings when the decoded value fits
Leb128DecodePolicySealed policy trait implemented by the built-in policy markers

Crate Boundary

qubit-codec-binary only contains buffer-level binary codecs. Use qubit-codec for shared core traits, qubit-io for generic std::io helpers, and qubit-io-binary for stream-oriented binary readers and writers.

Performance Considerations

BinaryCodec, Leb128Codec, and ZigZagCodec are zero-sized codec types with no runtime allocation. Their unchecked methods and Codec implementations with Unit = u8 are intended for validated hot paths where a caller has already checked buffer capacity or is operating inside a buffered stream adapter.

Testing & Code Coverage

This project keeps binary wire-format behavior covered by integration tests under tests/.

Running Tests

# Run all tests
cargo test

# Run with coverage report
./coverage.sh

# Generate text format report
./coverage.sh text

# Align code with CI requirements
./align-ci.sh

# Run CI checks (format, clippy, test, coverage, audit)
RS_CI_SKIP_TOOLCHAIN_UPDATE=1 ./ci-check.sh

Dependencies

Runtime dependencies are intentionally small:

License

Copyright (c) 2026. Haixing Hu.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

See LICENSE for the full license text.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Development Guidelines

Author

Haixing Hu

---

Repository: https://github.com/qubit-ltd/rs-codec-binary