Debian Codegraph @

third_party/rust/derive_more-impl/doc/is_variant.md

raw
1 # What `#[derive(IsVariant)]` generates
2
3 When an enum is decorated with `#[derive(IsVariant)]`, for each variant `foo` in
4 the enum, a public instance method `is_foo(&self) -> bool` is generated. If you
5 don't want the `is_foo` method generated for a variant you can put the
6 `#[is_variant(ignore)]` attribute on that variant.
7
8
9
10
11 ## Example usage
12
13 ```rust
14 # use derive_more::IsVariant;
15 #
16 #[derive(IsVariant)]
17 enum Maybe<T> {
18 Just(T),
19 Nothing
20 }
21
22 assert!(Maybe::<()>::Nothing.is_nothing());
23 assert!(!Maybe::<()>::Nothing.is_just());
24 ```
25
26
27 ### What is generated?
28
29 The derive in the above example code generates the following code:
30 ```rust
31 # enum Maybe<T> {
32 # Just(T),
33 # Nothing
34 # }
35 impl <T> Maybe<T>{
36 pub const fn is_just(&self) -> bool {
37 match self {Self::Just(..) => true, _ => false}
38 }
39 pub const fn is_nothing(&self) -> bool {
40 match self {Self::Nothing => true, _ => false}
41 }
42 }
43 ```