Skip to content

Commit

Permalink
fixup! Add TryFrom to convert repr to enum
Browse files Browse the repository at this point in the history
msrv compatible doctest
  • Loading branch information
ModProg committed Sep 1, 2023
1 parent 0bc0466 commit bcbfbe6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ rustc_version = { version = "0.4", optional = true }
[dev-dependencies]
derive_more = { path = "..", features = ["full"] }
itertools = "0.11.0"
rustversion = "1.0"

[badges]
github = { repository = "JelteF/derive_more", workflow = "CI" }
Expand Down
11 changes: 10 additions & 1 deletion impl/doc/try_from.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ Only field-less variants can be constructed from their variant, therefor the `Tr
## Example usage

```rust
# #[rustversion::since(1.66)]
# mod discriminant_on_non_unit_enum {
# use derive_more::TryFrom;
#
#[derive(TryFrom, Debug, PartialEq)]
#[repr(u32)]
enum Enum {
Expand All @@ -18,10 +19,18 @@ enum Enum {
Empty{},
}

# #[rustversion::since(1.66)]
# pub fn test(){
assert_eq!(Enum::Implicit, Enum::try_from(0).unwrap());
assert_eq!(Enum::Explicit, Enum::try_from(5).unwrap());
assert_eq!(Enum::Empty{}, Enum::try_from(7).unwrap());

// variants with fields are not supported
assert!(Enum::try_from(6).is_err());
# }
# }
# // We need to use a `function` declaration, because we cannot put `rustversion` on a statement.
# #[rustversion::since(1.66)] use discriminant_on_non_unit_enum::test;
# #[rustversion::before(1.66)] fn test() {}
# test();
```

0 comments on commit bcbfbe6

Please sign in to comment.