SafeRem

Trait SafeRem 

Source
pub trait SafeRem: Copy + Rem<Output = Self> {
    // Required method
    fn safe_rem(self, rhs: Self) -> Result<Self, SafeMathError>;
}
Expand description

Safe remainder operation with division-by-zero checking.

This trait provides checked remainder (modulo) operations that return a Result instead of panicking when attempting to compute remainder with a zero divisor. The operation follows the same rules as Rust’s % operator for the sign of the result.

§Arguments

  • rhs - Right-hand side operand (divisor).

§Returns

  • Ok(result) - The remainder of self divided by rhs if operation is valid
  • Err(SafeMathError::DivisionByZero) - If rhs is zero

§Examples

use safe_math::{SafeRem, SafeMathError};

let a: i8 = 10;
let b: i8 = 3;

// Safe remainder that works
assert_eq!(a.safe_rem(b), Ok(1));  // 10 % 3 = 1

// Safe remainder that detects division by zero
let zero: i8 = 0;
assert_eq!(a.safe_rem(zero), Err(SafeMathError::DivisionByZero));

// Example with negative numbers
let neg_a: i8 = -10;
assert_eq!(neg_a.safe_rem(b), Ok(-1));  // -10 % 3 = -1

§See also

  • SafeMathOps - Combined trait for all safe arithmetic operations
  • SafeMathError - Error type returned on arithmetic failures
  • SafeDiv - Safe division operations

Required Methods§

Source

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

Performs safe remainder with division-by-zero checking.

§Arguments
  • rhs - Right-hand side operand (divisor).
§Returns
  • Ok(result) - The remainder of self divided by rhs if operation is valid
  • Err(SafeMathError::DivisionByZero) - If rhs is zero

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 SafeRem for f32

Source§

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

Performs safe safe_rem 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 SafeRem for f64

Source§

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

Performs safe safe_rem 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> SafeRem for T
where T: IsSafeRem + Rem<Output = T> + Copy,