-
-
Save luser/25bd25c12c15f282fd666baefab1dce4 to your computer and use it in GitHub Desktop.
Rust enum with attached strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::slice::Iter; | |
use std::collections::HashSet; | |
trait EnumValues : Sized { | |
fn values() -> Iter<'static, Self>; | |
} | |
macro_rules! simple_enum { | |
($name:ident { $(($item:ident, $repr:expr),)* }) => { | |
#[derive(Debug)] | |
enum $name { | |
$( | |
$item, | |
)* | |
} | |
use std::fmt; | |
impl fmt::Display for $name { | |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
write!(f, "{}", match self { | |
$( | |
&$name::$item => $repr, | |
)* | |
}) | |
} | |
} | |
use std::str::FromStr; | |
impl FromStr for $name { | |
type Err = (); | |
fn from_str(s: &str) -> Result<$name, ()> { | |
match s { | |
$( | |
$repr => Ok($name::$item), | |
)* | |
_ => Err(()), | |
} | |
} | |
} | |
impl EnumValues for $name { | |
fn values() -> Iter<'static, $name> { | |
static VALUES: &'static [$name] = &[$($name::$item),*]; | |
VALUES.into_iter() | |
} | |
} | |
} | |
} | |
simple_enum!{ | |
Foo { | |
(A, "a"), | |
(B, "b"), | |
} | |
} | |
fn main() { | |
let s = Foo::values().map(|v| v.to_string()).collect::<HashSet<String>>(); | |
for v in Foo::values() { | |
println!("{} ({:?}): {}", v, v, s.contains(&v.to_string())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment