Available on crate feature 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.

Structs

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.
Wrapper struct that implements Decode and Encode on any type that implements serde’s DeserializeOwned and Serialize respectively.

Enums

A serde-specific error that occurred while decoding.
A serde-specific error that occurred while encoding.

Functions

Decode a borrowed type from the given slice. Some parts of the decoded type are expected to be referring to the given slice
Attempt to decode a given type D from the given Reader.
Decode an owned type from the given slice. Will return the decoded type T as well as the amount of bytes that were read.
Decode an owned type from the given std::io::Read.
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
Encode a serde Serialize type into a given byte slice with the bincode algorithm
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.
Encode the given value into a custom Writer.
Encode a serde Serialize type into a Vec<u8> with the bincode algorithm