Crate bincode

Source
Expand description

Bincode is a crate for encoding and decoding using a tiny binary serialization strategy. Using it, you can easily go from having an object in memory, quickly serialize it to bytes, and then deserialize it back just as fast!

If you’re coming from bincode 1, check out our migration guide

§Serde

Starting from bincode 2, serde is now an optional dependency. If you want to use serde, please enable the serde feature. See Features for more information.

§Features

NameDefault?Affects MSRV?Supported types for Encode/DecodeEnabled methodsOther
stdYesNoHashMap and HashSetdecode_from_std_read and encode_into_std_write
allocYesNoAll common containers in alloc, like Vec, String, Boxencode_to_vec
atomicYesNoAll Atomic* integer types, e.g. AtomicUsize, and AtomicBool
deriveYesNoEnables the BorrowDecode, Decode and Encode derive macros
serdeNoYes (MSRV reliant on serde)Compat and BorrowCompat, which will work for all types that implement serde’s traitsserde-specific encode/decode functions in the serde moduleNote: There are several known issues when using serde and bincode

§Which functions to use

Bincode has a couple of pairs of functions that are used in different situations.

SituationEncodeDecode
You’re working with fs::File or net::TcpStreamencode_into_std_writedecode_from_std_read
you’re working with in-memory buffersencode_to_vecdecode_from_slice
You want to use a custom Reader and Writerencode_into_writerdecode_from_reader
You’re working with pre-allocated buffers or on embedded targetsencode_into_slicedecode_from_slice

Note: If you’re using serde, use bincode::serde::... instead of bincode::...

§Example

let mut slice = [0u8; 100];

// You can encode any type that implements `Encode`.
// You can automatically implement this trait on custom types with the `derive` feature.
let input = (
    0u8,
    10u32,
    10000i128,
    'a',
    [0u8, 1u8, 2u8, 3u8]
);

let length = bincode::encode_into_slice(
    input,
    &mut slice,
    bincode::config::standard()
).unwrap();

let slice = &slice[..length];
println!("Bytes written: {:?}", slice);

// Decoding works the same as encoding.
// The trait used is `Decode`, and can also be automatically implemented with the `derive` feature.
let decoded: (u8, u32, i128, char, [u8; 4]) = bincode::decode_from_slice(slice, bincode::config::standard()).unwrap().0;

assert_eq!(decoded, input);

Re-exports§

pub use de::BorrowDecode;
pub use de::Decode;
pub use enc::Encode;

Modules§

config
The config module is used to change the behavior of bincode’s encoding and decoding logic.
de
Decoder-based structs and traits.
enc
Encoder-based structs and traits.
error
Errors that can be encounting by Encoding and Decoding.
migration_guide
Migrating from bincode 1 to 2
serdeserde
Support for serde integration. Enable this with the serde feature.
spec
Serialization Specification

Macros§

impl_borrow_decode
Helper macro to implement BorrowDecode for any type that implements Decode.
impl_borrow_decode_with_context
Helper macro to implement BorrowDecode for any type that implements Decode.

Functions§

borrow_decode_from_slice
Attempt to decode a given type D from the given slice. Returns the decoded output and the amount of bytes read.
borrow_decode_from_slice_with_context
Attempt to decode a given type D from the given slice with Context. Returns the decoded output and the amount of bytes read.
decode_from_reader
Attempt to decode a given type D from the given Reader.
decode_from_slice
Attempt to decode a given type D from the given slice. Returns the decoded output and the amount of bytes read.
decode_from_slice_with_context
Attempt to decode a given type D from the given slice with Context. Returns the decoded output and the amount of bytes read.
decode_from_std_readstd
Decode type D from the given reader with the given Config. The reader can be any type that implements std::io::Read, e.g. std::fs::File.
decode_from_std_read_with_contextstd
Decode type D from the given reader with the given Config and Context. The reader can be any type that implements std::io::Read, e.g. std::fs::File.
encode_into_slice
Encode the given value into the given slice. Returns the amount of bytes that have been written.
encode_into_std_writestd
Encode the given value into any type that implements std::io::Write, e.g. std::fs::File, with the given Config. See the config module for more information. Returns the amount of bytes written.
encode_into_writer
Encode the given value into a custom Writer.
encode_to_vecalloc
Encode the given value into a Vec<u8> with the given Config. See the config module for more information.

Derive Macros§

BorrowDecodederive
Decodederive
Encodederive