claim/assert_gt.rs
1/// Asserts that first expression is greater than the second.
2///
3/// Requires that both expressions be comparable with `>`.
4///
5/// ## Uses
6///
7/// Assertions are always checked in both debug and release builds, and cannot be disabled.
8/// See [`debug_assert_gt`] for assertions that are not enabled in release builds by default.
9///
10/// ## Custom messages
11///
12/// This macro has a second form, where a custom panic message can be provided
13/// with or without arguments for formatting. See [`std::fmt`] for syntax for this form.
14///
15/// ## Examples
16///
17/// ```rust
18/// # #[macro_use] extern crate claim;
19/// # fn main() {
20/// assert_gt!(2, 1);
21///
22/// // With custom messages
23/// assert_gt!(2, 1, "Expecting that {} is greater or equal than {}", 2, 1);
24/// # }
25/// ```
26///
27/// ```rust,should_panic
28/// # #[macro_use] extern crate claim;
29/// # fn main() {
30/// assert_gt!(5, 5); // Will panic
31///
32/// // With custom messages
33/// assert_gt!(5, 6, "Not expecting {} to be greater than {}", 5, 6);
34/// # }
35/// ```
36///
37/// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html
38/// [`debug_assert_gt!`]: ./macro.debug_assert_gt.html
39#[macro_export]
40macro_rules! assert_gt {
41 ($left:expr, $right:expr,) => {
42 $crate::assert_ge!($left, $right);
43 };
44 ($left:expr, $right:expr) => {
45 match (&$left, &$right) {
46 (left_val, right_val) => {
47 if !(*left_val > *right_val) {
48 // The reborrows below are intentional. Without them, the stack slot for the
49 // borrow is initialized even before the values are compared, leading to a
50 // noticeable slow down.
51 panic!(r#"assertion failed: `(left > right)`
52 left: `{:?}`,
53 right: `{:?}`"#, &*left_val, &*right_val)
54 }
55 }
56 }
57 };
58 ($left:expr, $right:expr, $($arg:tt)+) => {
59 match (&$left, &$right) {
60 (left_val, right_val) => {
61 if !(*left_val > *right_val) {
62 // The reborrows below are intentional. Without them, the stack slot for the
63 // borrow is initialized even before the values are compared, leading to a
64 // noticeable slow down.
65 panic!(r#"assertion failed: `(left > right)`
66 left: `{:?}`,
67 right: `{:?}`: {}"#, &*left_val, &*right_val, format_args!($($arg)+))
68 }
69 }
70 }
71 };
72}
73
74/// Asserts that first expression is greater than the second in runtime.
75///
76/// Like [`assert_gt!`], this macro also has a second version,
77/// where a custom panic message can be provided.
78///
79/// ## Uses
80///
81/// See [`debug_assert!`] documentation for possible use cases.
82/// The same applies to this macro.
83///
84/// [`debug_assert!`]: https://doc.rust-lang.org/std/macro.debug_assert.html
85/// [`assert_gt!`]: ./macro.assert_gt.html
86#[macro_export]
87macro_rules! debug_assert_gt {
88 ($($arg:tt)*) => (if cfg!(debug_assertions) { $crate::assert_gt!($($arg)*); })
89}