[go: up one dir, main page]

Module serde

Module serde 

Source
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.

§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§

BorrowCompat
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.
BorrowedSerdeDecoder
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.
OwnedSerdeDecoder
Serde decoder encapsulating an owned reader.

Enums§

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

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.
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_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.
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.
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.
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