• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..30-Mar-2022-

src/H30-Mar-2022-3,7832,904

tests/H30-Mar-2022-900769

.cargo-checksum.jsonH A D03-May-202289 11

Cargo.tomlH A D30-Mar-20221.4 KiB3834

LICENSE.mdH A D30-Mar-20221.1 KiB2217

readme.mdH A D30-Mar-20224.7 KiB11280

readme.md

1# Bincode
2
3<img align="right" src="./logo.png" />
4
5![CI](https://github.com/servo/bincode/workflows/CI/badge.svg)
6[![](https://meritbadge.herokuapp.com/bincode)](https://crates.io/crates/bincode)
7[![](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT)
8[![](https://img.shields.io/badge/bincode-rustc_1.18+-lightgray.svg)](https://blog.rust-lang.org/2017/06/08/Rust-1.18.html)
9
10A compact encoder / decoder pair that uses a binary zero-fluff encoding scheme.
11The size of the encoded object will be the same or smaller than the size that
12the object takes up in memory in a running Rust program.
13
14In addition to exposing two simple functions
15(one that encodes to `Vec<u8>`, and one that decodes from `&[u8]`),
16binary-encode exposes a Reader/Writer API that makes it work
17perfectly with other stream-based APIs such as Rust files, network streams,
18and the [flate2-rs](https://github.com/alexcrichton/flate2-rs) compression
19library.
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
32use serde::{Serialize, Deserialize};
33
34#[derive(Serialize, Deserialize, PartialEq, Debug)]
35struct Entity {
36    x: f32,
37    y: f32,
38}
39
40#[derive(Serialize, Deserialize, PartialEq, Debug)]
41struct World(Vec<Entity>);
42
43fn 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
59The encoding (and thus decoding) proceeds unsurprisingly -- primitive
60types are encoded according to the underlying `Writer`, tuples and
61structs are encoded by encoding their fields one-by-one, and enums are
62encoded by first writing out the tag representing the variant and
63then the contents.
64
65However, 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
75Bincode's format will eventually be codified into a specification, along with
76its configuration options and default configuration. In the meantime, here are
77some frequently asked questions regarding use of the crate:
78
79### Is Bincode suitable for storage?
80
81The encoding format is stable across minor revisions, provided the same
82configuration is used. This should ensure that later versions can still read
83data produced by a previous versions of the library if no major version change
84has occured.
85
86Bincode is invariant over byte-order in the default configuration
87(`bincode::options::DefaultOptions`), making an exchange between different
88architectures possible. It is also rather space efficient, as it stores no
89metadata like struct field names in the output format and writes long streams of
90binary data without needing any potentially size-increasing encoding.
91
92As a result, Bincode is suitable for storing data. Be aware that it does not
93implement any sort of data versioning scheme or file headers, as these
94features are outside the scope of this crate.
95
96### Is Bincode suitable for untrusted inputs?
97
98Bincode attempts to protect against hostile data. There is a maximum size
99configuration available (`bincode::config::Bounded`), but not enabled in the
100default configuration. Enabling it causes pre-allocation size to be limited to
101prevent against memory exhaustion attacks.
102
103Deserializing any incoming data will not cause undefined behavior or memory
104issues, assuming that the deserialization code for the struct is safe itself.
105
106Bincode can be used for untrusted inputs in the sense that it will not create a
107security issues in your application, provided the configuration is changed to enable a
108maximum size limit. Malicious inputs will fail upon deserialization.
109
110### What is Bincode's MSRV (minimum supported Rust version)?
111
112Bincode 1.0 maintains support for rust 1.18.0. Any changes to this are considered a breaking change for semver purposes.