mirror of
https://github.com/danog/strum.git
synced 2024-11-26 20:14:40 +01:00
Tweak docs to format correctly and update crates to the newest version
This commit is contained in:
parent
c8cd49f4e3
commit
5272fcea91
16
CHANGELOG.md
16
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<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`
|
||||
|
30
README.md
30
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<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 |
|
||||
@ -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
|
||||
|
@ -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"
|
||||
@ -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" }
|
||||
|
@ -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"] }
|
||||
//! ```
|
||||
//!
|
||||
|
||||
|
@ -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"
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user