Skip to content

Commit

Permalink
Add support for const_into_str attribute to enable static string co…
Browse files Browse the repository at this point in the history
…nversions in const contexts

resolves: Peternator7#352
  • Loading branch information
biryukovmaxim committed Apr 26, 2024
1 parent 410062e commit 07804d0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
4 changes: 4 additions & 0 deletions strum_macros/src/helpers/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod kw {

// enum metadata
custom_keyword!(serialize_all);
custom_keyword!(const_into_str);
custom_keyword!(use_phf);
custom_keyword!(prefix);

Expand Down Expand Up @@ -51,6 +52,7 @@ pub enum EnumMeta {
kw: kw::prefix,
prefix: LitStr,
},
ConstIntoStr(kw::const_into_str)
}

impl Parse for EnumMeta {
Expand Down Expand Up @@ -80,6 +82,8 @@ impl Parse for EnumMeta {
input.parse::<Token![=]>()?;
let prefix = input.parse()?;
Ok(EnumMeta::Prefix { kw, prefix })
} else if lookahead.peek(kw::const_into_str) {
Ok(EnumMeta::ConstIntoStr(input.parse()?))
} else {
Err(lookahead.error())
}
Expand Down
11 changes: 11 additions & 0 deletions strum_macros/src/helpers/type_props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct StrumTypeProperties {
pub use_phf: bool,
pub prefix: Option<LitStr>,
pub enum_repr: Option<TokenStream>,
pub const_into_str: bool,
}

impl HasTypeProperties for DeriveInput {
Expand All @@ -37,6 +38,8 @@ impl HasTypeProperties for DeriveInput {
let mut use_phf_kw = None;
let mut crate_module_path_kw = None;
let mut prefix_kw = None;
let mut const_into_str = None;

for meta in strum_meta {
match meta {
EnumMeta::SerializeAll { case_style, kw } => {
Expand Down Expand Up @@ -82,6 +85,14 @@ impl HasTypeProperties for DeriveInput {
prefix_kw = Some(kw);
output.prefix = Some(prefix);
}
EnumMeta::ConstIntoStr(kw) => {
if let Some(fst_kw) = const_into_str {
return Err(occurrence_error(fst_kw, kw, "const_into_str"));
}

const_into_str = Some(kw);
output.const_into_str = true;
}
}
}

Expand Down
24 changes: 23 additions & 1 deletion strum_macros/src/macros/strings/as_ref_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub fn as_static_str_inner(
}
}
},
GenerateTraitVariant::From => quote! {
GenerateTraitVariant::From if !type_properties.const_into_str => quote! {
impl #impl_generics ::core::convert::From<#name #ty_generics> for &'static str #where_clause {
fn from(x: #name #ty_generics) -> &'static str {
match x {
Expand All @@ -115,5 +115,27 @@ pub fn as_static_str_inner(
}
}
},
GenerateTraitVariant::From => quote! {
impl #name #ty_generics {
pub const fn into_str(&self) -> &'static str #where_clause {
match *self {
#(#arms3),*
}
}
}

impl #impl_generics ::core::convert::From<#name #ty_generics> for &'static str #where_clause {
fn from(x: #name #ty_generics) -> &'static str {
match x {
#(#arms2),*
}
}
}
impl #impl_generics2 ::core::convert::From<&'_derivative_strum #name #ty_generics> for &'static str #where_clause {
fn from(x: &'_derivative_strum #name #ty_generics) -> &'static str {
x.into_str()
}
}
},
})
}

0 comments on commit 07804d0

Please sign in to comment.