From 58ce968fd21d2904849e97e9415b9edbcdfa0551 Mon Sep 17 00:00:00 2001 From: Azriel Hoh Date: Mon, 24 Sep 2018 14:08:10 +1200 Subject: [PATCH] Added docs about `From` impls. Issue #33 --- README.md | 25 ++++++++++++++++++++++++- strum/src/lib.rs | 25 ++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 530e860f..614b8101 100644 --- a/README.md +++ b/README.md @@ -299,7 +299,7 @@ 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: + non-`Default` fields. By default, the generated enum has the following derives: `Clone, Copy, Debug, PartialEq, Eq`. You can add additional derives using the `#[strum_discriminants(derive(AdditionalDerive))]` attribute. @@ -357,6 +357,29 @@ Strum has implemented the following macros: } ``` + The derived enum also has the following trait implementations: + + * `impl From for MyEnumDiscriminants` + * `impl<'_enum> From<&'_enum MyEnum> for MyEnumDiscriminants` + + These allow you to get the *Discriminants* enum variant from the original enum: + + ```rust + extern crate strum; + #[macro_use] extern crate strum_macros; + + #[derive(Debug, EnumDiscriminants)] + #[strum_discriminants(name(MyVariants))] + enum MyEnum { + Variant0(bool), + Variant1 { a: bool }, + } + + fn main() { + assert_eq!(MyVariants::Variant0, MyEnum::Variant0(true).into()); + } + ``` + # 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 2d59fa72..81bcaf66 100644 --- a/strum/src/lib.rs +++ b/strum/src/lib.rs @@ -293,7 +293,7 @@ //! 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: +//! non-`Default` fields. By default, the generated enum has the following derives: //! `Clone, Copy, Debug, PartialEq, Eq`. You can add additional derives using the //! `#[strum_discriminants(derive(AdditionalDerive))]` attribute. //! @@ -350,6 +350,29 @@ //! } //! ``` //! +//! The derived enum also has the following trait implementations: +//! +//! * `impl From for MyEnumDiscriminants` +//! * `impl<'_enum> From<&'_enum MyEnum> for MyEnumDiscriminants` +//! +//! These allow you to get the *Discriminants* enum variant from the original enum: +//! +//! ```rust +//! extern crate strum; +//! #[macro_use] extern crate strum_macros; +//! +//! #[derive(Debug, EnumDiscriminants)] +//! #[strum_discriminants(name(MyVariants))] +//! enum MyEnum { +//! Variant0(bool), +//! Variant1 { a: bool }, +//! } +//! +//! fn main() { +//! assert_eq!(MyVariants::Variant0, MyEnum::Variant0(true).into()); +//! } +//! ``` +//! //! # Additional Attributes //! //! Strum supports several custom attributes to modify the generated code. At the enum level, the