Skip to content

Rust enum #[property] #544

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

Closed
WinstonHartnett opened this issue Jul 21, 2020 · 4 comments
Closed

Rust enum #[property] #544

WinstonHartnett opened this issue Jul 21, 2020 · 4 comments
Labels

Comments

@WinstonHartnett
Copy link

Am I wrong that for rust enums to be exported to the Inspector as enumerated strings, each Node has to add a property (w/ PropertyBuilder), a getter that returns a string representation of the enum variant, a setter that takes a string representation and converts it into an enum variant, and a StringHint::Enum(EnumHint::...? This seems rather verbose.

Can this instead be automated with #[property(...)]?

@ghost ghost added the question label Jul 21, 2020
@ghost
Copy link

ghost commented Jul 21, 2020

Adding an option to derive ToVariant and FromVariant using strings as discriminants would be possible. Generating the hints, however, is not. This is due to the fact that Rust attribute macros are macros, not run-time reflection. Macros can only operate on their input. It's not possible for the derive macro to know the variant names in another type defined elsewhere.

Writing a derive macro that generates static VARIANT_NAMES: &[&str] items for plain enums should be fairly straightforward, but I'm afraid that's outside the scope of godot-rust. You might want to search crates to see if it already exists.

@WinstonHartnett
Copy link
Author

The strum crate has enum helpers which includes an assoc. static VARIANTS: &[&str]. I wrote a simple derive macro for ToVariant and FromVariant but ExportInfo's fields are private, so how would I go about setting hint_string?

@ghost
Copy link

ghost commented Jul 23, 2020

Use StringHint::export_info.

@WinstonHartnett
Copy link
Author

Ah, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant