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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//! Type-level boolean and option types.
//!
//! A value of these traits can be used to conditionally enable or disable
//! particular functions in generic contexts. Compared to using [`bool`] or
//! [`Option`], there are no runtime checks, nor performance overhead, nor
//! potential panics due to unwrapping an [`Option`].
//!
//! # Example
//!
//! ```ignore
//! use common::typelevel::{TBool, True, False};
//!
//! trait MyTrait {
//! /// Whether the optional method `increment_value` can be called.
//! type CanIncrement: TBool;
//! /// Optional method. Because it accepts `CanIncrement` as an argument,
//! /// it can only be called if `CanIncrement` is `True`.
//! fn increment_value(&mut self, proof: Self::CanIncrement) -> u32;
//! }
//!
//! struct Unit;
//! impl MyTrait for Unit {
//! type CanIncrement = False;
//! fn increment_value(&mut self, proof: Self::CanIncrement) -> u32 {
//! // The value of type `False` is a proof to the compiler that this
//! // method cannot be called. It can be coerced to any type using an
//! // empty match.
//! match proof {} // coerced to `u32`
//! }
//! }
//!
//! struct Counter(u32);
//! impl MyTrait for Counter {
//! type CanIncrement = True;
//! fn increment_value(&mut self, _proof: Self::CanIncrement) -> u32 {
//! self.0 += 1;
//! self.0
//! }
//! }
//!
//! /// This function calls `increment_value` twice. It requires a proof that
//! /// `increment_value` can be called, and passes it to the method. Even if
//! /// `CanIncrement` is `False`, this function still type checks, but can
//! /// not be called. Note that there are no runtime checks in this method.
//! fn increment_twice<T: MyTrait>(proof: T::CanIncrement, x: &mut T) -> u32 {
//! x.increment_value(proof);
//! x.increment_value(proof)
//! }
//!
//! fn run<T: MyTrait>(x: &mut T) {
//! // The following `if` is the only runtime check in this example.
//! if let Some(enabled) = T::CanIncrement::VALUE {
//! let v = increment_twice(enabled, x);
//! println!("After incrementing twice: {v}");
//! }
//! }
//!
//! fn main() {
//! run(&mut Unit);
//! run(&mut Counter(100));
//! }
//! ```
use PhantomData;
use TransparentWrapper;
/// A boolean value lifted to the type level.
/// An [`Option`]-like wrapper lifted to the type level.
/// A [`TBool`] implementation of `true`.
///
/// This type is a zero-sized unit type.
;
/// A [`TBool`] implementation of `false`.
///
/// This type cannot be instantiated, similar to the [never] type.
///
/// [never]: https://doc.rust-lang.org/std/primitive.never.html
/// A [`TOption`] type that corresponds to [`True`].
///
/// Similar to [`Option::Some`] but lifted to the type level.
/// This type is a transparent wrapper around a value of type `T`.
;
/// A [`TOption`] type that corresponds to [`False`].
///
/// Similar to [`Option::None`] but lifted to the type level.
/// This type cannot be instantiated (similar to the [never] type).
///
/// [never]: https://doc.rust-lang.org/std/primitive.never.html
;