serde only.Expand description
Support for serde integration. Enable this with the serde feature.
To encode/decode type that implement serde’s trait, you can use:
For interop with bincode’s Decode/Encode, you can use:
For interop with bincode’s derive feature, you can use the #[bincode(with_serde)] attribute on each field that implements serde’s traits.
#[derive(Serialize, Deserialize)]
pub struct SerdeType {
// ...
}
#[derive(Decode, Encode)]
pub struct StructWithSerde {
#[bincode(with_serde)]
pub serde: SerdeType,
}
#[derive(Decode, Encode)]
pub enum EnumWithSerde {
Unit(#[bincode(with_serde)] SerdeType),
Struct {
#[bincode(with_serde)]
serde: SerdeType,
},
}§Known issues
Because bincode is a format without meta data, there are several known issues with serde’s attributes. Please do not use any of the following attributes if you plan on using bincode, or use bincode’s own derive macros.
#[serde(flatten)]#[serde(skip)]#[serde(skip_deserializing)]#[serde(skip_serializing)]#[serde(skip_serializing_if = "path")]#[serde(tag = "...")]#[serde(untagged)]
Using any of the above attributes can and will cause issues with bincode and will result in lost data. Consider using bincode’s own derive macro instead.
§Why move away from serde?
Serde is a great library, but it has some issues that makes us want to be decoupled from serde:
- The issues documented above with attributes.
- Serde has chosen to not have a MSRV (source). We think MSRV is important, bincode 1 still compiles with rust 1.18.
- Before serde we had rustc-serializer. Serde has more than replaced rustc-serializer, but we can imagine a future where serde is replaced by something else.
- We believe that less dependencies is better, and that you should be able to choose your own dependencies. If you disable all features, bincode 2 only has 1 dependency. (
unty, a micro crate we manage ourselves)
note: just because we’re making serde an optional dependency, it does not mean we’re dropping support for serde. Serde will still be fully supported, we’re just giving you the option to not use it.
Structs§
- Borrow
Compat - Wrapper struct that implements BorrowDecode and Encode on any type that implements serde’s Deserialize and Serialize respectively. This is mostly used on
&[u8]and&str, for other types consider using Compat instead. - Borrowed
Serde Decoder - Serde decoder encapsulating a borrowed reader.
- Compat
- Wrapper struct that implements Decode and Encode on any type that implements serde’s DeserializeOwned and Serialize respectively.
- Owned
Serde Decoder - Serde decoder encapsulating an owned reader.
Enums§
- Decode
Error - A serde-specific error that occurred while decoding.
- Encode
Error - A serde-specific error that occurred while encoding.
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. - 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_ 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. - 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. See the config module for more information. - 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. - seed_
decode_ from_ slice - Decode a borrowed type from the given slice using a seed. Some parts of the decoded type are expected to be referring to the given slice