Skip to main content

claim/
assert_le.rs

1/// Asserts that first expression is less or equal 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_le`] 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_le!(1, 2);
21///
22/// // With custom messages
23/// assert_le!(5, 5, "Expecting that {} is less or equal than {}", 5, 5);
24/// # }
25/// ```
26///
27/// ```rust,should_panic
28/// # #[macro_use] extern crate claim;
29/// # fn main() {
30/// assert_le!(6, 5);  // Will panic
31///
32/// // With custom messages
33/// assert_le!(6, 5, "Not expecting {} to be less or equal than {}", 6, 5);
34/// # }
35/// ```
36///
37/// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html
38/// [`debug_assert_le!`]: ./macro.debug_assert_le.html
39#[macro_export]
40macro_rules! assert_le {
41    ($left:expr, $right:expr) => {
42        match (&$left, &$right) {
43            (left_val, right_val) => {
44                if !(*left_val <= *right_val) {
45                    // The reborrows below are intentional. Without them, the stack slot for the
46                    // borrow is initialized even before the values are compared, leading to a
47                    // noticeable slow down.
48                    panic!(r#"assertion failed: `(left <= right)`
49    left: `{:?}`,
50    right: `{:?}`"#, &*left_val, &*right_val)
51                }
52            }
53        }
54    };
55    ($left:expr, $right:expr,) => {
56        $crate::assert_le!($left, $right);
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 less or equal than the second in runtime.
75///
76/// Like [`assert_le!`], 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_le!`]: ./macro.assert_le.html
86#[macro_export]
87macro_rules! debug_assert_le {
88    ($($arg:tt)*) => (if cfg!(debug_assertions) { $crate::assert_le!($($arg)*); })
89}