make types within generate_solution_type macro explicit (#8447)

* make types within `generate_solution_type` macro explicit

Closes #8444.

Just changes the parsing logic for that macro; does not change any
emitted code. The associated types associated with the macro now
require explicit, keyword-style declaration.

**Old**:

```rust
sp_npos_elections::generate_solution_type!(
	#[compact]
	pub struct TestCompact::<VoterIndex, TargetIndex, PerU16>(16)
);
```

**New**:

```rust
sp_npos_elections::generate_solution_type!(
	#[compact]
	pub struct TestCompact::<VoterIndex = VoterIndex, CandidateIndex = TargetIndex, Accuracy = PerU16>(16)
);
```

* un-ignore doc-tests

* use new form in bin/node/runtime/

* rename CandidateIndex -> TargetIndex

* add tests demonstrating some potential compile failures
This commit is contained in:
Peter Goodspeed-Niklaus
2021-03-28 10:21:06 +02:00
committed by GitHub
parent 2af0de50c9
commit c2dd5e21a4
17 changed files with 145 additions and 16 deletions
@@ -1148,7 +1148,11 @@ mod solution_type {
type TestAccuracy = Percent;
generate_solution_type!(pub struct TestSolutionCompact::<u32, u8, TestAccuracy>(16));
generate_solution_type!(pub struct TestSolutionCompact::<
VoterIndex = u32,
TargetIndex = u8,
Accuracy = TestAccuracy,
>(16));
#[allow(dead_code)]
mod __private {
@@ -1158,7 +1162,7 @@ mod solution_type {
use sp_arithmetic::Percent;
generate_solution_type!(
#[compact]
struct InnerTestSolutionCompact::<u32, u8, Percent>(12)
struct InnerTestSolutionCompact::<VoterIndex = u32, TargetIndex = u8, Accuracy = Percent>(12)
);
}
@@ -1166,7 +1170,11 @@ mod solution_type {
fn solution_struct_works_with_and_without_compact() {
// we use u32 size to make sure compact is smaller.
let without_compact = {
generate_solution_type!(pub struct InnerTestSolution::<u32, u32, Percent>(16));
generate_solution_type!(pub struct InnerTestSolution::<
VoterIndex = u32,
TargetIndex = u32,
Accuracy = Percent,
>(16));
let compact = InnerTestSolution {
votes1: vec![(2, 20), (4, 40)],
votes2: vec![
@@ -1180,7 +1188,11 @@ mod solution_type {
};
let with_compact = {
generate_solution_type!(#[compact] pub struct InnerTestSolutionCompact::<u32, u32, Percent>(16));
generate_solution_type!(#[compact] pub struct InnerTestSolutionCompact::<
VoterIndex = u32,
TargetIndex = u32,
Accuracy = Percent,
>(16));
let compact = InnerTestSolutionCompact {
votes1: vec![(2, 20), (4, 40)],
votes2: vec![