Expand description
Bincode-next 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
| Name | Default? | Affects MSRV? | Supported types for Encode/Decode | Enabled methods | Other |
|---|---|---|---|---|---|
| std | Yes | No | HashMap and HashSet | decode_from_std_read and encode_into_std_write | |
| alloc | Yes | No | All common containers in alloc, like Vec, String, Box | encode_to_vec | |
| atomic | Yes | No | All Atomic* integer types, e.g. AtomicUsize, and AtomicBool | ||
| derive | Yes | No | Enables the BorrowDecode, Decode, Encode, Fingerprint and BitPacked derive macros | ||
| serde | No | Yes (MSRV reliant on serde) | Compat and BorrowCompat, which will work for all types that implement serde’s traits | serde-specific encode/decode functions in the [serde] module | Note: There are several known issues when using serde and bincode |
| zero-copy | No | No | RelativePtr, ZeroArray, ZeroSlice, ZeroStr, ZeroString | Enables the relative_ptr module and the ZeroCopy derive macro | Zero-copy nested structures using offsets |
| static-size | No | No | Enables the static_size module, the bounded module and the StaticSize derive macro |
§Which functions to use
Bincode has a couple of pairs of functions that are used in different situations.
| Situation | Encode | Decode |
|---|---|---|
You’re working with [fs::File] or [net::TcpStream] | [encode_into_std_write] | [decode_from_std_read] |
| you’re working with in-memory buffers | [encode_to_vec] | [decode_from_slice] |
| You want to use a custom Reader and Writer | [encode_into_writer] | [decode_from_reader] |
| You’re working with pre-allocated buffers or on embedded targets | [encode_into_slice] | [decode_from_slice] |
Note: If you’re using serde, use bincode_next::serde::... instead of bincode_next::...
§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_next::encode_into_slice(
input,
&mut slice,
bincode_next::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_next::decode_from_slice(slice, bincode_next::config::standard()).unwrap().0;
assert_eq!(decoded, input);[fs::File]: std::fs::File
[net::TcpStream]: std::net::TcpStream
Re-exports§
pub use de::BorrowDecode;pub use de::Decode;pub use enc::Encode;pub use fingerprint::Fingerprint;
Modules§
- bounded
( allocorstdorderiveorserdeorzero-copyorstatic-size) andstatic-sizeandalloc - Bounded types for compile-time size guarantees.
- 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 encountering by Encoding and Decoding.
- fingerprint
- Fingerprinting support for schema verification. Fingerprinting implementation for bincode.
- migration_
guide - Migrating from bincode 1 to 2
- relative_
ptr ( allocorstdorderiveorserdeorzero-copyorstatic-size) andzero-copy - Relative pointer system for zero-copy nested structures
- serde
serde - Support for serde integration. Enable this with the
serdefeature. - spec
allocandderive - Serialization Specification
- static_
size ( allocorstdorderiveorserdeorzero-copyorstatic-size) andstatic-size - Static size trait for compile-time memory bound validation.
Structs§
- IoReader
std - A reader that reads from a
std::io::Read. - IoWriter
std - A writer that writes to a
std::io::Write. - VecWriter
alloc - A writer that writes into a
Vec<u8>.
Constants§
- BINCODE_
MAJOR_ VERSION - The major version of the bincode library.
Traits§
- Static
Size static-size - A trait that calculates the upper bound of a type’s serialized size as a
constvalue. - Zero
Copy zero-copy - A marker trait indicating that a type has a fixed, predictable layout (e.g.,
#[repr(C)]) and contains no padding bytes or invalid bit patterns allowing safe zero-copy casting. - Zero
Copy Type zero-copy - A trait for zero-copy types that defines their preferred builder type.
Functions§
- borrow_
decode_ from_ slice - Attempt to decode a given type
Dfrom the given slice. Returns the decoded output and the amount of bytes read. - borrow_
decode_ from_ slice_ static static-size - Attempt to decode a given type
Dfrom the given slice with a compile-time bound check. - borrow_
decode_ from_ slice_ static_ with_ context static-size - Attempt to borrow-decode a given type
Dfrom the given slice with a compile-time bound check and a decoding context. - borrow_
decode_ from_ slice_ with_ context - Attempt to decode a given type
Dfrom the given slice withContext. Returns the decoded output and the amount of bytes read. - decode_
async async-fiber - Attempt to decode a given type
Tfrom the given async reader safely using a non-blocking fiber. - decode_
async_ smol async-fiber - Attempt to decode a given type
Tfrom asmolreader. - decode_
async_ smol_ with_ context async-fiber - Attempt to decode a given type
Tfrom asmolreader. - decode_
async_ std async-fiber - Attempt to decode a given type
Tfrom anasync-stdreader. - decode_
async_ std_ with_ context async-fiber - Attempt to decode a given type
Tfrom anasync-stdreader. - decode_
async_ tokio tokioandasync-fiber - Attempt to decode a given type
Tfrom the given tokio async reader safely using a non-blocking fiber. - decode_
async_ tokio_ with_ context tokioandasync-fiber - Attempt to decode a given type
Tfrom the given tokio async reader using a non-blocking fiber and a context. - decode_
async_ with_ context async-fiber - Attempt to decode a given type
Tfrom the given async reader using a non-blocking fiber and a context. - decode_
from_ reader - Attempt to decode a given type
Dfrom the given [Reader]. - decode_
from_ slice - Attempt to decode a given type
Dfrom the given slice. Returns the decoded output and the amount of bytes read. - decode_
from_ slice_ static static-size - Attempt to decode a given type
Dfrom the given slice with a compile-time bound check. - decode_
from_ slice_ static_ with_ context static-size - Attempt to decode a given type
Dfrom the given slice with a compile-time bound check and a decoding context. - decode_
from_ slice_ with_ context - Attempt to decode a given type
Dfrom the given slice withContext. Returns the decoded output and the amount of bytes read. - decode_
from_ std_ read std - Decode type
Dfrom the given reader with the givenConfig. The reader can be any type that implementsstd::io::Read, e.g.std::fs::File. - decode_
from_ std_ read_ with_ context std - Decode type
Dfrom the given reader with the givenConfigandContext. The reader can be any type that implementsstd::io::Read, e.g.std::fs::File. - decode_
serde_ async async-fiberandserde - Attempt to decode a given serde-compatible type
Tfrom the given async reader safely using a non-blocking fiber. - decode_
serde_ async_ smol async-fiberandserde - Attempt to decode a given serde-compatible type
Tfrom asmolreader. - decode_
serde_ async_ smol_ with_ context async-fiberandserde - Attempt to decode a given serde-compatible type
Tfrom asmolreader. - decode_
serde_ async_ std async-fiberandserde - Attempt to decode a given serde-compatible type
Tfrom anasync-stdreader. - decode_
serde_ async_ std_ with_ context async-fiberandserde - Attempt to decode a given serde-compatible type
Tfrom anasync-stdreader. - decode_
serde_ async_ with_ context async-fiberandserde - Attempt to decode a given serde-compatible type
Tfrom the given async reader using a non-blocking fiber and a context. - decode_
serde_ tokio_ async tokioandasync-fiberandserde - Attempt to decode a given serde-compatible type
Tfrom the given tokio async reader safely using a non-blocking fiber. - decode_
serde_ tokio_ async_ with_ context tokioandasync-fiberandserde - Attempt to decode a given serde-compatible type
Tfrom the given tokio async reader using a non-blocking fiber and a context. - encode_
into_ slice - Encode the given value into the given slice. Returns the amount of bytes that have been written.
- encode_
into_ std_ write std - Encode the given value into any type that implements
std::io::Write, e.g.std::fs::File, with the givenConfig. - encode_
into_ writer - Encode the given value into a custom [
Writer]. - encode_
to_ vec alloc - Encode the given value into a
Vec<u8>with the givenConfig. See the config module for more information.
Derive Macros§
- BitPacked
derive - Borrow
Decode derive - Decode
derive - Encode
derive - Fingerprint
derive - Static
Size static-sizeandderive - Zero
Copy zero-copyandderive