summaryrefslogtreecommitdiffstats
path: root/third_party/rust/derive_more/tests/is_variant.rs
blob: e778ca47af1e82e764863bd5bdb22c3b7672afe5 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(dead_code)]

use derive_more::IsVariant;

#[derive(IsVariant)]
enum Either<TLeft, TRight> {
    Left(TLeft),
    Right(TRight),
}

const _: () = {
    let either: Either<u8, i16> = Either::Right(7);
    assert!(either.is_right());
    assert!(!either.is_left());

    let either: Either<u8, i16> = Either::Left(7);
    assert!(!either.is_right());
    assert!(either.is_left());
};

#[derive(IsVariant)]
enum Maybe<T> {
    Nothing,
    Just(T),
}

const _: () = {
    let maybe: Maybe<u8> = Maybe::Just(7);
    assert!(maybe.is_just());
    assert!(!maybe.is_nothing());

    let maybe: Maybe<u8> = Maybe::Nothing;
    assert!(!maybe.is_just());
    assert!(maybe.is_nothing());
};

#[test]
pub fn test_is_variant() {
    assert!(Maybe::<()>::Nothing.is_nothing());
    assert!(!Maybe::<()>::Nothing.is_just());
}

#[derive(IsVariant)]
enum Color {
    RGB(u8, u8, u8),
    CMYK { c: u8, m: u8, y: u8, k: u8 },
}

const _: () = {
    let color = Color::RGB(0, 0, 0);
    assert!(color.is_rgb());
    assert!(!color.is_cmyk());

    let color = Color::CMYK {
        c: 0,
        m: 0,
        y: 0,
        k: 0,
    };
    assert!(!color.is_rgb());
    assert!(color.is_cmyk());
};

#[derive(IsVariant)]
enum Nonsense<'a, T> {
    Ref(&'a T),
    NoRef,
    #[is_variant(ignore)]
    NoRefIgnored,
}

const _: () = {
    let nonsense: Nonsense<u8> = Nonsense::Ref(&7);
    assert!(nonsense.is_ref());
    assert!(!nonsense.is_no_ref());

    let nonsense: Nonsense<u8> = Nonsense::NoRef;
    assert!(!nonsense.is_ref());
    assert!(nonsense.is_no_ref());
};

#[derive(IsVariant)]
enum WithConstraints<T>
where
    T: Copy,
{
    One(T),
    Two,
}

const _: () = {
    let wc: WithConstraints<u8> = WithConstraints::One(1);
    assert!(wc.is_one());
    assert!(!wc.is_two());

    let wc: WithConstraints<u8> = WithConstraints::Two;
    assert!(!wc.is_one());
    assert!(wc.is_two());
};

#[derive(IsVariant)]
enum KitchenSink<'a, 'b, T1: Copy, T2: Clone>
where
    T2: Into<T1> + 'b,
{
    Left(&'a T1),
    Right(&'b T2),
    OwnBoth { left: T1, right: T2 },
    Empty,
    NeverMind(),
    NothingToSeeHere {},
}

const _: () = {
    let ks: KitchenSink<u16, u8> = KitchenSink::Left(&1);
    assert!(ks.is_left());
    assert!(!ks.is_right());
    assert!(!ks.is_own_both());
    assert!(!ks.is_empty());
    assert!(!ks.is_never_mind());
    assert!(!ks.is_nothing_to_see_here());

    let ks: KitchenSink<u16, u8> = KitchenSink::Right(&1);
    assert!(!ks.is_left());
    assert!(ks.is_right());
    assert!(!ks.is_own_both());
    assert!(!ks.is_empty());
    assert!(!ks.is_never_mind());
    assert!(!ks.is_nothing_to_see_here());

    let ks: KitchenSink<u16, u8> = KitchenSink::OwnBoth { left: 1, right: 2 };
    assert!(!ks.is_left());
    assert!(!ks.is_right());
    assert!(ks.is_own_both());
    assert!(!ks.is_empty());
    assert!(!ks.is_never_mind());
    assert!(!ks.is_nothing_to_see_here());

    let ks: KitchenSink<u16, u8> = KitchenSink::Empty;
    assert!(!ks.is_left());
    assert!(!ks.is_right());
    assert!(!ks.is_own_both());
    assert!(ks.is_empty());
    assert!(!ks.is_never_mind());
    assert!(!ks.is_nothing_to_see_here());

    let ks: KitchenSink<u16, u8> = KitchenSink::NeverMind();
    assert!(!ks.is_left());
    assert!(!ks.is_right());
    assert!(!ks.is_own_both());
    assert!(!ks.is_empty());
    assert!(ks.is_never_mind());
    assert!(!ks.is_nothing_to_see_here());

    let ks: KitchenSink<u16, u8> = KitchenSink::NothingToSeeHere {};
    assert!(!ks.is_left());
    assert!(!ks.is_right());
    assert!(!ks.is_own_both());
    assert!(!ks.is_empty());
    assert!(!ks.is_never_mind());
    assert!(ks.is_nothing_to_see_here());
};