-
Notifications
You must be signed in to change notification settings - Fork 25
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
Support for derive on unions? #6
Comments
I expect most custom derives will not handle unions and just panic. I included them in DeriveInput because the compiler permits derive on unions. Something like |
Yeah, that seems like the most reasonable solution, at least for now. |
Now that syn supports |
synstructure was changed to use |
I'm curious what the thinking is here? Do you mean handling unions at all, or does the word "generically" here mean something specific? The reason I ask is that I'd like to be able to support unions in my custom derive at some point. I can use something other than synstructure, of course - I just mean to illustrate that there are real use cases. |
Support for custom derives on unions was added in rust-lang/rust#50383 for consistency with built-in derives, despite most derives not being compatible (rust-lang/rust#50383 (comment)). This is because union field accesses are unsafe, and the correctness of these accesses depends on external logic not visible to the derive implementer. In effect, a derive which cares about the values of fields cannot support unions, which vastly limits the set of compatible derives. Only specific derives, such as the built-in |
If there are specific synstructure features which would be useful for your |
Concretely, I'm interested in deriving the zerocopy traits, which are just unsafe marker traits that don't actually produce any code. For example, a derive of FromBytes for a union would just need to decide whether each individual variant was itself The part of synstructure that we currently make use of is |
I didn't see any uses of #[proc_macro_derive(FromBytes)]
pub fn derive_from_bytes(ts: proc_macro::TokenStream) -> proc_macro::TokenStream {
let ast = syn::parse_macro_input!(ts as DeriveInput);
match &ast.data {
Data::Struct(strct) => derive_from_bytes_struct(&ast, strct),
Data::Enum(enm) => derive_from_bytes_enum(&ast, enm),
Data::Union(uni) => derive_from_bytes_union(&ast, uni),
}
} |
Ah you're probably right. It's been a while since I wrote the code, so I honestly don't remember why I used synstructure. Certainly looks like, whatever the reason, we don't need it anymore. |
I don't know what the correct behavior for synstructure should be here. In the update for syn 0.12, I intend to simply panic when being passed a union.
We should figure out what to do in that situation.
The text was updated successfully, but these errors were encountered: