当前位置: 首页 > news >正文

【typenum】30 类型级别的取负(Neg)

一、源码

三部分代码共同实现了一个类型级别的取负操作。

  1. 别名(src/operator_aliases.rs)
/// Alias for the associated type of `Neg`: `Negate<A> = <A as Neg>::Output`
pub type Negate<A> = <A as Neg>::Output;
  1. 整数实现取负
/// `-Z0 = Z0`
impl Neg for Z0 {type Output = Z0;#[inline]fn neg(self) -> Self::Output {Z0}
}/// `-PInt = NInt`
impl<U: Unsigned + NonZero> Neg for PInt<U> {type Output = NInt<U>;#[inline]fn neg(self) -> Self::Output {NInt::new()}
}/// `-NInt = PInt`
impl<U: Unsigned + NonZero> Neg for NInt<U> {type Output = PInt<U>;#[inline]fn neg(self) -> Self::Output {PInt::new()}
}
  1. 数组实现取负
// ---------------------------------------------------------------------------------------
// Negate an array
use core::ops::Neg;impl Neg for ATerm {type Output = ATerm;#[inline]fn neg(self) -> Self::Output {ATerm}
}impl<V, A> Neg for TArr<V, A>
whereV: Neg,A: Neg,
{type Output = TArr<Negate<V>, Negate<A>>;#[inline]fn neg(self) -> Self::Output {TArr {first: -self.first,rest: -self.rest,}}
}

二、别名定义 (src/operator_aliases.rs)


/// Alias for the associated type of `Neg`: `Negate<A> = <A as Neg>::Output`
pub type Negate<A> = <A as Neg>::Output;

解释:

  • 这是一个类型别名,用于简化代码

  • Negate 等价于 ::Output(即类型A实现Neg特质后的输出类型)

  • 作用:让代码更简洁易读,避免重复写冗长的关联类型语法

三、整数的取负实现

这部分为三种整数类型实现了 Neg 特质(取负操作):


/// `-Z0 = Z0`
impl Neg for Z0 {  // 为零实现取负type Output = Z0;  // 结果还是零#[inline]fn neg(self) -> Self::Output {Z0  // 返回零本身}
}/// `-PInt = NInt`
impl<U: Unsigned + NonZero> Neg for PInt<U> {  // 为正整数实现取负type Output = NInt<U>;  // 结果变为负整数#[inline]fn neg(self) -> Self::Output {NInt::new()  // 创建对应的负整数}
}/// `-NInt = PInt`
impl<U: Unsigned + NonZero> Neg for NInt<U> {  // 为负整数实现取负type Output = PInt<U>;  // 结果变为正整数#[inline]fn neg(self) -> Self::Output {PInt::new()  // 创建对应的正整数}
}

数学规则:

  • -0 = 0

  • -(正数) = 负数

  • -(负数) = 正数

四、数组的取负实现

这部分为类型级别数组实现了 Neg 特质:


impl Neg for ATerm {  // 空数组的取负type Output = ATerm;  // 结果还是空数组#[inline]fn neg(self) -> Self::Output {ATerm  // 返回空数组本身}
}impl<V, A> Neg for TArr<V, A>  // 非空数组的取负
whereV: Neg,    // 要求数组元素类型可取负A: Neg,    // 要求剩余数组类型可取负
{type Output = TArr<Negate<V>, Negate<A>>;  // 结果为每个元素取负的新数组#[inline]fn neg(self) -> Self::Output {TArr {first: -self.first,  // 对第一个元素取负rest: -self.rest,    // 对剩余数组递归取负}}
}

递归处理过程:
对于数组 [a, b, c](实际表示为 TArr(a, TArr(b, TArr(c, ATerm)))):

  • 取负得到:TArr(-a, -[b, c])

  • 递归:TArr(-a, TArr(-b, -[c]))

  • 递归:TArr(-a, TArr(-b, TArr(-c, -ATerm)))

  • 最终:TArr(-a, TArr(-b, TArr(-c, ATerm))) 即 [-a, -b, -c]

五、整体设计模式

这是一个典型的递归类型级别编程示例:

  • 基础情况:空数组取负还是空数组

  • 递归情况:非空数组取负 = 第一个元素取负 + 剩余数组递归取负

类型安全保证:

  • 编译时确保所有数组元素类型都实现了 Neg 特质

  • 编译时完成整个数组的取负操作,无运行时开销

用途: 在编译时进行数学运算验证、符号处理等类型级计算。

http://www.dtcms.com/a/352345.html

相关文章:

  • `mmap` 系统调用详解
  • 设备驱动程序 day62
  • 变压器副边电流计算
  • es-toolkit 是一个现代的 JavaScript 实用库
  • 15公里图传模组:为远程飞行赋能,突破极限的无线连接新选择
  • 微服务-28.配置管理-共享配置
  • 微服务-26.网关登录校验-OpenFeign传递用户信息
  • 前端RSA加密库优缺点总结
  • 42_基于深度学习的非机动车头盔佩戴检测系统(yolo11、yolov8、yolov5+UI界面+Python项目源码+模型+标注好的数据集)
  • Python内存模型与对象系统深度解析
  • 使用Kiro智能开发PYTHON应用程序
  • 25072班8.26日数据结构作业
  • 【CFA三级笔记】资产配置:第一章 资本市场预期(宏观分析)
  • ansible的一些重要配置文件
  • 基于 LQG 控制的轨迹跟踪 —— 从原理到实践
  • 游隼可视化项目
  • python删除执行目录
  • 服装行业/服饰品牌OMS订单管理系统:全渠道零售时代的数字化中枢|商派
  • Chrome您的连接不是私密连接怎么办?试下手敲 thisisunsafe
  • Kafka 生态选型地图、最佳实践与落地清单
  • SELinux相关介绍
  • Android 属性 property 系统
  • MyBatis-Flex多表关联查询指南
  • Dify 父子模式详解:如何实现模块化与高效协作
  • 学习做动画4.回转运动
  • Docker移动安装目录的两种实现方案
  • Qwen3-Coder-30B-A3B-Instruct AWQ 量化
  • 基于51单片机的DS18B20大棚温度监控系统
  • TRUST:a thermohydraulic software package for CFD simulations,开源多物理场数值模拟平台
  • Decode Global:以合规资质筑牢全球服务的根基