counters: allow deriving Count
for generic types
#1647
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, placing the
#[derive(Count)]
attribute on a genericenum
doesn't compile. This branch changes the
#[derive(Count)]
codegeneration to support generic types. If a type parameter is used as a
field with the
#[count(children)]
attribute, the generated countersstruct will also be generic over that type parameter, and contain the
counters type for that type's
Count
implementation, with awhere
clause requiring that type to also implement
Count
. If the typeparameter is not used in a field that has the
#[count(children)]
attribute, the generated counters struct needn't be generic over that
type parameter,1 so we exclude it from the counter's struct list
of type params. This required a bit of a complex change to the code
generation to implement.
I've added a new example of deriving
Count
for generic types,including one that's generic over type parameters that don't get used
in a
#[count(children)]
field. This example compiles, verifying thatwe now generate code that makes sense when deriving
Count
for genericenums.
Footnotes
and, in fact, if the counters struct is generic over a type
parameter that it doesn't actually use, it won't compile, as the
type parameter is unused. ↩