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

derive asset for enums #10410

Merged
merged 9 commits into from
Nov 21, 2023
Merged

derive asset for enums #10410

merged 9 commits into from
Nov 21, 2023

Conversation

robtfm
Copy link
Contributor

@robtfm robtfm commented Nov 6, 2023

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:

#[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:

#[derive(Asset, TypePath)]
pub struct MyImageNewtype(
    #[dependency]
    Handle<Image>
);

@robtfm robtfm added C-Enhancement A new feature A-Assets Load files from disk to use for things like images, models, and sounds labels Nov 6, 2023
Copy link
Contributor

@nicopap nicopap left a 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.

crates/bevy_asset/macros/src/lib.rs Outdated Show resolved Hide resolved
crates/bevy_asset/macros/src/lib.rs Outdated Show resolved Hide resolved
@nicopap
Copy link
Contributor

nicopap commented Nov 6, 2023

For interested 3rd parties: Additional feedback in robtfm#3

@robtfm
Copy link
Contributor Author

robtfm commented Nov 6, 2023

@nicopap much cleaner, thank you.

i only added a tweak to remove warnings on tuple structs.

@nicopap nicopap added this to the 0.13 milestone Nov 7, 2023
@SET001
Copy link
Member

SET001 commented Nov 16, 2023

how about adding some simple test somewhere:

    #[derive(Asset, Reflect)]
    enum FooBar {
        Foo,
        Bar,
    }

@robtfm
Copy link
Contributor Author

robtfm commented Nov 16, 2023

added some structs with the derive macro - it will test that the output compiles only.

@robtfm robtfm added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Nov 17, 2023
@Shatur
Copy link
Contributor

Shatur commented Nov 18, 2023

Looks like this PR doesn't break the API. Maybe we could apply it for 0.12.1?

@nicopap nicopap modified the milestones: 0.13, 0.12.1 Nov 20, 2023
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Nov 21, 2023
Merged via the queue into bevyengine:main with commit 7ff61a8 Nov 21, 2023
22 checks passed
cart pushed a commit that referenced this pull request Nov 30, 2023
# 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>
rdrpenguin04 pushed a commit to rdrpenguin04/bevy that referenced this pull request Jan 9, 2024
# 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Assets Load files from disk to use for things like images, models, and sounds C-Enhancement A new feature S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants