Skip to content

Commit

Permalink
Added docs about From impls.
Browse files Browse the repository at this point in the history
  • Loading branch information
azriel91 committed Sep 24, 2018
1 parent 91b9018 commit 58ce968
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
25 changes: 24 additions & 1 deletion README.md
Expand Up @@ -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.

Expand Down Expand Up @@ -357,6 +357,29 @@ Strum has implemented the following macros:
}
```

The derived enum also has the following trait implementations:

* `impl From<MyEnum> 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
Expand Down
25 changes: 24 additions & 1 deletion strum/src/lib.rs
Expand Up @@ -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.
//!
Expand Down Expand Up @@ -350,6 +350,29 @@
//! }
//! ```
//!
//! The derived enum also has the following trait implementations:
//!
//! * `impl From<MyEnum> 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
Expand Down

0 comments on commit 58ce968

Please sign in to comment.