You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My solution would be to implement a getter for each item.
fnmain(){let mine = MyEnum::Foo{bar:42,baz:"In the clearing stands a boxer".to_string()};// Items are named after the enum, and then their name in snake_caseprintln!("bar is: {}", mine.foo_bar());println!("baz is: {}", mine.foo_baz());}
My only concern is that this can get wordy when using snake_case.
If you think you have something more ergonomic (or overall better!) please suggest it here.
The text was updated successfully, but these errors were encountered:
I slept on this and I thought of another solution, which would be to generate a new struct for each struct variant. For example,
#[derive(EnumGetters)]enumMyEnum{Foo{bar:i32,baz:String},// ... the rest}
would generate a new struct:
structMyEnumFoo{bar:i32,baz:String,}
and have that be the return type of foo():
fnmain(){let mine = MyEnum::Foo{bar:42,baz:"In the clearing stands a boxer".to_string()};let foo = mine.foo();println!("bar is: {}",&foo.bar);println!("baz is: {}",&foo.baz);}
The upsides:
Solves this problem
Gives us a natural foo.bar struct access.
The downsides:
How do we generate unique struct names that still produce useful error messages?
e.g. I don't want to have MyEnumFoo_2Pr8iigLwXYxJL4ScUHs because getting an error message
with that would be weird and confusing.
Bloaty code - not sure how much of a footprint having an individual struct definition for each struct variant would have on the final binary
For example,
currently does not create an implementation.
My solution would be to implement a getter for each item.
My only concern is that this can get wordy when using
snake_case
.If you think you have something more ergonomic (or overall better!) please suggest it here.
The text was updated successfully, but these errors were encountered: