This is supported 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,
    },
}

alloc and no_std

The serde feature enables both alloc and std at this point in time. To use bincode and serde on no_std targets, try one of the following features:

  • serde_alloc: enables serde and alloc
  • serde_no_std: enables serde without alloc or std

Known issues

Currently the serde feature will automatically enable the alloc and std feature. If you’re running in a #[no_std] environment consider using bincode’s own derive macros.

Because bincode is a format without meta data, there are several known issues with serde’s skip attributes. Please do not use skip attributes if you plan on using bincode, or use bincode’s own derive macros.

This includes:

  • #[serde(skip)]
  • #[serde(skip_serializing)]
  • #[serde(skip_deserializing)]
  • #[serde(skip_serializing_if = "path")]
  • #[serde(flatten)]

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 occured while decoding.

A serde-specific error that occured 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.

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