[Mirror] A binary encoder / decoder implementation in Rust.
Go to file
Trangar 100685bc28
Some checks failed
Cross platform tests / Cross platform test (platforms with standard library) (aarch64-unknown-linux-gnu) (push) Has been cancelled
Cross platform tests / Cross platform test (platforms with standard library) (aarch64-unknown-linux-musl) (push) Has been cancelled
Cross platform tests / Cross platform test (platforms with standard library) (arm-linux-androideabi) (push) Has been cancelled
Cross platform tests / Cross platform test (platforms with standard library) (arm-unknown-linux-gnueabi) (push) Has been cancelled
Cross platform tests / Cross platform test (platforms with standard library) (arm-unknown-linux-gnueabihf) (push) Has been cancelled
Cross platform tests / Cross platform test (platforms with standard library) (arm-unknown-linux-musleabi) (push) Has been cancelled
Cross platform tests / Cross platform test (platforms with standard library) (arm-unknown-linux-musleabihf) (push) Has been cancelled
Cross platform tests / Cross platform test (platforms with standard library) (armv5te-unknown-linux-gnueabi) (push) Has been cancelled
Cross platform tests / Cross platform test (platforms with standard library) (armv5te-unknown-linux-musleabi) (push) Has been cancelled
Cross platform tests / Cross platform test (platforms with standard library) (armv7-linux-androideabi) (push) Has been cancelled
Cross platform tests / Cross platform test (platforms with standard library) (armv7-unknown-linux-gnueabi) (push) Has been cancelled
Cross platform tests / Cross platform test (platforms with standard library) (armv7-unknown-linux-gnueabihf) (push) Has been cancelled
Cross platform tests / Cross platform test (platforms with standard library) (armv7-unknown-linux-musleabi) (push) Has been cancelled
Cross platform tests / Cross platform test (platforms with standard library) (armv7-unknown-linux-musleabihf) (push) Has been cancelled
Cross platform tests / Cross platform test (platforms with standard library) (i586-unknown-linux-gnu) (push) Has been cancelled
Cross platform tests / Cross platform test (platforms with standard library) (i586-unknown-linux-musl) (push) Has been cancelled
Cross platform tests / Cross platform test (platforms with standard library) (i686-unknown-linux-gnu) (push) Has been cancelled
Cross platform tests / Cross platform test (platforms with standard library) (i686-unknown-linux-musl) (push) Has been cancelled
Cross platform tests / Cross platform test (platforms with standard library) (powerpc-unknown-linux-gnu) (push) Has been cancelled
Cross platform tests / Cross platform test (platforms with standard library) (powerpc64-unknown-linux-gnu) (push) Has been cancelled
Cross platform tests / Cross platform test (platforms with standard library) (powerpc64le-unknown-linux-gnu) (push) Has been cancelled
Cross platform tests / Cross platform test (platforms with standard library) (riscv64gc-unknown-linux-gnu) (push) Has been cancelled
Cross platform tests / Cross platform test (platforms with standard library) (s390x-unknown-linux-gnu) (push) Has been cancelled
Cross platform tests / Cross platform test (platforms with standard library) (sparc64-unknown-linux-gnu) (push) Has been cancelled
Cross platform tests / Cross platform test (platforms with standard library) (thumbv7neon-linux-androideabi) (push) Has been cancelled
Cross platform tests / Cross platform test (platforms with standard library) (thumbv7neon-unknown-linux-gnueabihf) (push) Has been cancelled
Cross platform tests / Cross platform test (platforms with standard library) (x86_64-unknown-linux-gnu) (push) Has been cancelled
Cross platform tests / Cross platform test (platforms with standard library) (x86_64-unknown-linux-musl) (push) Has been cancelled
miri / MIRI (push) Has been cancelled
CI / Check (beta) (push) Has been cancelled
CI / Check (nightly) (push) Has been cancelled
CI / Check (stable) (push) Has been cancelled
CI / Test (, macos-latest, stable) (push) Has been cancelled
CI / Test (, ubuntu-latest, stable) (push) Has been cancelled
CI / Test (, windows-latest, stable) (push) Has been cancelled
CI / Test (alloc, macos-latest, stable) (push) Has been cancelled
CI / Test (alloc, ubuntu-latest, stable) (push) Has been cancelled
CI / Test (alloc, windows-latest, stable) (push) Has been cancelled
CI / Test (alloc,derive, macos-latest, stable) (push) Has been cancelled
CI / Test (alloc,derive, ubuntu-latest, stable) (push) Has been cancelled
CI / Test (alloc,derive, windows-latest, stable) (push) Has been cancelled
CI / Test (alloc,serde, macos-latest, stable) (push) Has been cancelled
CI / Test (alloc,serde, ubuntu-latest, stable) (push) Has been cancelled
CI / Test (alloc,serde, windows-latest, stable) (push) Has been cancelled
CI / Test (alloc,serde,derive, macos-latest, stable) (push) Has been cancelled
CI / Test (alloc,serde,derive, ubuntu-latest, stable) (push) Has been cancelled
CI / Test (alloc,serde,derive, windows-latest, stable) (push) Has been cancelled
CI / Test (serde, macos-latest, stable) (push) Has been cancelled
CI / Test (serde, ubuntu-latest, stable) (push) Has been cancelled
CI / Test (serde, windows-latest, stable) (push) Has been cancelled
CI / Test (serde,derive, macos-latest, stable) (push) Has been cancelled
CI / Test (serde,derive, ubuntu-latest, stable) (push) Has been cancelled
CI / Test (serde,derive, windows-latest, stable) (push) Has been cancelled
CI / Test (std, macos-latest, stable) (push) Has been cancelled
CI / Test (std, ubuntu-latest, stable) (push) Has been cancelled
CI / Test (std, windows-latest, stable) (push) Has been cancelled
CI / Test (std,derive, macos-latest, stable) (push) Has been cancelled
CI / Test (std,derive, ubuntu-latest, stable) (push) Has been cancelled
CI / Test (std,derive, windows-latest, stable) (push) Has been cancelled
CI / Test (std,serde, macos-latest, stable) (push) Has been cancelled
CI / Test (std,serde, ubuntu-latest, stable) (push) Has been cancelled
CI / Test (std,serde, windows-latest, stable) (push) Has been cancelled
CI / Test (std,serde,derive, macos-latest, stable) (push) Has been cancelled
CI / Test (std,serde,derive, ubuntu-latest, stable) (push) Has been cancelled
CI / Test (std,serde,derive, windows-latest, stable) (push) Has been cancelled
CI / Lints (push) Has been cancelled
CI / Compatibility (push) Has been cancelled
CI / Code Coverage (push) Has been cancelled
Fixed a warning in a derive test that would cause CI to fail (#716)
* Fixed a warning in a derive test that would cause CI to fail

* Fixed new clippy warning

* Commented out breaking cross builds

---------

Co-authored-by: Victor Koenders <victor.koenders@qrtech.se>
2024-05-28 10:39:05 +02:00
.github Fixed a warning in a derive test that would cause CI to fail (#716) 2024-05-28 10:39:05 +02:00
benches Improved encoding and decoding speed of Vec<u8> (#619) 2023-03-30 11:45:47 +02:00
compatibility Made arrays never encode their length (#625) 2023-03-30 15:09:33 +02:00
derive Update virtue requirement from 0.0.15 to 0.0.16 (#692) 2024-03-17 19:10:10 +01:00
docs Update spec for Option<T> encoding (#702) 2024-03-17 19:20:49 +01:00
fuzz Release rc.3 (#628) 2023-03-30 16:12:47 +02:00
src Fixed a warning in a derive test that would cause CI to fail (#716) 2024-05-28 10:39:05 +02:00
tests Fixed a warning in a derive test that would cause CI to fail (#716) 2024-05-28 10:39:05 +02:00
.gitignore Fixed #707 (#708) 2024-03-21 10:27:32 +01:00
.mailmap add mailmap entries 2020-03-16 10:57:07 -04:00
.rustfmt.toml Fixed newline issues in project 2021-10-16 11:04:06 +02:00
Cargo.toml Update glam requirement from 0.24 to 0.25 (#688) 2024-02-21 15:49:11 +01:00
CODE_OF_CONDUCT.md Create CODE_OF_CONDUCT.md (#597) 2022-10-20 18:45:34 +02:00
LICENSE.md Update LICENSE.md 2014-09-18 13:30:55 -07:00
logo.svg Update logo (#407) 2021-10-07 14:25:53 +02:00
readme.md Move generated files to target/generated/bincode (#600) 2022-11-03 09:13:09 +01:00

Bincode

CI

Matrix

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.

API Documentation

Bincode in the Wild

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

Example

use bincode::{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::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::decode_from_slice(&encoded[..], config).unwrap();

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

Specification

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

FAQ

Is Bincode 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 1 and 2 are completely compatible if the same configuration is used.

Bincode 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 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 suitable for untrusted inputs?

Bincode 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 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's MSRV (minimum supported Rust version)?

Bincode 2.0 is still in development and does not yet have a targeted MSRV. Once 2.0 is fully released the MSRV will be locked. After this point any changes to the MSRV are considered a breaking change for semver purposes.

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

Bincode 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. We only support our own protocol (spec).

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