Name |
Date |
Size |
#Lines |
LOC |
||
---|---|---|---|---|---|---|
.. | - | - | ||||
src/ | 25-Apr-2025 | - | 3,783 | 2,904 | ||
tests/ | 25-Apr-2025 | - | 900 | 769 | ||
.cargo-checksum.json | D | 25-Apr-2025 | 1.5 KiB | 1 | 1 | |
Android.bp | D | 25-Apr-2025 | 833 | 32 | 28 | |
Cargo.toml | D | 25-Apr-2025 | 1.4 KiB | 38 | 34 | |
LICENSE | D | 25-Apr-2025 | 1.1 KiB | 22 | 17 | |
LICENSE.md | D | 25-Apr-2025 | 1.1 KiB | 22 | 17 | |
METADATA | D | 25-Apr-2025 | 453 | 18 | 17 | |
MODULE_LICENSE_MIT | D | 25-Apr-2025 | 0 | |||
cargo_embargo.json | D | 25-Apr-2025 | 25 | 4 | 3 | |
readme.md | D | 25-Apr-2025 | 4.7 KiB | 112 | 80 |
readme.md
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 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.