strum icon indicating copy to clipboard operation
strum copied to clipboard

IntoStaticStr Keep/Rename/Remove

Open Peternator7 opened this issue 5 years ago • 7 comments

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.

Peternator7 avatar Oct 07 '20 16:10 Peternator7

Have never used this one, if kept I think the name should stay as IntoStaticStr.

jplatte avatar Oct 30 '20 01:10 jplatte

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!

linclelinkpart5 avatar Nov 09 '20 21:11 linclelinkpart5

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.

jplatte avatar Nov 10 '20 00:11 jplatte

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 avatar Dec 20 '20 12:12 dzmitry-lahoda

@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 avatar Dec 30 '20 22:12 Peternator7

@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.

Logarithmus avatar Jun 01 '22 20:06 Logarithmus

@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.

Logarithmus avatar Oct 21 '22 11:10 Logarithmus