| 1 |
# Bincode |
| 2 |
<img align="right" src="./logo.svg" /> |
| 3 |
|
| 4 |
[](https://github.com/bincode-org/bincode/actions) |
| 5 |
[](https://crates.io/crates/bincode) |
| 6 |
[](https://opensource.org/licenses/MIT) |
| 7 |
<!-- [](https://blog.rust-lang.org/2020/02/27/Rust-1.41.1.html) --> |
| 8 |
[](https://matrix.to/#/#bincode:matrix.org) |
| 9 |
|
| 10 |
A compact encoder / decoder pair that uses a binary zero-fluff encoding scheme. |
| 11 |
The size of the encoded object will be the same or smaller than the size that |
| 12 |
the object takes up in memory in a running Rust program. |
| 13 |
|
| 14 |
In addition to exposing two simple functions |
| 15 |
(one that encodes to `Vec<u8>`, and one that decodes from `&[u8]`), |
| 16 |
binary-encode exposes a Reader/Writer API that makes it work |
| 17 |
perfectly with other stream-based APIs such as Rust files, network streams, |
| 18 |
and the [flate2-rs](https://github.com/rust-lang/flate2-rs) compression |
| 19 |
library. |
| 20 |
|
| 21 |
## [API Documentation](https://docs.rs/bincode/) |
| 22 |
|
| 23 |
## Bincode in the Wild |
| 24 |
|
| 25 |
* [google/tarpc](https://github.com/google/tarpc): Bincode is used to serialize and deserialize networked RPC messages. |
| 26 |
* [servo/webrender](https://github.com/servo/webrender): Bincode records WebRender API calls for record/replay-style graphics debugging. |
| 27 |
* [servo/ipc-channel](https://github.com/servo/ipc-channel): IPC-Channel uses Bincode to send structs between processes using a channel-like API. |
| 28 |
* [ajeetdsouza/zoxide](https://github.com/ajeetdsouza/zoxide): zoxide uses Bincode to store a database of directories and their access frequencies on disk. |
| 29 |
|
| 30 |
## Example |
| 31 |
|
| 32 |
```rust |
| 33 |
use bincode::{config, Decode, Encode}; |
| 34 |
|
| 35 |
#[derive(Encode, Decode, PartialEq, Debug)] |
| 36 |
struct Entity { |
| 37 |
x: f32, |
| 38 |
y: f32, |
| 39 |
} |
| 40 |
|
| 41 |
#[derive(Encode, Decode, PartialEq, Debug)] |
| 42 |
struct World(Vec<Entity>); |
| 43 |
|
| 44 |
fn main() { |
| 45 |
let config = config::standard(); |
| 46 |
|
| 47 |
let world = World(vec![Entity { x: 0.0, y: 4.0 }, Entity { x: 10.0, y: 20.5 }]); |
| 48 |
|
| 49 |
let encoded: Vec<u8> = bincode::encode_to_vec(&world, config).unwrap(); |
| 50 |
|
| 51 |
// The length of the vector is encoded as a varint u64, which in this case gets collapsed to a single byte |
| 52 |
// See the documentation on varint for more info for that. |
| 53 |
// The 4 floats are encoded in 4 bytes each. |
| 54 |
assert_eq!(encoded.len(), 1 + 4 * 4); |
| 55 |
|
| 56 |
let (decoded, len): (World, usize) = bincode::decode_from_slice(&encoded[..], config).unwrap(); |
| 57 |
|
| 58 |
assert_eq!(world, decoded); |
| 59 |
assert_eq!(len, encoded.len()); // read all bytes |
| 60 |
} |
| 61 |
``` |
| 62 |
|
| 63 |
## Specification |
| 64 |
|
| 65 |
Bincode's format is specified in [docs/spec.md](https://github.com/bincode-org/bincode/blob/trunk/docs/spec.md). |
| 66 |
|
| 67 |
## FAQ |
| 68 |
|
| 69 |
### Is Bincode suitable for storage? |
| 70 |
|
| 71 |
The encoding format is stable, provided the same configuration is used. |
| 72 |
This should ensure that later versions can still read data produced by a previous versions of the library if no major version change |
| 73 |
has occurred. |
| 74 |
|
| 75 |
Bincode 1 and 2 are completely compatible if the same configuration is used. |
| 76 |
|
| 77 |
Bincode is invariant over byte-order, making an exchange between different |
| 78 |
architectures possible. It is also rather space efficient, as it stores no |
| 79 |
metadata like struct field names in the output format and writes long streams of |
| 80 |
binary data without needing any potentially size-increasing encoding. |
| 81 |
|
| 82 |
As a result, Bincode is suitable for storing data. Be aware that it does not |
| 83 |
implement any sort of data versioning scheme or file headers, as these |
| 84 |
features are outside the scope of this crate. |
| 85 |
|
| 86 |
### Is Bincode suitable for untrusted inputs? |
| 87 |
|
| 88 |
Bincode attempts to protect against hostile data. There is a maximum size |
| 89 |
configuration available (`Configuration::with_limit`), but not enabled in the |
| 90 |
default configuration. Enabling it causes pre-allocation size to be limited to |
| 91 |
prevent against memory exhaustion attacks. |
| 92 |
|
| 93 |
Deserializing any incoming data will not cause undefined behavior or memory |
| 94 |
issues, assuming that the deserialization code for the struct is safe itself. |
| 95 |
|
| 96 |
Bincode can be used for untrusted inputs in the sense that it will not create a |
| 97 |
security issues in your application, provided the configuration is changed to enable a |
| 98 |
maximum size limit. Malicious inputs will fail upon deserialization. |
| 99 |
|
| 100 |
### What is Bincode's MSRV (minimum supported Rust version)? |
| 101 |
|
| 102 |
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. |
| 103 |
|
| 104 |
### Why does bincode not respect `#[repr(u8)]`? |
| 105 |
|
| 106 |
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`. |
| 107 |
|
| 108 |
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](https://github.com/bincode-org/bincode/blob/trunk/docs/spec.md)). |
| 109 |
|
| 110 |
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. |