strum
strum copied to clipboard
IntoStaticStr Keep/Rename/Remove
Evaluating the naming scheme and value of all the macros used in strum. Some are more or less redundant such as ToString now that Display exists.
This is a tracking issue to solicit feedback about the usefulness of IntoStaticStr and whether it should be kept and if so, what it's name should be.
Have never used this one, if kept I think the name should stay as IntoStaticStr.
Isn't this the same as AsRefStr, since this also generates a From<&'a YourEnum> impl? I would vote to combine this and AsRefStr, and call the resulting trait AsStr, or something close to that.
That being said, I feel this is a very useful feature, and I would like to see it kept!
AsRefStr derives, as its name implies, AsRef<str>. That doesn't allow obtaining a 'static string slice from a non-'static enum value, which this does.
We have something like this in code:
pub trait Named {
const NAME: &'static str;
}
Would be nice to have structural equivalent in strum for this. I.e. AsStaticStr to be trait.
@dzmitry-lahoda is NAME the name of the Enum type in this case? This somewhat exists in libstd. https://doc.rust-lang.org/std/any/fn.type_name.html
@Peternator7 I advise you to remove both IntoStaticStr & AsRefStr, but introduce AsStr instead, which will add as_str() method to enum, similar to String::as_str. When I first started using strum, I've very surprised by lack of such a macro.
@a1ien why? I mean
introduce a trait AsStr
pub trait AsStr {
fn as_str(&self) -> &'static str;
}
#[derive(AsStr)] would generate code like this:
impl AsStr for MyEnum {
fn as_str(&self) -> &'static str {
match self {
Self::Foo => "foo",
Self::Bar => "bar",
}
}
}
You could name the trait AsStaticStr if you wish.
This is the most universal & ergonomic way. You can use the resulting &'static str in static contexts, or convert it to String.