[][src]Macro sp_npos_elections_compact::generate_solution_type

generate_solution_type!() { /* proc-macro */ }

Generates a struct to store the election result in a small way. This can encode a structure which is the equivalent of a sp_npos_elections::Assignment<_>.

The following data types can be configured by the macro.

  • The identifier of the voter. This can be any type that supports parity-scale-codec's compact encoding.
  • The identifier of the target. This can be any type that supports parity-scale-codec's compact encoding.
  • The accuracy of the ratios. This must be one of the PerThing types defined in sp-arithmetic.

Moreover, the maximum number of edges per voter (distribution per assignment) also need to be specified. Attempting to convert from/to an assignment with more distributions will fail.

For example, the following generates a public struct with name TestSolution with u16 voter type, u8 target type and Perbill accuracy with maximum of 8 edges per voter.

generate_solution_type!(pub struct TestSolution<u16, u8, Perbill>::(8))

The given struct provides function to convert from/to Assignment:

  • [from_assignment()].
  • [fn into_assignment()].

The generated struct is by default deriving both Encode and Decode. This is okay but could lead to many 0s in the solution. If prefixed with #[compact], then a custom compact encoding for numbers will be used, similar to how parity-scale-codec's Compact works.

generate_solution_type!(
    #[compact]
    pub struct TestSolutionCompact<u16, u8, Perbill>::(8)
)