Skip to content

Commit

Permalink
Tweak docs to format correctly and update crates to the newest version
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Glotfelty committed Nov 17, 2021
1 parent c8cd49f commit 5272fce
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 44 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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<Self>`
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<str>` 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<str>` 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`
Expand Down
30 changes: 16 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<str>` for `MyEnum` |
| [IntoStaticStr] | Implements `From<MyEnum> for &'static str` on an enum |
| [EnumVariantNames] | Adds an associated `VARIANTS` constant which is an array of discriminant names |
Expand Down Expand Up @@ -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
6 changes: 3 additions & 3 deletions strum/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "strum"
version = "0.22.0"
version = "0.23.0"
edition = "2018"
authors = ["Peter Glotfelty <peter.glotfelty@microsoft.com>"]
license = "MIT"
Expand All @@ -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" }
Expand Down
6 changes: 3 additions & 3 deletions strum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
//! ```
//!

Expand Down
2 changes: 1 addition & 1 deletion strum_macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "strum_macros"
version = "0.22.0"
version = "0.23.0"
edition = "2018"
authors = ["Peter Glotfelty <peter.glotfelty@microsoft.com>"]
license = "MIT"
Expand Down
51 changes: 28 additions & 23 deletions strum_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
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> {
/// Number::from_repr(d)
/// }
///
/// # #[rustversion::before(1.46)]
/// # fn number_from_repr(d: u8) -> Option<Number> {
/// # 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 {
Expand Down

0 comments on commit 5272fce

Please sign in to comment.