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
#[macro_export]
macro_rules! assert_none {
($cond:expr $(,)?) => {
match $cond {
none @ None => none,
some @ Some(_) => {
$crate::panicking::assert_failed(
$crate::panicking::Value::Ref(&None),
$crate::panicking::Value::Ref(&some),
None,
);
}
}
};
($cond:expr, $($arg:tt)+) => {
match $cond {
none @ None => none,
value @ Some(_) => {
$crate::panicking::assert_failed(
$crate::panicking::Value::Ref(&None),
$crate::panicking::Value::Ref(&value),
Some(format_args!($($arg)+)),
);
}
}
};
}
#[macro_export]
macro_rules! debug_assert_none {
($($arg:tt)*) => {
if cfg!(debug_assertions) {
$crate::assert_none!($($arg)*);
}
}
}
#[cfg(test)]
mod tests {
#[test]
#[should_panic(expected = "assertion failed: expected `None`, got `Some(true)`")]
fn check_assert_none_panic() {
assert_none!(Some(true));
}
#[test]
#[should_panic(expected = "assertion failed: expected `None`, got `Some(true)`\ni'm confused")]
fn check_assert_none_panic_with_message() {
assert_none!(Some(true), "i'm confused");
}
#[test]
fn check_assert_none_pass() {
assert_none!(Option::<bool>::None);
}
}