Code Monkey home page Code Monkey logo

Comments (7)

Peternator7 avatar Peternator7 commented on May 24, 2024 1

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.

bitcoin-eagle avatar bitcoin-eagle commented on May 24, 2024 1

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.

bitcoin-eagle avatar bitcoin-eagle commented on May 24, 2024

#131 AsStr would solve it #131 (comment)

from strum.

bitcoin-eagle avatar bitcoin-eagle commented on May 24, 2024

maybe also duplicate of #152 ?

from strum.

bitcoin-eagle avatar bitcoin-eagle commented on May 24, 2024

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.

bitcoin-eagle avatar bitcoin-eagle commented on May 24, 2024

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.

bitcoin-eagle avatar bitcoin-eagle commented on May 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.