Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

counters: allow deriving Count for generic types #1647

Merged
merged 3 commits into from
Mar 7, 2024
Merged

Conversation

hawkw
Copy link
Member

@hawkw hawkw commented Mar 6, 2024

Currently, placing the #[derive(Count)] attribute on a generic enum
doesn't compile. This branch changes the #[derive(Count)] code
generation to support generic types. If a type parameter is used as a
field with the #[count(children)] attribute, the generated counters
struct will also be generic over that type parameter, and contain the
counters type for that type's Count implementation, with a where
clause requiring that type to also implement Count. If the type
parameter 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 that
we now generate code that makes sense when deriving Count for generic
enums.

Footnotes

  1. 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.

quote! { #ty: counters::Count }
})
.collect::<Vec<_>>();
// I'm not sure why we have to do this, but quote gets mad if it's not a vec...
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW I've also run into this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, as far as I understand it, it just needs to be IntoIterator, but it seems like the HashSet's IntoIterator implementation doesn't meet some required bound in quote somewhere...

@hawkw hawkw merged commit 38bbffa into master Mar 7, 2024
83 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants