mirror of
https://github.com/chatmail/core.git
synced 2026-04-20 15:06:30 +03:00
feat: ensure_and_debug_assert{,_eq,_ne} macros combining debug_assert* and anyhow::ensure (#6907)
We have some debug assertions already, but we also want the corresponding errors in the release configuration so that it's not less reliable than non-optimized one. This doesn't change any function signatures, only debug assertions in functions returning `Result` are replaced. Co-authored-by: l <link2xt@testrun.org>
This commit is contained in:
38
src/tools.rs
38
src/tools.rs
@@ -763,5 +763,43 @@ pub(crate) fn inc_and_check<T: PrimInt + AddAssign + std::fmt::Debug>(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns early with an error if a condition is not satisfied.
|
||||
/// In non-optimized builds, panics instead if so.
|
||||
#[macro_export]
|
||||
macro_rules! ensure_and_debug_assert {
|
||||
($($arg:tt)*) => {
|
||||
debug_assert!($($arg)*);
|
||||
anyhow::ensure!($($arg)*);
|
||||
};
|
||||
}
|
||||
|
||||
/// Returns early with an error on two expressions inequality.
|
||||
/// In non-optimized builds, panics instead if so.
|
||||
#[macro_export]
|
||||
macro_rules! ensure_and_debug_assert_eq {
|
||||
($left:expr, $right:expr, $($arg:tt)*) => {
|
||||
match (&$left, &$right) {
|
||||
(left_val, right_val) => {
|
||||
debug_assert_eq!(left_val, right_val, $($arg)*);
|
||||
anyhow::ensure!(left_val == right_val, $($arg)*);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Returns early with an error on two expressions equality.
|
||||
/// In non-optimized builds, panics instead if so.
|
||||
#[macro_export]
|
||||
macro_rules! ensure_and_debug_assert_ne {
|
||||
($left:expr, $right:expr, $($arg:tt)*) => {
|
||||
match (&$left, &$right) {
|
||||
(left_val, right_val) => {
|
||||
debug_assert_ne!(left_val, right_val, $($arg)*);
|
||||
anyhow::ensure!(left_val != right_val, $($arg)*);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tools_tests;
|
||||
|
||||
Reference in New Issue
Block a user