diff --git a/CHANGELOG.md b/CHANGELOG.md index c9f539ef..bf22885f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ # Changelog -## Unreleased +## 0.13.0 + +### Added + +* Added a derive to implement `From` for `&'static str`. This deprecates `AsStaticStr` since + the new solution doesn't require a `strum` specific trait to use. + +## 0.12.0 ### Added diff --git a/LICENSE b/LICENSE index f0871c0f..a4bb9116 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2017 Peter Glotfelty +Copyright (c) 2018 Peter Glotfelty Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 1a55680e..db8e14b4 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,8 @@ Cargo.toml. Strum_macros contains the macros needed to derive all the traits in ```toml [dependencies] -strum = "0.12.0" -strum_macros = "0.12.0" +strum = "0.13.0" +strum_macros = "0.13.0" ``` And add these lines to the root of your project, either lib.rs or main.rs. @@ -135,17 +135,15 @@ Strum has implemented the following macros: `ToString` for determining what string is returned. The difference is that `as_ref()` returns a `&str` instead of a `String` so you don't allocate any additional memory with each call. -4. `AsStaticStr`: this is similar to `AsRefStr`, but returns a `'static` reference to a string which is helpful - in some scenarios. This macro implements `strum::AsStaticRef` which adds a method `.to_static()` that - returns a `&'static str`. +4. `IntoStaticStr`: this trait implements `From` and `From<&'a YourEnum>` for `&'static str`. This is + useful for turning an enum variant into a static string. The Rust `std` provides a blanket impl of the + reverse direction - i.e. `impl Into<&'static str> for YourEnum`. ```rust - extern crate strum; + extern crate strum; #[macro_use] extern crate strum_macros; - - use strum::AsStaticRef; - #[derive(AsStaticStr)] + #[derive(IntoStaticStr)] enum State<'a> { Initial(&'a str), Finished @@ -155,13 +153,36 @@ Strum has implemented the following macros: let state = State::Initial(s); // The following won't work because the lifetime is incorrect so we can use.as_static() instead. // let wrong: &'static str = state.as_ref(); - let right: &'static str = state.as_static(); + let right: &'static str = state.into(); println!("{}", right); } fn main() { print_state(&"hello world".to_string()) } + ``` + +4. `AsStaticStr`: **Deprecated since version 0.13.0. Prefer IntoStaticStr instead.** + This is similar to `AsRefStr`, but returns a `'static` reference to a string which is helpful + in some scenarios. This macro implements `strum::AsStaticRef` which adds a method `.to_static()` that + returns a `&'static str`. + + ```rust + extern crate strum; + #[macro_use] extern crate strum_macros; + + use strum::AsStaticRef; + + #[derive(AsStaticStr)] + enum State<'a> { + Initial(&'a str), + Finished + } + + fn print_state<'a>(s:&'a str) { + let right: &'static str = State::Initial(s).as_static(); + println!("{}", right); + } ``` 4. `EnumIter`: iterate over the variants of an Enum. Any additional data on your variants will be diff --git a/strum/Cargo.toml b/strum/Cargo.toml index 2c1e1542..603d0e0d 100644 --- a/strum/Cargo.toml +++ b/strum/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "strum" -version = "0.12.0" +version = "0.13.0" authors = ["Peter Glotfelty "] license = "MIT" @@ -13,7 +13,7 @@ homepage = "https://github.com/Peternator7/strum" readme = "../README.md" [dev-dependencies] -strum_macros = { path = "../strum_macros", version = "0.12.0" } +strum_macros = { path = "../strum_macros", version = "0.13.0" } [badges] travis-ci = { repository = "Peternator7/strum" } \ No newline at end of file diff --git a/strum/src/lib.rs b/strum/src/lib.rs index 5e596e88..e34a8393 100644 --- a/strum/src/lib.rs +++ b/strum/src/lib.rs @@ -14,8 +14,8 @@ //! //! ```toml //! [dependencies] -//! strum = "0.11.0" -//! strum_macros = "0.11.0" +//! strum = "0.13.0" +//! strum_macros = "0.13.0" //! ``` //! //! And add these lines to the root of your project, either lib.rs or main.rs. @@ -122,34 +122,33 @@ //! 3. `AsRefStr`: this derive implements `AsRef` on your enum using the same rules as //! `ToString` for determining what string is returned. The difference is that `as_ref()` returns //! a borrowed `str` instead of a `String` so you can save an allocation. +//! +//! 4. `IntoStaticStr`: this trait implements `From` and `From<&'a YourEnum>` for `&'static str`. This is +//! useful for turning an enum variant into a static string. The Rust `std` provides a blanket impl of the +//! reverse direction - i.e. `impl Into<&'static str> for YourEnum`. //! -//! 4. `AsStaticStr`: this is similar to `AsRefStr`, but returns a `'static` reference to a string which is helpful -//! in some scenarios. This macro implements `strum::AsStaticRef` which adds a method `.as_static()` that -//! returns a `&'static str`. -//! -//! ```rust -//! # extern crate strum; -//! # #[macro_use] extern crate strum_macros; -//! use strum::AsStaticRef; -//! -//! #[derive(AsStaticStr)] -//! enum State<'a> { -//! Initial(&'a str), -//! Finished -//! } -//! -//! fn print_state<'a>(s:&'a str) { -//! let state = State::Initial(s); -//! // The following won't work because the lifetime is incorrect so we can use.as_static() instead. -//! // let wrong: &'static str = state.as_ref(); -//! let right: &'static str = state.as_static(); -//! println!("{}", right); -//! } -//! -//! fn main() { -//! print_state(&"hello world".to_string()) -//! } -//! ``` +//! ```rust +//! extern crate strum; +//! #[macro_use] extern crate strum_macros; +//! +//! #[derive(IntoStaticStr)] +//! enum State<'a> { +//! Initial(&'a str), +//! Finished +//! } +//! +//! fn print_state<'a>(s:&'a str) { +//! let state = State::Initial(s); +//! // The following won't work because the lifetime is incorrect so we can use.as_static() instead. +//! // let wrong: &'static str = state.as_ref(); +//! let right: &'static str = state.into(); +//! println!("{}", right); +//! } +//! +//! fn main() { +//! print_state(&"hello world".to_string()) +//! } +//! ``` //! //! 4. `EnumIter`: iterate over the variants of an Enum. Any additional data on your variants will be //! set to `Default::default()`. The macro implements `strum::IntoEnumIter` on your enum and diff --git a/strum_macros/Cargo.toml b/strum_macros/Cargo.toml index 942b54b3..364acc83 100644 --- a/strum_macros/Cargo.toml +++ b/strum_macros/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "strum_macros" -version = "0.12.0" +version = "0.13.0" authors = ["Peter Glotfelty "] license = "MIT" diff --git a/strum_tests/Cargo.toml b/strum_tests/Cargo.toml index f9b9fed7..56c2a18b 100644 --- a/strum_tests/Cargo.toml +++ b/strum_tests/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "strum_tests" -version = "0.12.0" +version = "0.13.0" authors = ["Peter Glotfelty "] [dependencies]