Skip to main content

Crate rustbinary

Crate rustbinary 

Source
Expand description

RustBinary is a bounded nextjson binary codec with explicit wire profiles.

Serialization is driven entirely by nextjson’s format-neutral contracts (nextjson::NsonSerialize / nextjson::NsonDeserialize + nextjson::FormatEncoder / nextjson::FormatDecoder), replacing the former Serde dependency. The binary wire format is a type-tagged, self-describing stream: every value carries a one-byte type tag and containers are terminator-delimited, so Option, Value, untagged enums and borrowed strings all round-trip unambiguously.

The top-level functions and options select the strict compact profile: canonical marker varints, ZigZag signed integers, bounded input, and rejected trailing bytes. legacy_options explicitly selects the old fixed-width, unbounded migration profile. Format-changing systems are explicit wrappers, so enabling a Cargo feature never silently changes an existing payload.

§Quick start

use nextjson::{NsonDeserialize, NsonSerialize};

#[derive(Debug, PartialEq, NsonSerialize, NsonDeserialize)]
struct Packet<'a> {
    sequence: u64,
    topic: &'a str,
    #[njson(borrow)]
    note: &'a str,
}

let config = rustbinary::options()
    .with_limit(4096)
    .with_collection_limit(256);
let packet = Packet {
    sequence: 42,
    topic: "telemetry/temperature",
    note: "ok",
};

let mut frame = [0_u8; 256];
let written = config.serialize_into_slice(&mut frame, &packet)?;
let decoded: Packet<'_> = config.deserialize(&frame[..written])?;
assert_eq!(decoded, packet);

Borrowed strings point into the input frame. Owned targets such as String and Vec<T> may allocate as required by their type.

§Format selection

  • Config is the core binary profile.
  • adaptive contains canonical cost-selected string and integer frames.
  • bitpack provides generated bit-level layouts.
  • cbor provides RFC 8949 payloads and deterministic map ordering.
  • evolution provides stable-field-ID schema evolution.
  • compression and encryption form an ordered transform pipeline.
  • parallel encodes independent records into deterministic batch frames.

§Untrusted input

Always set both Config::with_limit and Config::with_collection_limit at trust boundaries. Encryption authenticates bytes but does not replace resource limits. Schema fingerprints detect accidental type/configuration drift; they are not cryptographic hashes.

Re-exports§

pub use adaptive::AdaptiveConfig;adaptive
pub use adaptive::CollectionStrategy;adaptive
pub use adaptive::StringStrategy;adaptive
pub use bitpack::BitPack;bit-packing
pub use bitpack::BitPackedConfig;bit-packing
pub use bitpack::BitReader;bit-packing
pub use bitpack::BitValue;bit-packing
pub use bitpack::BitWriter;bit-packing
pub use cbor::CborConfig;cbor
pub use cbor::FingerprintedCborConfig;cbor and fingerprint
pub use compression::CompressedConfig;compression
pub use config::Config;
pub use config::Endian;
pub use config::IntEncoding;
pub use config::Options;
pub use config::TrailingBytes;
pub use config::DEFAULT_COLLECTION_LIMIT;
pub use config::DEFAULT_SIZE_LIMIT;
pub use encryption::EncryptedConfig;encryption
pub use encryption::EncryptionKey;encryption
pub use error::Error;
pub use error::ErrorCategory;
pub use error::Result;
pub use evolution::EvolutionConfig;schema-evolution
pub use evolution::FieldDecoder;schema-evolution
pub use evolution::FieldEncoder;schema-evolution
pub use evolution::SchemaDecode;schema-evolution
pub use evolution::SchemaEncode;schema-evolution
pub use evolution::UnknownField;schema-evolution
pub use parallel::ParallelConfig;parallel
pub use reflection::FieldInfo;reflection
pub use reflection::Reflect;reflection
pub use reflection::TypeShape;reflection
pub use reflection::VariantInfo;reflection
pub use schema::Fingerprint;fingerprint
pub use schema::FingerprintedConfig;fingerprint
pub use simd::hardware_capabilities;simd
pub use simd::simd_backend;simd
pub use simd::HardwareCapabilities;simd
pub use simd::SimdBackend;simd
pub use static_size::StaticSize;static-size
pub use writer::CountWriter;
pub use writer::EncodeWriter;
pub use writer::SliceWriter;

Modules§

adaptersstd
Bridges between the slice-based core and std::io readers and writers. Standard-library adapters around the no_std Compact V1 core.
adaptiveadaptive
Canonical data-aware encodings for strings and integer collections.
archivearchive
Validated relative-pointer archives for read-only memory mapping. Validated relative-pointer archives for read-only memory mapping.
bitpackbit-packing
Bit-level caller-buffer codecs and the BitPack contract.
cborcbor
RFC 8949 CBOR configuration and deterministic encoding. RFC 8949 CBOR configuration driven by nextjson’s CBOR relay.
compressioncompression
Adaptive Zstandard framing.
config
Core wire-profile configuration.
core
Minimal stable binary codec product surface. Minimal stable binary format and resource-policy API.
encryptionencryption
Authenticated XChaCha20-Poly1305 framing.
error
Codec result and error types.
evolutionschema-evolution
Stable-field-ID schema evolution.
parallelparallel
Ordered multi-core batch encoding and decoding.
pipeline
Optional transform product surface. Opt-in CBOR, compression, encryption, and parallel transform APIs.
protocol
Schema and wire-governance product surface. Opt-in schema, compatibility, and compact-layout governance APIs.
reflectionreflection
Allocation-free structural metadata generated by Reflect.
schemafingerprint
Compile-time schema identity and fingerprinted frame support.
simdsimd
Runtime-dispatched SIMD primitives used by codec hot paths.
static_sizestatic-size
Compile-time upper bounds for statically sized data.
writer
Core output sinks for caller-owned and counting serialization.

Traits§

NsonDeserializederive
Re-exports nextjson’s format-neutral serialization contracts.
NsonSchemaderive
Re-exports nextjson’s format-neutral serialization contracts.
NsonSerializederive
Re-exports nextjson’s format-neutral serialization contracts.

Functions§

deserialize
Deserializes from a slice with the bounded compact Core profile.
deserialize_fromstd
Deserializes an owned value from a reader with the bounded compact Core profile.
legacy_options
Returns the fixed-width compatibility profile used by the top-level API.
options
Returns the standard compact profile.
serializealloc
Serializes a value with the bounded compact Core profile.
serialize_intostd
Serializes a value directly into a writer with the bounded compact Core profile.
serialize_into_slice
Serializes into a caller-owned slice without codec-owned heap allocation.
serialized_size
Computes the exact serialized byte count without allocating an output buffer.

Derive Macros§

BitPackedbit-packing and derive
Derives rustbinary::BitPack for structs and enums.
Fingerprintderive and fingerprint
Derives rustbinary::Fingerprint from structural type metadata.
NsonDeserializederive
Re-exports nextjson’s format-neutral serialization contracts.
NsonSerializederive
Re-exports nextjson’s format-neutral serialization contracts.
Reflectderive and reflection
Derives allocation-free structural reflection metadata.
StaticSizederive and static-size
Derives compile-time normal and bit-packed size bounds.