| 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 generates code like this: |
| 30 | ```rust |
| 31 | # enum Maybe<T> { |
| 32 | # Just(T), |
| 33 | # Nothing |
| 34 | # } |
| 35 | impl<T> Maybe<T>{ |
| 36 | #[must_use] |
| 37 | pub const fn is_just(&self) -> bool { |
| 38 | matches!(self, Self::Just(..)) |
| 39 | } |
| 40 | #[must_use] |
| 41 | pub const fn is_nothing(&self) -> bool { |
| 42 | matches!(self, Self::Nothing) |
| 43 | } |
| 44 | } |
| 45 | ``` |