学Rust写CAD】9 变量类

Variable 结构体,封装了一个泛型值 T,其中 T 必须满足 FloatType trait 的约束。FloatType trait 确保 T 实现了必要的算术操作(Mul、Add、Neg)以及 Copy trait。还为 Variable 实现了各种算术操作,并提供了单元测试来验证这些操作的正确性。

一、代码

/*
 * 变量结构体 Variable
 * 该结构体泛型参数 T 需满足 FloatType 约束
 */

 use std::ops::{Mul, Add, Neg};
 use super::constant::{Constant, ValidConstant};
 
 /// 定义 FloatType trait,约束 T 必须实现 Mul、Add、Neg 和 Copy
 pub trait FloatType: Mul<Output = Self> + Add<Output = Self> + Neg<Output = Self> + Copy + Sized {}
 impl FloatType for f32 {}  // 为 f32 实现 FloatType
 impl FloatType for f64 {}  // 为 f64 实现 FloatType
 
 /// 变量结构体,封装一个泛型值 T
 #[derive(Debug, Clone, Copy, PartialEq)]
 pub struct Variable<T: FloatType>(pub T);
 
 /// 实现 Variable 与 Constant 的乘法运算
 /// V * C,调用 C * V
 impl<const N: i32, T: FloatType> Mul<Constant<N>> for Variable<T>
 where
     Constant<N>: ValidConstant + Mul<Variable<T>>,  // 必须实现乘法和类型转换
 {
     type Output = <Constant<N> as Mul<Variable<T>>>::Output;
 
     fn mul(self, b: Constant<N>) -> Self::Output {
         b * self
     }
 }
 
 /// 实现 Variable 与 Variable 的乘法运算
 /// V * V
 impl<T: FloatType> Mul<Variable<T>> for Variable<T> {
     type Output = Self;
 
     fn mul(self, b: Self) -> Self::Output {
         Variable(self.0 * b.0)
     }
 }
 
 /// 实现 Variable 与 Variable 的加法运算
 /// V + V
 impl<T: FloatType> Add for Variable<T> {
     type Output = Self;
 
     fn add(self, b: Self) -> Self::Output {
         Variable(self.0 + b.0)
     }
 }
 
 /// 实现 Variable 与 Constant 的加法运算
 /// V + C,调用 C + V
 impl<const N: i32, T: FloatType> Add<Constant<N>> for Variable<T>
 where
     Constant<N>: ValidConstant + Add<Variable<T>>,  // 必须实现加法和类型转换
 {
     type Output = <Constant<N> as Add<Variable<T>>>::Output;
 
     fn add(self, b: Constant<N>) -> Self::Output {
         b + self
     }
 }
 
 /// 实现 Variable 的取反运算
 /// -V
 impl<T: FloatType> Neg for Variable<T> {
     type Output = Self;
 
     fn neg(self) -> Self::Output {
         Variable(-self.0)
     }
 }

 #[cfg(test)]
mod tests {
    use super::*;

    /// 测试 Variable 与 Variable 的乘法
    #[test]
    fn test_variable_mul_variable() {
        let v1 = Variable(3.0_f32);
        let v2 = Variable(2.0_f32);
        let result = v1 * v2;
        assert_eq!(result, Variable(6.0_f32));
    }

    /// 测试 Variable 与 Variable 的加法
    #[test]
    fn test_variable_add_variable() {
        let v1 = Variable(3.0_f32);
        let v2 = Variable(2.0_f32);
        let result = v1 + v2;
        assert_eq!(result, Variable(5.0_f32));
    }

    /// 测试 Variable 的取反运算
    #[test]
    fn test_variable_neg() {
        let v = Variable(3.0_f32);
        let result = -v;
        assert_eq!(result, Variable(-3.0_f32));
    }
}

二、介绍

  1. FloatType Trait
  • FloatType trait 用于约束泛型类型 T,要求 T 必须实现 Mul、Add、Neg 和 Copy。

  • 为 f32 和 f64 显式实现了 FloatType,允许它们作为 Variable 中的类型 T 使用。

  1. Variable 结构体
  • Variable 结构体是泛型的,T 必须实现 FloatType。

  • 它封装了一个类型为 T 的值。

  1. 算术操作实现
  • 乘法 (Mul):

    • 为 Variable * Constant 和 Variable * Variable 实现了乘法。

    • 与 Constant 的乘法操作委托给 Constant 的乘法实现。

  • 加法 (Add):

    • 为 Variable + Variable 和 Variable + Constant 实现了加法。

    • 与 Constant 的加法操作委托给 Constant 的加法实现。

  • 取反 (Neg):

    +为 -Variable 实现了取反操作,直接对封装的值取反。

  1. 单元测试
  • 乘法测试:验证两个 Variable 实例的乘法是否正确。

  • 加法测试:验证两个 Variable 实例的加法是否正确。

  • 取反测试:验证对 Variable 取反是否正确。

示例用法

以下是如何使用 Variable 结构体及其操作的示例:

fn main() {
    let v1 = Variable(3.0_f32);
    let v2 = Variable(2.0_f32);

    let product = v1 * v2;  // Variable(6.0_f32)
    let sum = v1 + v2;      // Variable(5.0_f32)
    let negated = -v1;      // Variable(-3.0_f32)

    println!("乘法结果: {:?}", product);
    println!("加法结果: {:?}", sum);
    println!("取反结果: {:?}", negated);
}

你可能感兴趣的:(学Rust写CAD,rust)