| 1 |
# Bincode |
| 2 |
|
| 3 |
<img align="right" src="./logo.png" /> |
| 4 |
|
| 5 |
 |
| 6 |
[](https://crates.io/crates/bincode) |
| 7 |
[](https://opensource.org/licenses/MIT) |
| 8 |
[](https://blog.rust-lang.org/2017/06/08/Rust-1.18.html) |
| 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/alexcrichton/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 |
|
| 29 |
## Example |
| 30 |
|
| 31 |
```rust |
| 32 |
use serde::{Serialize, Deserialize}; |
| 33 |
|
| 34 |
#[derive(Serialize, Deserialize, PartialEq, Debug)] |
| 35 |
struct Entity { |
| 36 |
x: f32, |
| 37 |
y: f32, |
| 38 |
} |
| 39 |
|
| 40 |
#[derive(Serialize, Deserialize, PartialEq, Debug)] |
| 41 |
struct World(Vec<Entity>); |
| 42 |
|
| 43 |
fn main() { |
| 44 |
let world = World(vec![Entity { x: 0.0, y: 4.0 }, Entity { x: 10.0, y: 20.5 }]); |
| 45 |
|
| 46 |
let encoded: Vec<u8> = bincode::serialize(&world).unwrap(); |
| 47 |
|
| 48 |
// 8 bytes for the length of the vector, 4 bytes per float. |
| 49 |
assert_eq!(encoded.len(), 8 + 4 * 4); |
| 50 |
|
| 51 |
let decoded: World = bincode::deserialize(&encoded[..]).unwrap(); |
| 52 |
|
| 53 |
assert_eq!(world, decoded); |
| 54 |
} |
| 55 |
``` |
| 56 |
|
| 57 |
## Details |
| 58 |
|
| 59 |
The encoding (and thus decoding) proceeds unsurprisingly -- primitive |
| 60 |
types are encoded according to the underlying `Writer`, tuples and |
| 61 |
structs are encoded by encoding their fields one-by-one, and enums are |
| 62 |
encoded by first writing out the tag representing the variant and |
| 63 |
then the contents. |
| 64 |
|
| 65 |
However, there are some implementation details to be aware of: |
| 66 |
|
| 67 |
* `isize`/`usize` are encoded as `i64`/`u64`, for portability. |
| 68 |
* enums variants are encoded as a `u32` instead of a `usize`. |
| 69 |
`u32` is enough for all practical uses. |
| 70 |
* `str` is encoded as `(u64, &[u8])`, where the `u64` is the number of |
| 71 |
bytes contained in the encoded string. |
| 72 |
|
| 73 |
## Specification |
| 74 |
|
| 75 |
Bincode's format will eventually be codified into a specification, along with |
| 76 |
its configuration options and default configuration. In the meantime, here are |
| 77 |
some frequently asked questions regarding use of the crate: |
| 78 |
|
| 79 |
### Is Bincode suitable for storage? |
| 80 |
|
| 81 |
The encoding format is stable across minor revisions, provided the same |
| 82 |
configuration is used. This should ensure that later versions can still read |
| 83 |
data produced by a previous versions of the library if no major version change |
| 84 |
has occured. |
| 85 |
|
| 86 |
Bincode is invariant over byte-order in the default configuration |
| 87 |
(`bincode::options::DefaultOptions`), making an exchange between different |
| 88 |
architectures possible. It is also rather space efficient, as it stores no |
| 89 |
metadata like struct field names in the output format and writes long streams of |
| 90 |
binary data without needing any potentially size-increasing encoding. |
| 91 |
|
| 92 |
As a result, Bincode is suitable for storing data. Be aware that it does not |
| 93 |
implement any sort of data versioning scheme or file headers, as these |
| 94 |
features are outside the scope of this crate. |
| 95 |
|
| 96 |
### Is Bincode suitable for untrusted inputs? |
| 97 |
|
| 98 |
Bincode attempts to protect against hostile data. There is a maximum size |
| 99 |
configuration available (`bincode::config::Bounded`), but not enabled in the |
| 100 |
default configuration. Enabling it causes pre-allocation size to be limited to |
| 101 |
prevent against memory exhaustion attacks. |
| 102 |
|
| 103 |
Deserializing any incoming data will not cause undefined behavior or memory |
| 104 |
issues, assuming that the deserialization code for the struct is safe itself. |
| 105 |
|
| 106 |
Bincode can be used for untrusted inputs in the sense that it will not create a |
| 107 |
security issues in your application, provided the configuration is changed to enable a |
| 108 |
maximum size limit. Malicious inputs will fail upon deserialization. |
| 109 |
|
| 110 |
### What is Bincode's MSRV (minimum supported Rust version)? |
| 111 |
|
| 112 |
Bincode 1.0 maintains support for rust 1.18.0. Any changes to this are considered a breaking change for semver purposes. |