-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
const_assert.rs
178 lines (171 loc) · 5.06 KB
/
const_assert.rs
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
/// Asserts that constant expressions evaluate to `true`.
///
/// Constant expressions can be ensured to have certain properties via this
/// macro If the expression evaluates to `false`, the file will fail to compile.
/// This is synonymous to [`static_assert` in C++][static_assert].
///
/// # Alternatives
///
/// There also exists [`const_assert_eq`](macro.const_assert_eq.html) for
/// validating whether a sequence of expressions are equal to one another.
///
/// # Examples
///
/// A common use case is to guarantee properties about a constant value that's
/// generated via meta-programming.
///
/// ```
/// # #[macro_use] extern crate static_assertions; fn main() {}
/// const VALUE: i32 = // ...
/// # 3;
///
/// const_assert!(VALUE >= 2);
/// ```
///
/// Inputs are type-checked as booleans:
///
/// ```compile_fail
/// # #[macro_use] extern crate static_assertions; fn main() {}
/// const_assert!(!0);
/// ```
///
/// Despite this being a macro, we see this produces a type error:
///
/// ```txt
/// | const_assert!(!0);
/// | ^^ expected bool, found integral variable
/// |
/// = note: expected type `bool`
/// found type `{integer}`
/// ```
///
/// The following fails to compile because multiplying by 5 does not have an
/// identity property:
///
/// ```compile_fail
/// # #[macro_use] extern crate static_assertions; fn main() {}
/// const_assert!(5 * 5 == 5);
/// ```
///
/// [static_assert]: http://en.cppreference.com/w/cpp/language/static_assert
#[macro_export(local_inner_macros)]
macro_rules! const_assert {
($x:expr $(,)?) => {
const _: $crate::True = _to_bool!($x);
};
}
/// Asserts that constants are equal in value.
///
/// Use [`const_assert_eq_usize!`](macro.const_assert_eq_usize.html) for better
/// error messages when asserting
/// [`usize`](https://doc.rust-lang.org/std/primitive.usize.html) equality.
///
/// # Examples
///
/// This works as a shorthand for `const_assert!(a == b)`:
///
/// ```
/// # #[macro_use] extern crate static_assertions; fn main() {}
/// const TWO: i32 = 2;
///
/// const_assert_eq!(TWO * TWO, TWO + TWO);
/// ```
///
/// Just because 2 × 2 = 2 + 2 doesn't mean it holds true for other numbers:
///
/// ```compile_fail
/// # #[macro_use] extern crate static_assertions; fn main() {}
/// const_assert_eq!(4 + 4, 4 * 4);
/// ```
#[macro_export(local_inner_macros)]
macro_rules! const_assert_eq {
($x:expr, $($y:expr),+ $(,)?) => {
const_assert!($($x == $y)&&+);
};
}
/// Asserts that constants of type
/// [`usize`](https://doc.rust-lang.org/std/primitive.usize.html) are equal in
/// value.
///
/// This is equivalent to [`const_assert_eq!`](macro.const_assert_eq.html) but
/// allows for inspecting the values in error messages.
#[macro_export]
macro_rules! const_assert_eq_usize {
($x:expr, $($y:expr),+ $(,)?) => {
// Assigned instance must match the annotated type or else it will fail.
$(const _: [(); $x] = [(); $y];)+
};
}
/// Asserts that constants are **not** equal in value.
///
/// # Examples
///
/// This works as a shorthand for `const_assert!(a != b)`:
///
/// ```
/// # #[macro_use] extern crate static_assertions; fn main() {}
/// const NUM: usize = 32;
///
/// const_assert_ne!(NUM * NUM, 64);
/// ```
///
/// The following example fails to compile because 2 is magic and 2 × 2 = 2 + 2:
///
/// ```compile_fail
/// # #[macro_use] extern crate static_assertions; fn main() {}
/// const_assert_ne!(2 + 2, 2 * 2);
/// ```
#[macro_export(local_inner_macros)]
macro_rules! const_assert_ne {
($x:expr, $($y:expr),+ $(,)?) => {
const_assert!($($x != $y)&&+);
};
}
/// Asserts that constants are less than each other.
#[macro_export(local_inner_macros)]
macro_rules! const_assert_lt {
($x:expr, $($y:expr),+ $(,)?) => {
const_assert_lt!(@build $x, $($y),+);
};
(@build $x:expr) => {};
(@build $x:expr, $($y:expr),+) => {
const_assert!($x < _head!($($y),+));
const_assert_lt!(@build $($y),+);
};
}
/// Asserts that constants are less than or equal to each other.
#[macro_export(local_inner_macros)]
macro_rules! const_assert_le {
($x:expr, $($y:expr),+ $(,)?) => {
const_assert_le!(@build $x, $($y),+);
};
(@build $x:expr) => {};
(@build $x:expr, $($y:expr),+) => {
const_assert!($x <= _head!($($y),+));
const_assert_le!(@build $($y),+);
};
}
/// Asserts that constants are greater than each other.
#[macro_export(local_inner_macros)]
macro_rules! const_assert_gt {
($x:expr, $($y:expr),+ $(,)?) => {
const_assert_gt!(@build $x, $($y),+);
};
(@build $x:expr) => {};
(@build $x:expr, $($y:expr),+) => {
const_assert!($x > _head!($($y),+));
const_assert_gt!(@build $($y),+);
};
}
/// Asserts that constants are less than or equal to each other.
#[macro_export(local_inner_macros)]
macro_rules! const_assert_ge {
($x:expr, $($y:expr),+ $(,)?) => {
const_assert_ge!(@build $x, $($y),+);
};
(@build $x:expr) => {};
(@build $x:expr, $($y:expr),+) => {
const_assert!($x >= _head!($($y),+));
const_assert_ge!(@build $($y),+);
};
}