【unitrix】 1.9 Z0与其它类型的算术运算(arith_ops.rs)

一、源码

这段代码是用Rust语言实现的零值(Z0)与其他类型的算术运算。Z0代表数字0,代码中为它实现了加法、减法、乘法、除法和取余运算。

use core::ops::{Add, Sub, Mul, Div, Rem, Neg};
use crate::number::{Z0, Integer, NonZero, Var, Primitive};

// ========== Z0 算术运算实现 / Z0 Arithmetic Implementations ==========

// ==================== Z0 + All ====================
// Z0 + I
impl<I: Integer> Add<I> for Z0 {
    type Output = I;
    #[inline(always)]
    fn add(self, rhs: I) -> Self::Output {
        rhs
    }
}

// Z0 + Var
impl<T: Primitive> Add<Var<T>> for Z0 {
    type Output = Var<T>;
    #[inline(always)]
    fn add(self, rhs: Var<T>) -> Self::Output {
        rhs
    }
}

// ==================== Z0 - All ====================
// Z0 - I = -I
impl<I: Integer + Neg> Sub<I> for Z0 {
    type Output = I::Output;
    #[inline(always)]
    fn sub(self, i: I) -> Self::Output {
       -i
    }
}

// Z0 - Var
impl<T: Primitive + Neg> Sub<Var<T>> for Z0 {
    type Output = Var<T>;
    #[inline(always)]
    fn sub(self, rhs: Var<T>) -> Self::Output {
        Var(-rhs.0)
    }
}

// ==================== Z0 * All ====================
// Z0 * I = Z0
impl<I: Integer> Mul<I> for Z0 {
    type Output = Z0;
    #[inline(always)]
    fn mul(self, _rhs: I) -> Self::Output {
        Z0
    }
}

// Z0 * Var = Z0
impl<T: Primitive> Mul<Var<T>> for Z0 {
    type Output = Z0;
    #[inline(always)]
    fn mul(self, _rhs: Var<T>) -> Self::Output {
        Z0
    }
}

// ==================== Z0 / All ====================
// Division of zero by any non-zero type
// 0 除以任何非零类型

// 0 / 0 is illegal and not implemented
// 0 / 0 非法,未实现

// Z0 / NonZero = Z0
impl<I: NonZero> Div<I> for Z0 {
    type Output = Z0;
    #[inline(always)]
    fn div(self, _rhs: I) -> Self::Output {
        Z0
    }
}

// Z0 / Var
impl<T: Primitive + PartialEq> Div<Var<T>> for Z0 {
    type Output = Z0;
    fn div(self, rhs: Var<T>) -> Self::Output {
        assert!(rhs.0 != T::from(0), "division by zero");
        Z0
    }
}

// ==================== Z0 % All ====================
// Remainder of zero by any non-zero type
// 0 取余任何非零类型

// 0 % 0 is illegal and not implemented
// 0 % 0 非法,未实现

// Z0 % NonZero = Z0
impl<I: NonZero> Rem<I> for Z0 {
    type Output = Z0;
    #[inline(always)]
    fn rem(self, _rhs: I) -> Self::Output {
        Z0
    }
}

// Z0 / Var
impl<T: Primitive + PartialEq> Rem<Var<T>> for Z0 {
    type Output = Z0;
    fn rem(self, rhs: Var<T>) -> Self::Output {
        assert!(rhs.0 != T::from(0), "division by zero");
        Z0
    }
}

二、源码分析

  1. 加法运算 (Add trait)
  • Z0 + I = I(I是任意整数类型):任何数加0等于它本身。

  • Z0 + Var = Var(Var是变量类型):0加变量等于变量本身。

  1. 减法运算 (Sub trait)
  • Z0 - I = -I:0减任何数等于该数的相反数(需要I实现Neg trait)。

  • Z0 - Var = Var<-T>:0减变量等于变量的相反数(需要T实现Neg trait)。

  1. 乘法运算 (Mul trait)
  • Z0 * I = Z0:0乘以任何数等于0。

  • Z0 * Var = Z0:0乘以变量等于0。

  1. 除法运算 (Div trait)
  • Z0 / NonZero = Z0:0除以任何非零数等于0(NonZero是非零类型的约束)。

  • Z0 / Var:0除以变量时,先检查变量是否为0(通过assert!宏),如果是则触发panic(运行时错误),否则返回0。

  1. 取余运算 (Rem trait)
  • Z0 % NonZero = Z0:0对任何非零数取余等于0。

  • Z0 % Var:0对变量取余时,先检查变量是否为0,如果是则触发panic,否则返回0。

三、关键点:

  1. 零除处理:除法和取余运算中,除数不能为0,否则会触发panic。

  2. 泛型约束:通过Integer、NonZero、Primitive等trait约束确保类型安全。

  3. 性能优化:使用#[inline(always)]提示编译器内联这些简单操作,减少函数调用开销。

四、用途:

这段代码可以用于数学库或类型系统,其中Z0代表编译期已知的零值,通过类型系统保证算术运算的正确性。

你可能感兴趣的:(【unitrix】 1.9 Z0与其它类型的算术运算(arith_ops.rs))