use crate::error::{Error, Result};
pub(crate) use crate::limits::{
checked_usize, ensure_u64_ceiling, ensure_u64_limit, ensure_usize_limit,
};
pub(crate) const fn widen_u32(value: u32) -> u64 {
value as u64
}
pub(crate) fn add_u64(left: u64, right: u64, context: &'static str) -> Result<u64> {
left.checked_add(right).ok_or(Error::InvalidFormat(context))
}
pub(crate) fn mul_u64(left: u64, right: u64, context: &'static str) -> Result<u64> {
left.checked_mul(right).ok_or(Error::InvalidFormat(context))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn widening_preserves_every_u32_boundary() {
assert_eq!(widen_u32(0), 0);
assert_eq!(widen_u32(u32::MAX), 4_294_967_295);
}
#[test]
fn rejects_overflowing_section_arithmetic() {
assert!(add_u64(u64::MAX, 1, "test overflow").is_err());
assert!(mul_u64(u64::MAX, 2, "test overflow").is_err());
assert_eq!(add_u64(2, 3, "test").unwrap(), 5);
assert_eq!(mul_u64(2, 3, "test").unwrap(), 6);
}
}