-
Notifications
You must be signed in to change notification settings - Fork 161
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
Add EnumVariantNames
#56
Add EnumVariantNames
#56
Conversation
This derive adds a static `variants()` methods yielding the names of the enum variants. This happens to be exactly what clap [wants][1], and is the last puzzle piece to use strum with clap/structopt. [1]: https://docs.rs/clap/2.33.0/clap/macro.arg_enum.html
Why introduce array while there is |
This returns a static array of static string slices and is meant as a convenience method. My main motivation is this: Clap's |
My main concern that such functionality already exists and new functionality duplicate it. May be it is possible to combine them to achive what you want without #[macro_use]
extern crate strum_macros;
use strum::{EnumCount, IntoEnumIterator};
#[derive(EnumCount, IntoStaticStr, EnumIter)]
enum Boo {
A,
B,
C,
}
fn get_me_static_array<E, I>() -> [&'static str; 3]
where
E: IntoEnumIterator<Iterator = I> + EnumCount + Into<&'static str>,
I: Iterator<Item = E>,
{
let mut arr: [&'static str; 3] = [""; 3/*something like <E as EnumCount>::COUNT*/];
for (idx, it) in E::iter().enumerate() {
arr[idx] = it.into();
}
arr
}
fn main() {
println!("{:?}", get_me_static_array::<Boo, _>());
} This requires only modification of |
Thanks for your comments! It seems I have misjudged the philosophy of this library, so I'm a bit surprised. You are suggesting to use three of the existing derives (and making one more complex in the process) to not add a new one that is 36 lines of code. I assumed that adding more derives to allow more conveniences was totally in scope. But I guess it is you who will have to maintain this, so it's not my call. Assuming I agree and we go ahead and introduce My goal is unchanged; I want to use this with clap/structopt without writing any boilerplate code. |
Take into consideration that I am not maintainer of this crate, I just user and contributor. And this is point of view of user of strum.
I suppose it can be added as generic "free" function, |
I appreciate the discussion and the PR! I agree it's a little redundant, but my inclination is to merge it anyway. Here's my thinking:
@killercup I'll give the code a review in the next day or 2 :) |
Ping on review? Not urgent, just one of the dependencies I use a local fork for right now :) |
Whoops I forgot to submit the review. thanks for the ping |
This reduces the risk of breaking the public API by accident when adding a new variant.
Thanks! That's super good feedback! I've pushed some updates that address all your comments and add tests asserting that this actually works with clap and structopt. |
This derive adds a static
variants()
methods yielding the names ofthe enum variants. This happens to be exactly what clap wants, and
is the last puzzle piece to use strum with clap/structopt.