Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A binary encoder / decoder implementation in Rust.

License

NotificationsYou must be signed in to change notification settings

bincode-org/bincode

Repository files navigation

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 thatthe object takes up in memory in a running Rust program.

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

Usage Manifesto

The bincode developers do not endorse or support: the gas and oil industry, gambling, the military industrial complex, or any usage of AI. This means we will not accept any contributions coming from or related to these fields. This includes both PRs and filed issues. If you fall in one of these categories; do better.

Additionally, if any contribution you make makes use of generative AI, be it in code or PR/issue descriptions, you will be immediately banned from this organization.

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)]structEntity{x:f32,y:f32,}#[derive(Encode,Decode,PartialEq,Debug)]structWorld(Vec<Entity>);fnmain(){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 indocs/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 changehas occurred.

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

Bincode is invariant over byte-order, making an exchange between differentarchitectures possible. It is also rather space efficient, as it stores nometadata like struct field names in the output format and writes long streams ofbinary data without needing any potentially size-increasing encoding.

As a result, Bincode is suitable for storing data. Be aware that it does notimplement any sort of data versioning scheme or file headers, as thesefeatures are outside the scope of this crate.

Is Bincode suitable for untrusted inputs?

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

Deserializing any incoming data will not cause undefined behavior or memoryissues, 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 asecurity issues in your application, provided the configuration is changed to enable amaximum size limit. Malicious inputs will fail upon deserialization.

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

Bincode 2.0 has an MSRV of 1.85.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 not respect#[repr(u8)]?

Bincode will encode enum variants as au32. If you're worried about storage size, we can recommend enablingConfiguration::with_variable_int_encoding(). This option is enabled by default with thestandard configuration. In this case enum variants will almost always be encoded as au8.

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 implementingEncode andDecode yourself.bincode-derive will output the generated implementation intarget/generated/bincode/<name>_Encode.rs andtarget/generated/bincode/<name>_Decode.rs which should get you started.

About

A binary encoder / decoder implementation in Rust.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

    Packages

    No packages published

    Contributors112

    Languages


    [8]ページ先頭

    ©2009-2025 Movatter.jp