From 0091ac77d90f031cfb8f2ce3e6b7f42827051c56 Mon Sep 17 00:00:00 2001 From: Azriel Hoh Date: Thu, 20 Sep 2018 09:30:16 +1200 Subject: [PATCH] Updated `README.md` and lib.rs docs. Issue #33 --- README.md | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ strum/src/lib.rs | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) diff --git a/README.md b/README.md index d3d80f37..420c92c0 100644 --- a/README.md +++ b/README.md @@ -296,6 +296,67 @@ Strum has implemented the following macros: } ``` +7. `EnumDiscriminants`: Given an enum named `MyEnum`, generates another enum called + `MyEnumDiscriminants` with the same variants, without any data fields. This is useful when you + wish to determine the variant of an enum from a String, but the variants contain any + non-`Default` fields. By default, the generated enum has the followign derives: + `Clone, Copy, Debug, PartialEq, Eq`. You can add additional derives using the + `#[strum_discriminants_derive(AdditionalDerive)]` attribute. + + Here's an example: + + ```rust + extern crate strum; + #[macro_use] extern crate strum_macros; + + // Bring trait into scope + use std::str::FromStr; + + #[derive(Debug)] + struct NonDefault; + + #[allow(dead_code)] + #[derive(Debug, EnumDiscriminants)] + #[strum_discriminants_derive(EnumString)] + enum MyEnum { + Variant0(NonDefault), + Variant1 { a: NonDefault }, + } + + fn main() { + assert_eq!( + MyEnumDiscriminants::Variant0, + MyEnumDiscriminants::from_str("Variant0").unwrap() + ); + } + ``` + + You can also rename the generated enum using the `#[strum_discriminants_name(OtherName)]` + attribute: + + ```rust + extern crate strum; + #[macro_use] extern crate strum_macros; + // You need to bring the type into scope to use it!!! + use strum::IntoEnumIterator; + + #[allow(dead_code)] + #[derive(Debug, EnumDiscriminants)] + #[strum_discriminants_derive(EnumIter)] + #[strum_discriminants_name(MyVariants)] + enum MyEnum { + Variant0(bool), + Variant1 { a: bool }, + } + + fn main() { + assert_eq!( + vec![MyVariants::Variant0, MyVariants::Variant1], + MyVariants::iter().collect::>() + ); + } + ``` + # Additional Attributes Strum supports several custom attributes to modify the generated code. At the enum level, the diff --git a/strum/src/lib.rs b/strum/src/lib.rs index 7a7b597b..7e238ea3 100644 --- a/strum/src/lib.rs +++ b/strum/src/lib.rs @@ -290,6 +290,66 @@ //! } //! ``` //! +//! 7. `EnumDiscriminants`: Given an enum named `MyEnum`, generates another enum called +//! `MyEnumDiscriminants` with the same variants, without any data fields. This is useful when you +//! wish to determine the variant of an enum from a String, but the variants contain any +//! non-`Default` fields. By default, the generated enum has the followign derives: +//! `Clone, Copy, Debug, PartialEq, Eq`. You can add additional derives using the +//! `#[strum_discriminants_derive(AdditionalDerive)]` attribute. +//! +//! Here's an example: +//! +//! ```rust +//! # extern crate strum; +//! # #[macro_use] extern crate strum_macros; +//! +//! // Bring trait into scope +//! use std::str::FromStr; +//! +//! #[derive(Debug)] +//! struct NonDefault; +//! +//! #[allow(dead_code)] +//! #[derive(Debug, EnumDiscriminants)] +//! #[strum_discriminants_derive(EnumString)] +//! enum MyEnum { +//! Variant0(NonDefault), +//! Variant1 { a: NonDefault }, +//! } +//! +//! fn main() { +//! assert_eq!( +//! MyEnumDiscriminants::Variant0, +//! MyEnumDiscriminants::from_str("Variant0").unwrap() +//! ); +//! } +//! ``` +//! +//! You can also rename the generated enum using the `#[strum_discriminants_name(OtherName)]` +//! attribute: +//! +//! ```rust +//! # extern crate strum; +//! # #[macro_use] extern crate strum_macros; +//! // You need to bring the type into scope to use it!!! +//! use strum::IntoEnumIterator; +//! +//! #[allow(dead_code)] +//! #[derive(Debug, EnumDiscriminants)] +//! #[strum_discriminants_derive(EnumIter)] +//! #[strum_discriminants_name(MyVariants)] +//! enum MyEnum { +//! Variant0(bool), +//! Variant1 { a: bool }, +//! } +//! +//! fn main() { +//! assert_eq!( +//! vec![MyVariants::Variant0, MyVariants::Variant1], +//! MyVariants::iter().collect::>() +//! ); +//! } +//! ``` //! //! # Additional Attributes //!