Skip to content

Commit

Permalink
Add AsStaticStr trait and let derive(AsRefStr) implement it
Browse files Browse the repository at this point in the history
This will solve Peternator7#23.
  • Loading branch information
lo48576 committed May 23, 2018
1 parent 179e8ea commit a1efb21
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
6 changes: 6 additions & 0 deletions strum/src/lib.rs
Expand Up @@ -515,3 +515,9 @@ pub trait EnumProperty {
Option::None
}
}


// FIXME: documentation.
pub trait AsStaticStr {
fn as_str(&self) -> &'static str;
}
11 changes: 10 additions & 1 deletion strum_macros/src/as_ref_str.rs
Expand Up @@ -48,12 +48,21 @@ pub fn as_ref_str_inner(ast: &syn::DeriveInput) -> quote::Tokens {
}

if arms.len() < variants.len() {
arms.push(quote!{ _ => panic!("AsRef<str>::as_ref() called on disabled variant.")})
arms.push(quote!{
_ => panic!("AsRef<str>::as_ref() or AsStaticStr::as_str() called on disabled variant.")
})
}

let arms = &arms;
quote!{
impl #impl_generics ::std::convert::AsRef<str> for #name #ty_generics #where_clause {
fn as_ref(&self) -> &str {
::strum::AsStaticStr::as_str(self)
}
}

impl #impl_generics ::strum::AsStaticStr for #name #ty_generics #where_clause {
fn as_str(&self) -> &'static str {
match *self {
#(#arms),*
}
Expand Down
6 changes: 6 additions & 0 deletions strum_tests/tests/as_ref_str.rs
Expand Up @@ -4,6 +4,8 @@ extern crate strum_macros;

use std::str::FromStr;

use strum::AsStaticStr;

#[derive(Debug,Eq,PartialEq,EnumString,AsRefStr)]
enum Color {
#[strum(to_string="RedRed")]
Expand All @@ -27,20 +29,24 @@ fn as_red_str() {
(Color::Red).as_ref());
assert_eq!(Color::Red,
Color::from_str((Color::Red).as_ref()).unwrap());
let _: &'static str = Color::Red.as_str();
}

#[test]
fn as_blue_str() {
assert_eq!("blue",
(Color::Blue { hue: 0 }).as_ref());
let _: &'static str = (Color::Blue { hue: 0 }).as_str();
}

#[test]
fn as_yellow_str() {
assert_eq!("yellow", (Color::Yellow).as_ref());
let _: &'static str = Color::Yellow.as_str();
}

#[test]
fn as_green_str() {
assert_eq!("Green", (Color::Green(String::default())).as_ref());
let _: &'static str = Color::Green(String::default()).as_str();
}

0 comments on commit a1efb21

Please sign in to comment.