pub trait SafeMul: Copy + Mul<Output = Self> {
// Required method
fn safe_mul(self, rhs: Self) -> Result<Self, SafeMathError>;
}Expand description
Safe multiplication operation with overflow checking.
This trait provides checked multiplication that returns a Result instead of panicking
or wrapping on overflow.
§Arguments
rhs- Right-hand side operand.
§Returns
Ok(result)- The product ofselfandrhsif no overflow occurredErr(SafeMathError::Overflow)- If the multiplication would overflow
§Examples
use safe_math::{SafeMul, SafeMathError};
let a: u8 = 10;
let b: u8 = 5;
// Safe multiplication that works
assert_eq!(a.safe_mul(b), Ok(50));
// Safe multiplication that detects overflow
let c: u8 = 100;
assert_eq!(a.safe_mul(c), Err(SafeMathError::Overflow));§See also
SafeMathOps- Combined trait for all safe arithmetic operationsSafeMathError- Error type returned on arithmetic failures
Required Methods§
Sourcefn safe_mul(self, rhs: Self) -> Result<Self, SafeMathError>
fn safe_mul(self, rhs: Self) -> Result<Self, SafeMathError>
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.