2025-01-01 20:20:41 +00:00
|
|
|
mod expected;
|
2025-01-16 12:04:31 +00:00
|
|
|
mod tried;
|
2025-01-01 20:20:41 +00:00
|
|
|
|
2025-01-01 20:09:41 +00:00
|
|
|
use std::{cmp, convert::TryFrom};
|
2024-07-07 03:36:50 +00:00
|
|
|
|
|
|
|
|
pub use checked_ops::checked_ops;
|
|
|
|
|
|
2025-01-16 12:04:31 +00:00
|
|
|
pub use self::{expected::Expected, tried::Tried};
|
2025-02-23 01:17:45 -05:00
|
|
|
use crate::{Err, Error, Result, debug::type_name, err};
|
2024-07-07 06:17:58 +00:00
|
|
|
|
2024-07-07 03:36:50 +00:00
|
|
|
/// Checked arithmetic expression. Returns a Result<R, Error::Arithmetic>
|
|
|
|
|
#[macro_export]
|
2025-04-04 23:04:13 +00:00
|
|
|
#[collapse_debuginfo(yes)]
|
2024-07-07 03:36:50 +00:00
|
|
|
macro_rules! checked {
|
2024-09-07 22:04:28 +00:00
|
|
|
($($input:tt)+) => {
|
|
|
|
|
$crate::utils::math::checked_ops!($($input)+)
|
2024-07-15 04:19:43 +00:00
|
|
|
.ok_or_else(|| $crate::err!(Arithmetic("operation overflowed or result invalid")))
|
2024-09-07 22:04:28 +00:00
|
|
|
};
|
2024-07-07 03:36:50 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-07 22:04:28 +00:00
|
|
|
/// Checked arithmetic expression which panics on failure. This is for
|
|
|
|
|
/// expressions which do not meet the threshold for validated! but the caller
|
|
|
|
|
/// has no realistic expectation for error and no interest in cluttering the
|
|
|
|
|
/// callsite with result handling from checked!.
|
|
|
|
|
#[macro_export]
|
2025-04-04 23:04:13 +00:00
|
|
|
#[collapse_debuginfo(yes)]
|
2024-09-07 22:04:28 +00:00
|
|
|
macro_rules! expected {
|
|
|
|
|
($msg:literal, $($input:tt)+) => {
|
|
|
|
|
$crate::checked!($($input)+).expect($msg)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
($($input:tt)+) => {
|
|
|
|
|
$crate::expected!("arithmetic expression expectation failure", $($input)+)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Unchecked arithmetic expression in release-mode. Use for performance when
|
|
|
|
|
/// the expression is obviously safe. The check remains in debug-mode for
|
|
|
|
|
/// regression analysis.
|
2024-07-07 03:36:50 +00:00
|
|
|
#[cfg(not(debug_assertions))]
|
|
|
|
|
#[macro_export]
|
2025-04-04 23:04:13 +00:00
|
|
|
#[collapse_debuginfo(yes)]
|
2024-07-07 03:36:50 +00:00
|
|
|
macro_rules! validated {
|
2024-09-07 22:04:28 +00:00
|
|
|
($($input:tt)+) => {
|
2024-07-07 04:46:16 +00:00
|
|
|
//#[allow(clippy::arithmetic_side_effects)] {
|
|
|
|
|
//Some($($input)*)
|
2024-07-15 04:19:43 +00:00
|
|
|
// .ok_or_else(|| $crate::err!(Arithmetic("this error should never been seen")))
|
2024-07-07 04:46:16 +00:00
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
//NOTE: remove me when stmt_expr_attributes is stable
|
2024-09-07 22:04:28 +00:00
|
|
|
$crate::expected!("validated arithmetic expression failed", $($input)+)
|
|
|
|
|
};
|
2024-07-07 03:36:50 +00:00
|
|
|
}
|
|
|
|
|
|
2024-09-07 22:04:28 +00:00
|
|
|
/// Checked arithmetic expression in debug-mode. Use for performance when
|
|
|
|
|
/// the expression is obviously safe. The check is elided in release-mode.
|
2024-07-07 03:36:50 +00:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
|
#[macro_export]
|
2025-04-04 23:04:13 +00:00
|
|
|
#[collapse_debuginfo(yes)]
|
2024-07-07 03:36:50 +00:00
|
|
|
macro_rules! validated {
|
2024-09-07 22:04:28 +00:00
|
|
|
($($input:tt)+) => { $crate::expected!($($input)+) }
|
2024-07-07 03:36:50 +00:00
|
|
|
}
|
2024-07-07 03:39:35 +00:00
|
|
|
|
2024-07-07 06:17:58 +00:00
|
|
|
#[inline]
|
2026-11-21 01:58:19 +00:00
|
|
|
#[allow(clippy::as_conversions, clippy::cast_possible_truncation, clippy::cast_sign_loss)]
|
2024-07-07 06:17:58 +00:00
|
|
|
pub fn usize_from_f64(val: f64) -> Result<usize, Error> {
|
2026-11-21 01:58:19 +00:00
|
|
|
// Exclusive upper bound for values whose truncation fits in `usize`.
|
|
|
|
|
// Note that `usize::MAX as f64` rounds up to 2^64 on 64-bit targets, so a
|
|
|
|
|
// strict `<` check against 2^(usize::BITS) rejects all out-of-range values
|
|
|
|
|
// on both 32-bit and 64-bit targets.
|
|
|
|
|
#[allow(clippy::cast_precision_loss)]
|
|
|
|
|
const MAX_USIZE_EXCLUSIVE: f64 = (1_u128 << usize::BITS) as f64;
|
|
|
|
|
|
|
|
|
|
if !val.is_finite() {
|
|
|
|
|
return Err!(Arithmetic("Converting non-finite float to unsigned integer"));
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-07 06:17:58 +00:00
|
|
|
if val < 0.0 {
|
2024-07-15 04:19:43 +00:00
|
|
|
return Err!(Arithmetic("Converting negative float to unsigned integer"));
|
2024-07-07 06:17:58 +00:00
|
|
|
}
|
|
|
|
|
|
2026-11-21 01:58:19 +00:00
|
|
|
if val >= MAX_USIZE_EXCLUSIVE {
|
|
|
|
|
return Err!(Arithmetic("Converting out-of-range float to unsigned integer"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(val as usize)
|
2024-07-07 06:17:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
#[must_use]
|
|
|
|
|
pub fn usize_from_ruma(val: ruma::UInt) -> usize {
|
|
|
|
|
usize::try_from(val).expect("failed conversion from ruma::UInt to usize")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
#[must_use]
|
|
|
|
|
pub fn ruma_from_u64(val: u64) -> ruma::UInt {
|
|
|
|
|
ruma::UInt::try_from(val).expect("failed conversion from u64 to ruma::UInt")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
#[must_use]
|
|
|
|
|
pub fn ruma_from_usize(val: usize) -> ruma::UInt {
|
|
|
|
|
ruma::UInt::try_from(val).expect("failed conversion from usize to ruma::UInt")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
#[must_use]
|
2024-07-07 07:14:09 +00:00
|
|
|
#[allow(clippy::as_conversions, clippy::cast_possible_truncation)]
|
2024-07-07 06:17:58 +00:00
|
|
|
pub fn usize_from_u64_truncated(val: u64) -> usize { val as usize }
|
2024-08-04 06:12:45 +00:00
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn try_into<Dst: TryFrom<Src>, Src>(src: Src) -> Result<Dst> {
|
|
|
|
|
Dst::try_from(src).map_err(try_into_err::<Dst, Src>)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn try_into_err<Dst: TryFrom<Src>, Src>(e: <Dst as TryFrom<Src>>::Error) -> Error {
|
|
|
|
|
drop(e);
|
|
|
|
|
err!(Arithmetic(
|
|
|
|
|
"failed to convert from {} to {}",
|
|
|
|
|
type_name::<Src>(),
|
|
|
|
|
type_name::<Dst>()
|
|
|
|
|
))
|
|
|
|
|
}
|
2024-09-13 18:24:33 +00:00
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn clamp<T: Ord>(val: T, min: T, max: T) -> T { cmp::min(cmp::max(val, min), max) }
|
2026-11-21 01:58:19 +00:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn usize_from_f64_valid() {
|
|
|
|
|
assert_eq!(usize_from_f64(0.0).unwrap(), 0);
|
|
|
|
|
assert_eq!(usize_from_f64(-0.0).unwrap(), 0);
|
|
|
|
|
assert_eq!(usize_from_f64(42.9).unwrap(), 42);
|
|
|
|
|
assert_eq!(usize_from_f64(1_048_576.0).unwrap(), 1_048_576);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn usize_from_f64_rejects_invalid() {
|
|
|
|
|
usize_from_f64(-1.0).unwrap_err();
|
|
|
|
|
usize_from_f64(-0.5).unwrap_err();
|
|
|
|
|
usize_from_f64(f64::NAN).unwrap_err();
|
|
|
|
|
usize_from_f64(f64::INFINITY).unwrap_err();
|
|
|
|
|
usize_from_f64(f64::NEG_INFINITY).unwrap_err();
|
|
|
|
|
usize_from_f64(1e30).unwrap_err();
|
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
|
|
|
|
{
|
|
|
|
|
// `usize::MAX as f64` rounds up to 2^64, which is out of range.
|
|
|
|
|
#[allow(clippy::as_conversions, clippy::cast_precision_loss)]
|
|
|
|
|
let rounded_max = usize::MAX as f64;
|
|
|
|
|
usize_from_f64(rounded_max).unwrap_err();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|