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

【unitrix】 1.8 常量约束(const_traits.rs)

一、源码

这段代码是用Rust实现的类型级数字系统,使用类型来表示整数并定义它们的属性。这是一种在编译时进行数学运算的技术,常用于需要编译时验证的场景。

use crate::sealed::Sealed;
use crate::number::{Z0, P1, N1, B0, B1};// ========== Marker Traits Definition ==========
// ========== 标记特质定义 ==========/// Integer type marker trait
/// 整数类型标记特质
pub trait Integer: Default+ Sealed + Copy + 'static {/// Convert to i32 (will be deprecated after Var implementation)/// 转换为i32 (Var方法完善后将取消)fn to_i32() -> i32;
}/// Non-zero integer marker
/// 非零整数标记
pub trait NonZero: Integer {}// Integer not equal to 1
/// 不等于1的整数标记
pub trait NonOne: Integer {}/// Integer not equal to -1
/// 不等于-1的整数标记
pub trait NonNegOne: Integer {}// Unsigned integer marker
/// 无符号整数标记
pub trait Unsigned: Integer {}// ========== NonZero Implementations ==========
// ========== NonZero 实现 ==========
impl NonZero for P1 {}
impl NonZero for N1 {}
impl<H: NonZero> NonZero for B0<H> {}
impl<H: NonZero> NonZero for B1<H> {}// ========== Integer Implementations ==========
// ========== Integer 实现 ==========// Z0 represents 0
// Z0 表示 0
impl Integer for Z0 {#[inline(always)]fn to_i32() -> i32 {0}
}// P1 represents +1
// P1 表示 +1
impl Integer for P1 {#[inline(always)]fn to_i32() -> i32 {1}
}// N1 represents -1
// N1 表示 -1
impl Integer for N1 {#[inline(always)]fn to_i32() -> i32 {-1}
}// B0<H> represents H * 2
// B0<H> 表示 H * 2
impl<H: NonZero> Integer for B0<H> {#[inline(always)]fn to_i32() -> i32 {H::to_i32() * 2}
}// B1<H> represents H * 2 + 1
// B1<H> 表示 H * 2 + 1
impl<H: NonZero> Integer for B1<H> {#[inline(always)]fn to_i32() -> i32 {H::to_i32() * 2 + 1}
}// ========== NonOne Implementations ==========
// ========== NonOne 实现 ==========
impl NonOne for Z0 {}
impl NonOne for N1 {}
impl<H: NonZero> NonOne for B0<H> {}
impl<H: NonZero> NonOne for B1<H> {}// ========== NonNegOne Implementations ==========
// ========== NonNegOne 实现 ==========
impl NonNegOne for Z0 {}
impl NonNegOne for P1 {}
impl<H: NonZero> NonNegOne for B0<H> {}
impl<H: NonZero> NonNegOne for B1<H> {}// ========== Unsigned Implementations ==========
// ========== Unsigned 实现 ==========
impl Unsigned for Z0 {}
impl Unsigned for P1 {}
impl<H: NonZero + NonNegOne> Unsigned for B0<H> {}
impl<H: NonZero + NonNegOne> Unsigned for B1<H> {}

二、代码分析

  1. 基本结构
use crate::sealed::Sealed;
use crate::number::{Z0, P1, N1, B0, B1};
  • 引入了密封模式(Sealed)和一些数字类型(Z0, P1, N1, B0, B1)
  1. 标记特质(Trait)定义
    定义了一系列标记特质来描述数字类型的属性:
  • Integer: 基础整数特质,要求实现Default、Sealed、Copy和’static,并提供一个to_i32()方法

  • NonZero: 标记非零整数

  • NonOne: 标记不等于1的整数

  • NonNegOne: 标记不等于-1的整数

  • Unsigned: 标记无符号整数(非负整数)

  1. 数字类型表示
    系统使用类型来表示数字:
  • Z0: 表示数字0

  • P1: 表示+1

  • N1: 表示-1

  • B0: 表示H*2 (二进制表示,在末尾加0)

  • B1: 表示H*2+1 (二进制表示,在末尾加1)

  1. 实现细节
  • NonZero实现: P1、N1和所有以B0、B1结尾的非零数字都是NonZero

  • Integer实现:

  • Z0转换为0

  • P1转换为1

  • N1转换为-1

  • B0转换为H::to_i32() * 2

  • B1转换为H::to_i32() * 2 + 1

  • NonOne实现: 所有不等于1的类型

  • NonNegOne实现: 所有不等于-1的类型

  • Unsigned实现: 所有非负整数(0、1和所有以B0、B1结尾的非负非-1数字)

  1. 设计模式
  • 密封模式(Sealed): 防止外部类型实现这些特质

  • 类型级编程: 使用类型而不是值来表示数字,在编译时进行计算

  • 标记特质: 用于分类和约束类型

  1. 用途
    这种设计通常用于:
  • 编译时维度检查

  • 类型安全的状态机

  • 编译时计算的数学运算

  • 需要强类型保证的领域

例如,可以创建类型安全的数组,其长度在类型中编码,并在编译时检查操作是否合法。

这种技术虽然增加了编译时复杂度,但可以提供更强的类型安全和运行时零成本抽象。

http://www.xdnf.cn/news/1056403.html

相关文章:

  • SOLIDWORKS的“12”个简单高效的草图绘制规则,全部应用成为草图大师!
  • SpringBoot常用注解
  • C++ Builder xe 关于ListView的自然排序功能排序效果与Windows资源管理器相同
  • 蛋白分析工具和数据库
  • 鼓励建设性对抗,反对攻击性评论
  • 计量经济学EViews软件题与证明题预测
  • Java 多线程轮流打印 ABC 的 4 种实现方式详解
  • 关于脉冲功率技术的认识
  • 【Python训练营打卡】day53 @浙大疏锦行
  • Java30:SpringBoot3
  • 数据库优化实战分享
  • Python 基础语法(3)【适合0基础】
  • 你听过网关支付吗?它是什么?
  • 2.7 获取激光雷达数据与避障
  • 重复文件检测提取(C#编写的winform项目源码)
  • 柬埔寨 - 高棉语 点阵方式详解
  • 华晨宇火星演唱会郑州开唱 中西乐交融编曲再升级
  • linux 下 Doris 单点部署
  • 2.4.2 ASPICE的集成与系统测试
  • 1688 API 接口接入说明与文档
  • 键盘效率提升实战,快速训练指法与速度
  • PLC基础知识整理(三菱) - 扩展
  • Pico rp2040开发之Vscode插件+ c/c++独立环境搭建
  • 端侧大模型:边缘智能的破局之战——资源约束下的技术突围
  • cocos2 使用 Layout 组件后,子节点 Label 高度变化后,抖动问题
  • 第一章 绪论
  • Java事务隔离问题详解:脏读、不可重复读与幻读(含解决方案)
  • SpringCloud框架全面学习指南
  • strcpy 和 memcpy
  • Java的抽象类