【PhysUnits】2 Scalar<T> 标量元组结构体(scalar/mod.rs)

Scalar<T> 标量元组结构体分析

泛型标量元组结构体 Scalar<T>包装了一个类型为 T 的值。我将解释这个结构体并根据不同的 T 类型讨论如何实现不同的方法和常量。

基本结构

rust 复制代码
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Scalar<T>(pub T);
  • Scalar<T> 是一个元组结构体,包含一个类型为 T 的单一字段
  • 使用了多个 derive 属性自动实现常见 trait:
    • Debug: 允许打印调试信息
    • Copy: 表示类型可以通过位复制
    • Clone: 提供 clone() 方法
    • PartialEq, Eq: 实现相等比较
    • PartialOrd, Ord: 实现排序比较

根据 T 类型的不同实现

要为不同的 T 类型实现不同的方法和常量,可以使用 Rust 的 trait 系统和条件实现。以下是三种特例的实现方式:

1. 有符号整数特例

rust 复制代码
impl<T> Scalar<T>
where
    T: SignedInt, // 假设有这个trait或者使用std::ops::Neg + 其他整数trait
{
    pub fn abs(&self) -> Self {
        Scalar(if self.0 < T::zero() { -self.0 } else { self.0 })
    }
    
    pub const MIN: Self = Scalar(T::MIN);
    pub const MAX: Self = Scalar(T::MAX);
}

2. 浮点数特例

rust 复制代码
impl<T> Scalar<T>
where
    T: Float, // 比如使用num_traits::Float或std::float特性
{
    pub fn sqrt(&self) -> Self {
        Scalar(self.0.sqrt())
    }
    
    pub fn is_nan(&self) -> bool {
        self.0.is_nan()
    }
    
    pub const EPSILON: Self = Scalar(T::EPSILON);
    pub const INFINITY: Self = Scalar(T::INFINITY);
}

3. 自定义定点数特例

rust 复制代码
impl<T> Scalar<T>
where
    T: FixedPoint, // 自定义trait
{
    pub fn to_f64(&self) -> f64 {
        self.0.to_f64()
    }
    
    pub fn from_f64(value: f64) -> Self {
        Scalar(T::from_f64(value))
    }
    
    pub const SCALE_FACTOR: Self = Scalar(T::SCALE_FACTOR);
}

完整的实现示例

rust 复制代码
use std::ops::Neg;

// 自定义trait定义
pub trait SignedInt: Neg<Output = Self> + PartialOrd + Copy {
    const MIN: Self;
    const MAX: Self;
    fn zero() -> Self;
}

pub trait FixedPoint {
    fn to_f64(&self) -> f64;
    fn from_f64(value: f64) -> Self;
    const SCALE_FACTOR: Self;
}

// 为内置类型实现SignedInt
impl SignedInt for i32 {
    const MIN: i32 = i32::MIN;
    const MAX: i32 = i32::MAX;
    fn zero() -> i32 { 0 }
}

// Scalar实现
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Scalar<T>(pub T);

// 有符号整数实现
impl<T> Scalar<T>
where
    T: SignedInt,
{
    pub fn abs(&self) -> Self {
        Scalar(if self.0 < T::zero() { -self.0 } else { self.0 })
    }
    
    pub const MIN: Self = Scalar(T::MIN);
    pub const MAX: Self = Scalar(T::MAX);
}

// 自定义定点数实现
impl<T> Scalar<T>
where
    T: FixedPoint,
{
    pub fn to_f64(&self) -> f64 {
        self.0.to_f64()
    }
    
    pub fn from_f64(value: f64) -> Self {
        Scalar(T::from_f64(value))
    }
    
    pub const SCALE_FACTOR: Self = Scalar(T::SCALE_FACTOR);
}

使用方式

根据不同的类型参数 TScalar<T> 会提供不同的方法和常量:

rust 复制代码
let int_scalar = Scalar(-5i32);
println!("Abs: {:?}", int_scalar.abs()); // 可用abs()方法
println!("Max: {:?}", Scalar::<i32>::MAX); // 可用MAX常量

// 对于浮点数类型,会有sqrt()等方法
// 对于自定义定点数类型,会有to_f64()等方法

这种设计模式允许您为不同类型的标量提供特定于类型的行为,同时保持统一的接口。

相关推荐
superman超哥1 天前
Serde 性能优化的终极武器
开发语言·rust·编程语言·rust serde·serde性能优化·rust开发工具
sayang_shao2 天前
Rust多线程编程学习笔记
笔记·学习·rust
鸿乃江边鸟2 天前
Spark Datafusion Comet 向量化Rust Native--读数据
rust·spark·native·arrow
硬汉嵌入式2 天前
基于Rust构建的单片机Ariel RTOS,支持Cortex-M、RISC-V 和 Xtensa
单片机·rust·risc-v
低调滴开发3 天前
Tauri开发桌面端服务,配置指定防火墙端口
rust·tauri·桌面端·windows防火墙规则
咚为3 天前
Rust Cell使用与原理
开发语言·网络·rust
咸甜适中3 天前
rust的docx-rs库,自定义docx模版批量生成docx文档(逐行注释)
开发语言·rust·docx·docx-rs
FAFU_kyp3 天前
RISC0_ZERO项目在macOs上生成链上证明避坑
开发语言·后端·学习·macos·rust
古城小栈4 天前
开发常用 宏
算法·rust
咸甜适中4 天前
rust的docx-rs库读取docx文件中的文本内容(逐行注释)
开发语言·rust·docx·docx-rs