-
Notifications
You must be signed in to change notification settings - Fork 43
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
add derive bound option #23
Conversation
Should we have the automatic bounds be Also, I don't know where to put the documentation for the proc macro gc-arena/src/gc-arena-derive/src/lib.rs Line 226 in 5431134
|
I'm not sure why there's the convention of putting trait bounds on generics that aren't used tbh. Like in my case Sure, I just moved the doc to the collect derive macro. Apparently it has to go right before |
It would be the same override option right? We would only add bounds for generics by default, but |
Oh, so you mean replace |
I looked more into the dtolnay chimes in and says that bounds on field types is almost never what you want, and links another issue in I believe the current serde heuristic is to add a generic bound on the impl for every type parameter in every field which is actually going to be serialized / deserialized, and then allows you to ofc override the bounds when the heuristic fails. It allows this both for the type as a whole, and interestingly also on each field / variant, replacing any bounds produced by applying the heuristic to that field: Based on my understanding so far, I still think using |
Ok, just switched over to |
It's a shame that we need more bounds in so many places like that, but I guess this is still the best option? I played with serde a bit just to make sure I understood how it handles this and it seems to be the same way, but there are a lot of fun heuristics on top of it https://gist.github.com/77f765c79c13afb3899c376d51e2eb77 Did you know that if a type is named literally 'PhantomData' it will not generate a serialize bound on generics automatically? I didn't! Anyway I still think this is the right move it's just a tad more annoying than I would have hoped. I guess we can always get funky with heuristics in the future. |
I think other than the small nit it looks good now, thank you for going through all the back and forth here. |
Oh yeah, any time heuristics have to be used for code generation like this it's gonna get really detailed really quickly lol But as long as we're doing what other popular crates do, it's probably fine. Worst case is users have to explicitly set bounds, but that's not so bad either. The big benefit of the macro is making deriving Sure, I just unified those error messages under the new |
Yeah, the error messages are much better now, thank you! Merging! |
Closes #21. @kyren from further inspection I saw the trait bounds causing issues were not added by the proc macro when the
S
param was absent, sincesynstructure
doesn't producewhere
clauses for structs/enums when everything is known (no generics). To fix this, I took your suggestion and added abound = "<code>"
option to#[collect]
that overrides the default bounds added byAddBounds::Fields
.My original problem just uses the
S
param as a container for type aliases that already implementCollect
, so using#[collect(bound = "")]
solves my problem and i can successfully build again.Also, when I first started using
gc-arena
, I found it a bit hard to get started just because of a lack of detail about how to actually deriveCollect
(since it needs the additional#[collect]
attribute, which I had to look up in source for usage). So I also added some details about how to derive it to theCollect
docs entry (including its ability to be used on fields, which I had no idea about prior to delving into this issue but is actually really useful for my library).