add math utils; integrate checked expression macros

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-07 03:36:50 +00:00
parent 611f09829e
commit 5e72d36800
7 changed files with 68 additions and 0 deletions

26
src/core/utils/math.rs Normal file
View file

@ -0,0 +1,26 @@
use std::{cmp, time::Duration};
pub use checked_ops::checked_ops;
/// Checked arithmetic expression. Returns a Result<R, Error::Arithmetic>
#[macro_export]
macro_rules! checked {
($($input:tt)*) => {
$crate::utils::math::checked_ops!($($input)*)
.ok_or_else(|| $crate::Error::Arithmetic("operation overflowed or result invalid"))
}
}
/// in release-mode. Use for performance when the expression is obviously safe.
/// The check remains in debug-mode for regression analysis.
#[cfg(not(debug_assertions))]
#[macro_export]
macro_rules! validated {
($($input:tt)*) => { Ok($($input)*) }
}
#[cfg(debug_assertions)]
#[macro_export]
macro_rules! validated {
($($input:tt)*) => { $crate::checked!($($input)*) }
}

View file

@ -5,6 +5,7 @@ pub mod defer;
pub mod hash;
pub mod html;
pub mod json;
pub mod math;
pub mod mutex_map;
pub mod rand;
pub mod string;

View file

@ -62,3 +62,22 @@ fn common_prefix_none() {
let output = string::common_prefix(&input);
assert_eq!(output, "");
}
#[test]
fn checked_add() {
use utils::math::checked;
let a = 1234;
let res = checked!(a + 1).unwrap();
assert_eq!(res, 1235);
}
#[test]
#[should_panic(expected = "overflow")]
fn checked_add_overflow() {
use utils::math::checked;
let a: u64 = u64::MAX;
let res = checked!(a + 1).expect("overflow");
assert_eq!(res, 0);
}