SafeMul

Trait SafeMul 

Source
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 of self and rhs if no overflow occurred
  • Err(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 operations
  • SafeMathError - Error type returned on arithmetic failures

Required Methods§

Source

fn safe_mul(self, rhs: Self) -> Result<Self, SafeMathError>

Performs safe multiplication with overflow checking.

§Arguments
  • rhs - Right-hand side operand.
§Returns
  • Ok(result) - The product of self and rhs if no overflow occurred
  • Err(SafeMathError::Overflow) - If the multiplication would overflow

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.

Implementations on Foreign Types§

Source§

impl SafeMul for f32

Source§

fn safe_mul(self, rhs: Self) -> Result<Self, SafeMathError>

Performs safe safe_mul for f32.

Used internally by the #[safe_math] macro during expansion. Checks for finite results to prevent infinity/NaN propagation.

§Arguments
  • self - First operand.
  • rhs - Second operand.
§Returns

Ok(result) on success, Err(SafeMathError::InfiniteOrNaN) on error.

Source§

impl SafeMul for f64

Source§

fn safe_mul(self, rhs: Self) -> Result<Self, SafeMathError>

Performs safe safe_mul for f64.

Used internally by the #[safe_math] macro during expansion. Checks for finite results to prevent infinity/NaN propagation.

§Arguments
  • self - First operand.
  • rhs - Second operand.
§Returns

Ok(result) on success, Err(SafeMathError::InfiniteOrNaN) on error.

Implementors§

Source§

impl<T> SafeMul for T
where T: IsSafeMul + Mul<Output = T> + Copy,