diff --git a/CHANGELOG.md b/CHANGELOG.md index 74ed216..a3b66ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,21 @@ # Changelog +## 0.23.0 + +* [#185](https://github.com/Peternator7/strum/pull/185) Adds the `FromRepr` derive that adds a `from_repr(x: usize) -> Option` + method to your enum. This lets you convert integer values to your enum. If you specify a #[repr(..)] attribute on your enum, or use + an explicit discriminant, this will be incorporated into the derive. + * `from_repr` will be `const` if you use a recent rust version. + * This cannot be a trait method currently because only inherent methods support `const`. + +* [#186](https://github.com/Peternator7/strum/pull/186) Automatically implement `TryFrom` for enums that implement `EnumString`. + This is only enabled for rustc >= 1.34 which is when `TryFrom was stabilized. + * This is a small breaking change. If you had manually implemented `TryFrom` for your enum, this will cause a conflict. You + can probably remove your manual implementation. + +* [#189](https://github.com/Peternator7/strum/pull/189) Use `core::result::Result` instead of `std::result::Result`. This should be + more portable in no-std environments. + ## 0.22.0 * [#180](https://github.com/Peternator7/strum/pull/180): Deprecates `ToString` derive. You should use `Display` diff --git a/README.md b/README.md index 5aa1732..ec4c2b3 100644 --- a/README.md +++ b/README.md @@ -22,11 +22,11 @@ Cargo.toml. Strum_macros contains the macros needed to derive all the traits in ```toml [dependencies] -strum = "0.22" -strum_macros = "0.22" +strum = "0.23" +strum_macros = "0.23" # You can also use the "derive" feature, and import the macros directly from "strum" -# strum = { version = "0.22", features = ["derive"] } +# strum = { version = "0.23", features = ["derive"] } ``` # Strum Macros @@ -35,8 +35,9 @@ Strum has implemented the following macros: | Macro | Description | | --- | ----------- | -| [EnumString] | Converts strings to enum variants based on their name | +| [EnumString] | Converts strings to enum variants based on their name. | | [Display] | Converts enum variants to strings | +| [FromRepr] | Convert from an integer to an enum. | | [AsRefStr] | Implement `AsRef` for `MyEnum` | | [IntoStaticStr] | Implements `From for &'static str` on an enum | | [EnumVariantNames] | Adds an associated `VARIANTS` constant which is an array of discriminant names | @@ -67,13 +68,14 @@ information through strings. Strumming is also a very whimsical motion, much like writing Rust code. [Macro-Renames]: https://github.com/Peternator7/strum/wiki/Macro-Renames -[EnumString]: https://docs.rs/strum_macros/0.22/strum_macros/derive.EnumString.html -[Display]: https://docs.rs/strum_macros/0.22/strum_macros/derive.Display.html -[AsRefStr]: https://docs.rs/strum_macros/0.22/strum_macros/derive.AsRefStr.html -[IntoStaticStr]: https://docs.rs/strum_macros/0.22/strum_macros/derive.IntoStaticStr.html -[EnumVariantNames]: https://docs.rs/strum_macros/0.22/strum_macros/derive.EnumVariantNames.html -[EnumIter]: https://docs.rs/strum_macros/0.22/strum_macros/derive.EnumIter.html -[EnumProperty]: https://docs.rs/strum_macros/0.22/strum_macros/derive.EnumProperty.html -[EnumMessage]: https://docs.rs/strum_macros/0.22/strum_macros/derive.EnumMessage.html -[EnumDiscriminants]: https://docs.rs/strum_macros/0.22/strum_macros/derive.EnumDiscriminants.html -[EnumCount]: https://docs.rs/strum_macros/0.22/strum_macros/derive.EnumCount.html +[EnumString]: https://docs.rs/strum_macros/0.23/strum_macros/derive.EnumString.html +[Display]: https://docs.rs/strum_macros/0.23/strum_macros/derive.Display.html +[AsRefStr]: https://docs.rs/strum_macros/0.23/strum_macros/derive.AsRefStr.html +[IntoStaticStr]: https://docs.rs/strum_macros/0.23/strum_macros/derive.IntoStaticStr.html +[EnumVariantNames]: https://docs.rs/strum_macros/0.23/strum_macros/derive.EnumVariantNames.html +[EnumIter]: https://docs.rs/strum_macros/0.23/strum_macros/derive.EnumIter.html +[EnumProperty]: https://docs.rs/strum_macros/0.23/strum_macros/derive.EnumProperty.html +[EnumMessage]: https://docs.rs/strum_macros/0.23/strum_macros/derive.EnumMessage.html +[EnumDiscriminants]: https://docs.rs/strum_macros/0.23/strum_macros/derive.EnumDiscriminants.html +[EnumCount]: https://docs.rs/strum_macros/0.23/strum_macros/derive.EnumCount.html +[FromRepr]: https://docs.rs/strum_macros/0.23/strum_macros/derive.FromRepr.html diff --git a/strum/Cargo.toml b/strum/Cargo.toml index 2d0d373..147c7a9 100644 --- a/strum/Cargo.toml +++ b/strum/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "strum" -version = "0.22.0" +version = "0.23.0" edition = "2018" authors = ["Peter Glotfelty "] license = "MIT" @@ -15,10 +15,10 @@ repository = "https://github.com/Peternator7/strum" readme = "../README.md" [dependencies] -strum_macros = { path = "../strum_macros", optional = true, version = "0.22" } +strum_macros = { path = "../strum_macros", optional = true, version = "0.23" } [dev-dependencies] -strum_macros = { path = "../strum_macros", version = "0.22" } +strum_macros = { path = "../strum_macros", version = "0.23" } [badges] travis-ci = { repository = "Peternator7/strum" } diff --git a/strum/src/lib.rs b/strum/src/lib.rs index fe12b92..aeff2be 100644 --- a/strum/src/lib.rs +++ b/strum/src/lib.rs @@ -16,11 +16,11 @@ //! //! ```toml //! [dependencies] -//! strum = "0.22" -//! strum_macros = "0.22" +//! strum = "0.23" +//! strum_macros = "0.23" //! //! # You can also access strum_macros exports directly through strum using the "derive" feature -//! strum = { version = "0.22", features = ["derive"] } +//! strum = { version = "0.23", features = ["derive"] } //! ``` //! diff --git a/strum_macros/Cargo.toml b/strum_macros/Cargo.toml index fcf643c..e83a6bb 100644 --- a/strum_macros/Cargo.toml +++ b/strum_macros/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "strum_macros" -version = "0.22.0" +version = "0.23.0" edition = "2018" authors = ["Peter Glotfelty "] license = "MIT" diff --git a/strum_macros/src/lib.rs b/strum_macros/src/lib.rs index 1ccaf5b..9d9ff89 100644 --- a/strum_macros/src/lib.rs +++ b/strum_macros/src/lib.rs @@ -421,29 +421,34 @@ pub fn enum_iter(input: proc_macro::TokenStream) -> proc_macro::TokenStream { /// /// assert_eq!(None, Vehicle::from_repr(0)); /// ``` -#[rustversion::attr(since(1.46),doc=" -`const` tests (only works in rust >= 1.46) -``` -use strum_macros::FromRepr; - -#[derive(FromRepr, Debug, PartialEq)] -#[repr(u8)] -enum Number { - One = 1, - Three = 3, -} - -// This test confirms that the function works in a `const` context -const fn number_from_repr(d: u8) -> Option { - Number::from_repr(d) -} -assert_eq!(None, number_from_repr(0)); -assert_eq!(Some(Number::One), number_from_repr(1)); -assert_eq!(None, number_from_repr(2)); -assert_eq!(Some(Number::Three), number_from_repr(3)); -assert_eq!(None, number_from_repr(4)); -``` -")] +/// +/// On versions of rust >= 1.46, the `from_repr` function is marked `const`. +/// +/// ```rust +/// use strum_macros::FromRepr; +/// +/// #[derive(FromRepr, Debug, PartialEq)] +/// #[repr(u8)] +/// enum Number { +/// One = 1, +/// Three = 3, +/// } +/// +/// # #[rustversion::since(1.46)] +/// const fn number_from_repr(d: u8) -> Option { +/// Number::from_repr(d) +/// } +/// +/// # #[rustversion::before(1.46)] +/// # fn number_from_repr(d: u8) -> Option { +/// # Number::from_repr(d) +/// # } +/// assert_eq!(None, number_from_repr(0)); +/// assert_eq!(Some(Number::One), number_from_repr(1)); +/// assert_eq!(None, number_from_repr(2)); +/// assert_eq!(Some(Number::Three), number_from_repr(3)); +/// assert_eq!(None, number_from_repr(4)); +/// ``` #[proc_macro_derive(FromRepr, attributes(strum))] pub fn from_repr(input: proc_macro::TokenStream) -> proc_macro::TokenStream {