-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
derive asset for enums #10410
derive asset for enums #10410
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need this feature. (well, I don't, since I can easily wrap my enum into a struct, but it would be useful)
But the macro code is always very intimidating to newcomers, I think we should take care to use good quality code for proc macros. There are possible improvements.
For interested 3rd parties: Additional feedback in robtfm#3 |
Cleanup derive_dependency_visitor_internal impl
@nicopap much cleaner, thank you. i only added a tweak to remove warnings on tuple structs. |
how about adding some simple test somewhere: #[derive(Asset, Reflect)]
enum FooBar {
Foo,
Bar,
} |
added some structs with the derive macro - it will test that the output compiles only. |
Looks like this PR doesn't break the API. Maybe we could apply it for 0.12.1? |
# Objective allow deriving `Asset` for enums, and for tuple structs. ## Solution add to the proc macro, generating visitor calls to the variant's data (if required). supports unnamed or named field variants, and struct variants when the struct also derives `Asset`: ```rust #[derive(Asset, TypePath)] pub enum MyAssetEnum { Unnamed ( #[dependency] Handle<Image> ), Named { #[dependency] array_handle: Handle<Image>, #[dependency] atlas_handles: Vec<Handle<Image>>, }, StructStyle( #[dependency] VariantStruct ), Empty, } #[derive(Asset, TypePath)] pub struct VariantStruct { // ... } ``` also extend the struct implementation to support tuple structs: ```rust #[derive(Asset, TypePath)] pub struct MyImageNewtype( #[dependency] Handle<Image> ); ```` --------- Co-authored-by: Nicola Papale <nico@nicopap.ch>
# Objective allow deriving `Asset` for enums, and for tuple structs. ## Solution add to the proc macro, generating visitor calls to the variant's data (if required). supports unnamed or named field variants, and struct variants when the struct also derives `Asset`: ```rust #[derive(Asset, TypePath)] pub enum MyAssetEnum { Unnamed ( #[dependency] Handle<Image> ), Named { #[dependency] array_handle: Handle<Image>, #[dependency] atlas_handles: Vec<Handle<Image>>, }, StructStyle( #[dependency] VariantStruct ), Empty, } #[derive(Asset, TypePath)] pub struct VariantStruct { // ... } ``` also extend the struct implementation to support tuple structs: ```rust #[derive(Asset, TypePath)] pub struct MyImageNewtype( #[dependency] Handle<Image> ); ```` --------- Co-authored-by: Nicola Papale <nico@nicopap.ch>
Objective
allow deriving
Asset
for enums, and for tuple structs.Solution
add to the proc macro, generating visitor calls to the variant's data (if required).
supports unnamed or named field variants, and struct variants when the struct also derives
Asset
:also extend the struct implementation to support tuple structs: