Comments (7)
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
from strum.
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 AsStaticStr
because there is a difference from IntoStaticStr
as shown in previous comment.
Apart from that this issue can be closed from my side
from strum.
#131 AsStr would solve it #131 (comment)
from strum.
maybe also duplicate of #152 ?
from strum.
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);
}
from strum.
How about AsStaticRef<str>
-> AsStr
and AsStaticStr
-> AsStr
pub trait AsStr{
fn as_str(&self) -> &'static str
}
Plus also if the From<&MyEnum>
is possible for IntoStaticStr
from strum.
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 AsStaticStr
it works. Here is the code I want to make work.
What trait should be added to EnumIntoStaticStr
so it compiles?
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`.```
from strum.
Related Issues (20)
- Proposal: Exclude macro for EnumIter macro
- `EnumString` match with non-default variables HOT 1
- Feature request: A function to convert to integer by variant HOT 1
- EnumIter does not work for enums named 'Option'
- [Feature] Macro to Implement PartialEq<str> HOT 1
- strum::Display doesn't work with non-unit non-string variants
- `EnumString` should derive `From<&str>` instead of `TryFrom<&str>` when `#[strum(default)]` is present HOT 1
- to_string and serialize when used together causes unreachable code HOT 2
- `get_bool` and `get_int` in `EnumProperty` are undocumented and always return None
- AsRef<str> and default variants
- EnumDiscriminants: can't infer type HOT 3
- Getting the discriminant from an enum variant HOT 3
- Publish new release. HOT 4
- AsRefStr documentation incorrectly states 'static ref is returned HOT 2
- Publish Git tags HOT 2
- TryFrom<Repr> for enums using FromRepr? HOT 2
- Implement `Iterator` for EnumTable HOT 6
- Customize error message for FromStr
- Documentation for `VariantArray` and `VariantNames` say their `VARIANTS` constants are arrays, but they're actually slices HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from strum.