Skip to content

Commit

Permalink
enum messages: Make the returned values all 'static (#160)
Browse files Browse the repository at this point in the history
This is (at least in theory) a breaking change, because someone might
have implemented this trait by hand, and their implementation won't
be declared as returning 'static lifetimes.

Closes #159

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
  • Loading branch information
ijackson authored May 18, 2021
1 parent f71830c commit 3869b6f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions strum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ pub trait IntoEnumIterator: Sized {
/// assert_eq!("I have a dog", my_pet.get_message().unwrap());
/// ```
pub trait EnumMessage {
fn get_message(&self) -> Option<&str>;
fn get_detailed_message(&self) -> Option<&str>;
fn get_serializations(&self) -> &[&str];
fn get_message(&self) -> Option<&'static str>;
fn get_detailed_message(&self) -> Option<&'static str>;
fn get_serializations(&self) -> &'static [&'static str];
}

/// EnumProperty is a trait that makes it possible to store additional information
Expand Down
6 changes: 3 additions & 3 deletions strum_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,23 +396,23 @@ pub fn enum_iter(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
/// // Generated code looks like more or less like this:
/// /*
/// impl ::strum::EnumMessage for Color {
/// fn get_message(&self) -> ::std::option::Option<&str> {
/// fn get_message(&self) -> ::std::option::Option<&'static str> {
/// match self {
/// &Color::Red => ::std::option::Option::Some("Red"),
/// &Color::Green {..} => ::std::option::Option::Some("Simply Green"),
/// _ => None
/// }
/// }
///
/// fn get_detailed_message(&self) -> ::std::option::Option<&str> {
/// fn get_detailed_message(&self) -> ::std::option::Option<&'static str> {
/// match self {
/// &Color::Red => ::std::option::Option::Some("This is very red"),
/// &Color::Green {..}=> ::std::option::Option::Some("Simply Green"),
/// _ => None
/// }
/// }
///
/// fn get_serializations(&self) -> &[&str] {
/// fn get_serializations(&self) -> &'static [&'static str] {
/// match self {
/// &Color::Red => {
/// static ARR: [&'static str; 1] = ["Red"];
Expand Down
6 changes: 3 additions & 3 deletions strum_macros/src/macros/enum_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,19 @@ pub fn enum_message_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {

Ok(quote! {
impl #impl_generics ::strum::EnumMessage for #name #ty_generics #where_clause {
fn get_message(&self) -> ::core::option::Option<&str> {
fn get_message(&self) -> ::core::option::Option<&'static str> {
match self {
#(#arms),*
}
}

fn get_detailed_message(&self) -> ::core::option::Option<&str> {
fn get_detailed_message(&self) -> ::core::option::Option<&'static str> {
match self {
#(#detailed_arms),*
}
}

fn get_serializations(&self) -> &[&str] {
fn get_serializations(&self) -> &'static [&'static str] {
match self {
#(#serializations),*
}
Expand Down

0 comments on commit 3869b6f

Please sign in to comment.