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

【unitrix】 3.4 类型级逻辑运算(bit.rs)

一、源码

这段代码实现了一个类型级(type-level)的比特位系统,用于在编译时进行布尔逻辑运算。

//! 类型级比特位实现
//!
//! 这些是基础的比特位类型,作为本库中其他数值类型的构建基础
//!
//! 已实现的**类型运算符**:
//!
//! - 来自 `core::ops` 的:`BitAnd`(与), `BitOr`(或), `BitXor`(异或) 和 `Not`(非)
//! - 比较操作:`PartialEq`, `Eq`
//! - 转换操作:`From<bool>`, `Into<bool>`
//!use core::ops::{BitAnd, BitOr, BitXor, Not};use crate::sealed::Sealed;
use crate::number::{Cmp, Max, Min, Equal, Less, Greater};/// 编译时比特位的标记特征
///
/// 这个 trait 定义了类型级布尔值的基本操作和行为,
/// 包括构造、转换和常量值访问。
pub trait Boolean: Sealed + Copy + Default + 'static {/// 布尔值的编译时常量表示const BOOL: bool;/// 创建一个该类型的新实例fn new() -> Self;/// 获取当前实例对应的运行时布尔值fn to_bool(&self) -> bool {Self::BOOL}
}/// 类型级比特位0(逻辑假)
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Debug, Default)]
pub struct O;impl O {/// 创建一个新的 `O` 实例#[inline(always)]pub const fn new() -> Self {O}
}/// 类型级比特位1(逻辑真)
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Debug, Default)]
pub struct I;impl I {/// 创建一个新的 `I` 实例#[inline(always)]pub const fn new() -> Self {I}
}// 为布尔类型实现密封标记
impl Sealed for O {}
impl Sealed for I {}impl Boolean for O {const BOOL: bool = false;#[inline(always)]fn new() -> Self {Self}
}impl Boolean for I {const BOOL: bool = true;#[inline(always)]fn new() -> Self {Self}
}// 为bit时的功能
impl Cmp<O> for O {type Output = Equal;#[inline]fn compare(&self, _: &O) -> Self::Output {Equal::new()}
}impl Cmp<I> for O {type Output = Less;#[inline]fn compare(&self, _: &I) -> Self::Output {Less::new()}
}impl Cmp<O> for I {type Output = Greater;#[inline]fn compare(&self, _: &O) -> Self::Output {Greater::new()}
}impl Cmp<I> for I {type Output = Equal;#[inline]fn compare(&self, _: &I) -> Self::Output {Equal::new()}
}// Min
impl Min<O> for O {type Output = O;#[inline]fn min(self, _: O) -> O {self}
}
impl Min<I> for O {type Output = O;#[inline]fn min(self, _: I) -> O {self}
}
impl Min<O> for I {type Output = O;#[inline]fn min(self, rhs: O) -> O {rhs}
}
impl Min<I> for I {type Output = I;#[inline]fn min(self, _: I) -> I {self}
}// Max
impl Max<O> for O {type Output = O;#[inline]fn max(self, _: O) -> O {self}
}
impl Max<I> for O {type Output = I;#[inline]fn max(self, rhs: I) -> I {rhs}
}
impl Max<O> for I {type Output = I;#[inline]fn max(self, _: O) -> I {self}
}
impl Max<I> for I {type Output = I;#[inline]fn max(self, _: I) -> I {self}
}// 逻辑值
pub type False = O;
pub type True = I;// 实现所有逻辑运算/// 实现逻辑非运算
impl Not for O {type Output = I;#[inline(always)]fn not(self) -> Self::Output {I}
}impl Not for I {type Output = O;#[inline(always)]fn not(self) -> Self::Output {O}
}/// 实现逻辑与运算
impl<Rhs: Boolean> BitAnd<Rhs> for O {type Output = Self;#[inline(always)]fn bitand(self, _: Rhs) -> Self::Output {Self}
}impl<Rhs: Boolean> BitAnd<Rhs> for I {type Output = Rhs;#[inline(always)]fn bitand(self, rhs: Rhs) -> Self::Output {rhs}
}/// 实现逻辑或运算
impl<Rhs: Boolean> BitOr<Rhs> for O {type Output = Rhs;#[inline(always)]fn bitor(self, rhs: Rhs) -> Self::Output {rhs}
}impl<Rhs: Boolean> BitOr<Rhs> for I {type Output = Self;#[inline(always)]fn bitor(self, _: Rhs) -> Self::Output {self}
}/// 实现逻辑异或运算
impl BitXor<O> for O {type Output = O;#[inline(always)]fn bitxor(self, _: O) -> Self::Output {O}
}impl BitXor<O> for I {type Output = I;#[inline(always)]fn bitxor(self, _: O) -> Self::Output {I}
}impl BitXor<I> for O {type Output = I;#[inline(always)]fn bitxor(self, _: I) -> Self::Output {I}
}impl BitXor<I> for I {type Output = O;#[inline(always)]fn bitxor(self, _: I) -> Self::Output {O}
}// 实现转换操作
impl From<True> for bool {fn from(_: True) -> bool { true }
}impl From<False> for bool {fn from(_: False) -> bool { false }
}

二、代码分析

  1. 核心概念
  • 类型级编程:通过在类型系统层面(而非运行时)表示值和操作,利用Rust的类型系统在编译期完成计算。

  • 标记特征:

    • Boolean trait定义了类型级布尔值的基本操作和行为

    • Sealed trait(来自crate::sealed)用于限制用户不能实现自己的Boolean类型

  1. 主要类型
  • O:表示逻辑假/0的类型

  • I:表示逻辑真/1的类型

  • 类型别名:

    • False = O

    • True = I

三、实现的功能

基本功能
  1. 构造与转换:
  • new()方法创建实例

  • to_bool()和From实现用于与运行时bool值的转换

  • BOOL关联常量表示编译时布尔值

比较操作:
  • 实现了Cmp trait用于类型比较,返回Equal/Less/Greater
逻辑运算

实现了所有基本逻辑运算:

  • 非运算 (Not):

    • !O = I

    • !I = O

  • 与运算 (BitAnd):

    • O & x = O

    • I & x = x

  • 或运算 (BitOr):

    • O | x = x

    • I | x = I

异或运算 (BitXor):
  • O ^ O = O

  • O ^ I = I

  • I ^ O = I

  • I ^ I = O

其他运算
  • Min/Max:实现了最小值和最大值运算

三、设计特点

  1. 零运行时开销:所有操作都在编译时通过类型系统完成,运行时只是简单的类型转换。

  2. 强类型安全:通过Rust的类型系统保证逻辑运算的正确性。

  3. 扩展性:作为基础构建块,可以用于构建更复杂的类型级数值系统。

四、使用场景

这种类型级布尔系统通常用于:

  • 编译时条件检查

  • 类型级状态机

  • 复杂类型系统的约束条件

  • 零成本抽象的API设计

这种技术在Rust的类型系统编程和嵌入式领域特别有用,可以在编译时捕获更多错误并减少运行时检查。

http://www.lqws.cn/news/453709.html

相关文章:

  • 广州AR公司诚推广州华锐互动​
  • 常用 Docker 命令整理
  • 设置vscode使用eslint
  • SynchronizedMap 和 ConcurrentHashMap 的区别
  • EfficientVLA:面向视觉-语言-动作模型无训练的加速与压缩
  • Xilinx XC7A12T‑1CPG238I Artix‑7 FPGA
  • 08-Python文件处理
  • MySQL EXPLAIN中的key_len终极指南:精准掌握索引使用情况
  • 【unitrix】 3.5 类型级别的比较系统(cmp.rs)
  • 【机器学习实战笔记 12】集成学习:AdaBoost算法
  • 分布式系统中的 Kafka:流量削峰与异步解耦(二)
  • 高性能群集部署技术-Nginx+Tomcat负载均衡群集
  • Docker Swarm
  • 如何轻松地将音乐从 iPhone 传输到 Mac?
  • Element UI 表格中实现搜索关键字高亮的
  • 华为OD机考-亲子游戏-BFS(JAVA 2025B卷 200分)
  • OCCT基础类库介绍:Modeling Algorithm - Sewing
  • 如何正确处理音频数据:16位整数与32位浮点数
  • Agent轻松通-P3:分析我们的Agent
  • CppCon 2017 学习:Mocking Frameworks Considered
  • 您的元服务存在问题,不符合元服务UX设计规范
  • 从零开始:飞牛云NAS+Docker搭建WordPress全流程
  • (链表:哈希表 + 双向链表)146.LRU 缓存
  • XML在线格式化工具
  • MySQL基础多表查询
  • docker安装datax详细步骤
  • AUTOSAR实战教程--OS调试利器ORTI文件使用说明OSEK调试方法
  • OBCP第二章 OceanBase 存储引擎高级技术学习笔记
  • 63 网络交互的过程中目标设备的选择
  • PROFIBUS DP 转 EtherCAT 网关:冶金自动化高效协同的基石