bincode_reloaded 3.1.0

A binary serialization / deserialization strategy for transforming structs into bytes and vice versa!
Documentation

bincode_reloaded

CodeQL Rust CI/Unit Tests Snyk Security-Monitored Security audit dependency status rust-clippy analyze OpenSSF Best Practices Scorecard supply-chain security Microsoft Defender For Devops Coverage Status Feature Requests Bugs

A compact encoder / decoder pair that uses a binary zero-fluff encoding scheme. The size of the encoded object will be the same or smaller than the size that the object takes up in memory in a running Rust program.

In addition to exposing two simple functions (one that encodes to Vec<u8>, and one that decodes from &[u8]), binary-encode exposes a Reader/Writer API that makes it work perfectly with other stream-based APIs such as Rust files, network streams, and the flate2-rs compression library.

Notes on this fork

  • originally forked from bincode 2.0.1
  • several security scanners have been added to the repo to ensure any issues are found quickly
  • MSRV (minimum supported Rust version) updated from 1.85 to 1.86 (without build/unit test errors)
  • Rust edition updated from 2021 to 2024 (without build/unit test errors)
  • minor code optimizations to improve efficiency
  • will be maintained (depenencies/crates updated & CVEs addressed in a timely manner, etc.)

API Documentation

bincode_reloaded in the Wild

  • google/tarpc: bincode_reloaded is used to serialize and deserialize networked RPC messages.
  • servo/webrender: bincode_reloaded records WebRender API calls for record/replay-style graphics debugging.
  • servo/ipc-channel: IPC-Channel uses bincode_reloaded to send structs between processes using a channel-like API.
  • ajeetdsouza/zoxide: zoxide uses bincode_reloaded to store a database of directories and their access frequencies on disk.

Example

use bincode_reloaded::{config, Decode, Encode};

#[derive(Encode, Decode, PartialEq, Debug)]
struct Entity {
    x: f32,
    y: f32,
}

#[derive(Encode, Decode, PartialEq, Debug)]
struct World(Vec<Entity>);

fn main() {
    let config = config::standard();

    let world = World(vec![Entity { x: 0.0, y: 4.0 }, Entity { x: 10.0, y: 20.5 }]);

    let encoded: Vec<u8> = bincode_reloaded::encode_to_vec(&world, config).unwrap();

    // The length of the vector is encoded as a varint u64, which in this case gets collapsed to a single byte
    // See the documentation on varint for more info for that.
    // The 4 floats are encoded in 4 bytes each.
    assert_eq!(encoded.len(), 1 + 4 * 4);

    let (decoded, len): (World, usize) = bincode_reloaded::decode_from_slice(&encoded[..], config).unwrap();

    assert_eq!(world, decoded);
    assert_eq!(len, encoded.len()); // read all bytes
}

Specification

bincode_reloaded's format is specified in docs/spec.md.

FAQ

Is bincode_reloaded suitable for storage?

The encoding format is stable, provided the same configuration is used. This should ensure that later versions can still read data produced by a previous versions of the library if no major version change has occurred.

bincode_reloaded 1 and 2 are completely compatible if the same configuration is used.

bincode_reloaded is invariant over byte-order, making an exchange between different architectures possible. It is also rather space efficient, as it stores no metadata like struct field names in the output format and writes long streams of binary data without needing any potentially size-increasing encoding.

As a result, bincode_reloaded is suitable for storing data. Be aware that it does not implement any sort of data versioning scheme or file headers, as these features are outside the scope of this crate.

Is bincode_reloaded suitable for untrusted inputs?

bincode_reloaded attempts to protect against hostile data. There is a maximum size configuration available (Configuration::with_limit), but not enabled in the default configuration. Enabling it causes pre-allocation size to be limited to prevent against memory exhaustion attacks.

Deserializing any incoming data will not cause undefined behavior or memory issues, assuming that the deserialization code for the struct is safe itself.

bincode_reloaded can be used for untrusted inputs in the sense that it will not create a security issues in your application, provided the configuration is changed to enable a maximum size limit. Malicious inputs will fail upon deserialization.

What is bincode_reloaded's MSRV (minimum supported Rust version)?

bincode_reloaded 2.0 has an MSRV of 1.86.0. Any changes to the MSRV are considered a breaking change for semver purposes, except when certain features are enabled. Features affecting MSRV are documented in the crate root.

Why does bincode_reloaded not respect #[repr(u8)]?

bincode_reloaded will encode enum variants as a u32. If you're worried about storage size, we can recommend enabling Configuration::with_variable_int_encoding(). This option is enabled by default with the standard configuration. In this case enum variants will almost always be encoded as a u8.

Currently we have not found a compelling case to respect #[repr(...)]. You're most likely trying to interop with a format that is similar-but-not-quite-bincode_reloaded. We only support our own protocol (spec).

If you really want to use bincode_reloaded to encode/decode a different protocol, consider implementing Encode and Decode yourself. bincode_reloaded-derive will output the generated implementation in target/generated/bincode_reloaded/<name>_Encode.rs and target/generated/bincode_reloaded/<name>_Decode.rs which should get you started.

Terms of Service

Please read our Terms of Service before using our software. Violators of these Terms are not supported by the community or contributors.

Privacy Policy

Please also read our Privacy Policy to understand how we handle your personal information.

Contact

Have questions or suggestions? Reach out to us at dev@butlergroup.net. Thank you and happy coding! :)

Star History

Star History Chart