Skip to content

Commit

Permalink
fix: fix const into str implementation, add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
biryukovmaxim committed May 13, 2024
1 parent 07804d0 commit c37398b
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
4 changes: 2 additions & 2 deletions strum_macros/src/macros/strings/as_ref_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ pub fn as_static_str_inner(
}
},
GenerateTraitVariant::From => quote! {
impl #name #ty_generics {
pub const fn into_str(&self) -> &'static str #where_clause {
impl #impl_generics #name #ty_generics #where_clause {
pub const fn into_str(&self) -> &'static str {
match *self {
#(#arms3),*
}
Expand Down
74 changes: 74 additions & 0 deletions strum_tests/tests/as_ref_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,77 @@ fn brightness_serialize_all() {
assert_eq!("dim", <&'static str>::from(Brightness::Dim { glow: 0 }));
assert_eq!("Bright", <&'static str>::from(Brightness::BrightWhite));
}

#[derive(IntoStaticStr)]
#[strum(const_into_str)]
enum Bar<'a, T>
where
T: AsRef<str>,
{
A(T),
B,
C(&'a i32),
#[strum(serialize = "Dark")]
D,
#[strum(to_string = "Green")]
G,
#[strum(serialize = "b", to_string = "blue")]
Blue { hue: usize },
#[strum(serialize = "y", serialize = "yellow")]
Yellow,
}

#[derive(IntoStaticStr)]
#[strum(const_into_str)]
enum Baz<'a, T> {
A(T),
C(&'a i32),
}

#[derive(IntoStaticStr)]
#[strum(serialize_all = "snake_case")]
#[strum(const_into_str)]
enum BrightnessConst {
DarkBlack,
Dim {
glow: usize,
},
#[strum(serialize = "Bright")]
BrightWhite,
}

#[test]
fn test_const_into_static_str() {

const A: &'static str = Bar::A("foo").into_str();
assert_eq!("A", A);
const B: &'static str = Bar::B::<&str>.into_str();
assert_eq!("B", B);
const C: &'static str = Bar::C::<&str>(&12).into_str();
assert_eq!("C", C);

const D: &'static str = Bar::D::<&str>.into_str();
assert_eq!("Dark", D);

const G: &'static str = Bar::G::<&str>.into_str();
assert_eq!("Green", G);

const BLUE: &'static str = Bar::Blue::<&str>{ hue: 2 }.into_str();
assert_eq!("blue", BLUE);

const YELLOW: &'static str = Bar::Yellow::<&str>.into_str();
assert_eq!("yellow", YELLOW);

const BAZ_A: &'static str = Baz::A("foo").into_str();
assert_eq!("A", BAZ_A);

const BAZ_C: &'static str = Baz::C::<&str>(&6).into_str();
assert_eq!("C", BAZ_C);

const DARK_BLACK: &'static str = BrightnessConst::DarkBlack.into_str();
assert_eq!("dark_black", DARK_BLACK);
const DIM: &'static str = BrightnessConst::Dim {glow:1}.into_str();
assert_eq!("dim", DIM);
const BRIGHT_WHITE: &'static str = BrightnessConst::BrightWhite.into_str();
assert_eq!("Bright", BRIGHT_WHITE);
}

0 comments on commit c37398b

Please sign in to comment.