summaryrefslogtreecommitdiffstats
path: root/third_party/rust/derive_more-impl/doc/is_variant.md
blob: 9e549dc1bdb291d67c988859ff12b7c27111dd4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# What `#[derive(IsVariant)]` generates

When an enum is decorated with `#[derive(IsVariant)]`, for each variant `foo` in
the enum, a public instance method `is_foo(&self) -> bool` is generated. If you
don't want the `is_foo` method generated for a variant you can put the
`#[is_variant(ignore)]` attribute on that variant.




## Example usage

```rust
# use derive_more::IsVariant;
#
#[derive(IsVariant)]
enum Maybe<T> {
    Just(T),
    Nothing
}

assert!(Maybe::<()>::Nothing.is_nothing());
assert!(!Maybe::<()>::Nothing.is_just());
```


### What is generated?

The derive in the above example code generates the following code:
```rust
# enum Maybe<T> {
#     Just(T),
#     Nothing
# }
impl <T> Maybe<T>{
    pub const fn is_just(&self) -> bool {
        match self {Self::Just(..) => true, _ => false}
    }
    pub const fn is_nothing(&self) -> bool {
        match self {Self::Nothing => true, _ => false}
    }
}
```