-
Notifications
You must be signed in to change notification settings - Fork 150
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
IntoStaticStr but from immutable reference to an enum From<&MyEnum> #291
Comments
#131 AsStr would solve it #131 (comment) |
maybe also duplicate of #152 ? |
Aww, it actually works with the depracated feature. Can you undeprecate it? or maybe just rename to as_str as suggested in #131 (comment) ? use strum::{AsStaticRef, AsStaticStr};
#[derive(AsStaticStr)]
enum MyEnum {
Foo,
}
fn main() {
let v = MyEnum::Foo;
let str3: &'static str = v.as_static();
println!("{}", str3);
drop(v);
} |
How about pub trait AsStr{
fn as_str(&self) -> &'static str
} Plus also if the |
This should already work, but you might need to play with the de-refs to get rustc to pick the right impl. Check out this test for an example |
you are correct, it works. So I turn this into a question. I don't know how to declare Into<&'static str> trait so it works for both Enum and &Enum. Whereas with What trait should be added to use strum::{AsStaticRef, AsStaticStr, IntoStaticStr};
trait EnumIntoStaticStr: Into<&'static str> {}
impl<T: Into<&'static str>> EnumIntoStaticStr for T {}
trait EnumAsStaticStr: AsStaticRef<str> {}
impl<T: AsStaticRef<str>> EnumAsStaticStr for T {}
#[derive(AsStaticStr)]
enum MyEnum {
Foo,
Bar(String),
}
#[derive(IntoStaticStr)]
enum MyEnum2 {
Foo2,
Bar(String),
}
fn main() {
let v = MyEnum::Foo;
let str = borrow_as_str(&v);
drop(v);
println!("{}", str);
let v2 = MyEnum2::Foo2;
let str2 = borrow_into_str(&v2);
drop(v2);
println!("{}", str2);
}
fn move_as_str(v: impl EnumAsStaticStr) -> &'static str {
v.as_static()
}
fn borrow_as_str(v: &impl EnumAsStaticStr) -> &'static str {
v.as_static()
}
fn move_into_str(v: impl EnumIntoStaticStr) -> &'static str {
v.into()
}
fn borrow_into_str(v: &impl EnumIntoStaticStr) -> &'static str {
v.into()
} Error: error[E0277]: the trait bound `&str: From<&impl EnumIntoStaticStr>` is not satisfied
--> src/main.rs:45:7
|
45 | v.into()
| ^^^^ the trait `From<&impl EnumIntoStaticStr>` is not implemented for `&str`
|
= note: required for `&impl EnumIntoStaticStr` to implement `Into<&str>`
help: consider dereferencing here
|
45 | (*v).into()
| ++ +
For more information about this error, try `rustc --explain E0277`.``` |
I got my answer on stackoverflow https://stackoverflow.com/questions/77120723/how-to-declare-supertraits-intot-so-it-works-with-both-move-and-borrow/77122298#77122298 I would vouch for keeping Apart from that this issue can be closed from my side |
I need IntoStaticStr but from immutable reference to an enum From<&MyEnum> for &'static str on an &Enum is it possible to add?
Because without it the only way is to clone the enum to get the static string by using .into()
.as_ref() doesn't work either because it doesn't return the 'static lifetime
Or am I just missing some rust construct that can easily achieve this?
The text was updated successfully, but these errors were encountered: