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

#2207 in Encoding

Download history 20629/week @ 2023-12-09 20680/week @ 2023-12-16 13065/week @ 2023-12-23 15497/week @ 2023-12-30 19954/week @ 2024-01-06 21935/week @ 2024-01-13 21837/week @ 2024-01-20 23049/week @ 2024-01-27 22499/week @ 2024-02-03 15856/week @ 2024-02-10 21131/week @ 2024-02-17 22730/week @ 2024-02-24 25532/week @ 2024-03-02 24274/week @ 2024-03-09 27447/week @ 2024-03-16 22771/week @ 2024-03-23

103,108 downloads per month
Used in 18 crates (via fksainetwork)

MIT license

42KB
729 lines

Bincode-derive

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

This crate is roughly split into 2 parts:

Parsing

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

Generate

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.

Dependencies

~155KB