#binary #encode #decode #serialize #deserialize

macro bincode_derive

Implementation of #[derive(Encode, Decode)] for bincode

10 releases

2.0.0-rc.3 Mar 30, 2023
2.0.0-rc.2 Oct 4, 2022
2.0.0-rc.1 Mar 4, 2022
2.0.0-beta.3 Feb 16, 2022
2.0.0-alpha.0 Oct 25, 2021

#39 in #deserialize

Download history 15995/week @ 2023-02-07 15013/week @ 2023-02-14 12388/week @ 2023-02-21 10701/week @ 2023-02-28 11059/week @ 2023-03-07 12016/week @ 2023-03-14 11842/week @ 2023-03-21 13894/week @ 2023-03-28 12696/week @ 2023-04-04 14327/week @ 2023-04-11 10696/week @ 2023-04-18 13950/week @ 2023-04-25 8822/week @ 2023-05-02 10946/week @ 2023-05-09 9282/week @ 2023-05-16 9874/week @ 2023-05-23

40,390 downloads per month

MIT license

729 lines


The derive crate for bincode. Implements bincode::Encode and bincode::Decode.

This crate is roughly split into 2 parts:


Most of parsing is done in the src/parse/ folder. This will generate the following types:

  • Attributes, not being used currently
  • Visibility, not being used currently
  • DataType either Struct or Enum, with the name of the data type being parsed
  • Generics the generics part of the type, e.g. struct Foo<'a>
  • GenericConstraints the "where" part of the type


Generating the code implementation is done in either src/derive_enum.rs and src/derive_struct.rs.

This is supported by the structs in src/generate. The most notable points of this module are:

  • StreamBuilder is a thin but friendly wrapper around TokenStream
  • Generator is the base type of the code generator. This has helper methods to generate implementations:
    • ImplFor is a helper struct for a single impl A for B construction. In this functions can be defined:
      • GenerateFnBody is a helper struct for a single function in the above impl. This is created with a callback to FnBuilder which helps set some properties. GenerateFnBody has a stream() function which returns StreamBuilder for the function.

For additional derive testing, see the test cases in ../tests

For testing purposes, all generated code is outputted to the current target/generated/bincode folder, under file name <struct/enum name>_Encode.rs and <struct/enum name>_Decode.rs. This can help with debugging.