当前位置: 首页 > 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的类型系统编程和嵌入式领域特别有用,可以在编译时捕获更多错误并减少运行时检查。

相关文章:

  • 【Docker】docker-compose中的nginx为何突然访问不到服务了?
  • 【CS创世SD NAND征文】STM32户外无线终端管理设备的数据存储方案
  • 拼多多消息对接、支付服务策略(策略+工厂)
  • supervisor /usr/bin/dotnet: cannot execute binary file
  • cili3d笔记20 正交投影3d重建笔记1
  • GoFrame的Gtoken--基于电商项目以及GPT-o3的辅助解惑
  • 如何轻松地将联系人从 iPhone 转移到 iPhone?
  • window显示驱动开发—输出合并器阶段
  • django FileSystemStorage is located outside of the base path component
  • 【前端隐蔽 Bug 深度剖析:SVG 组件复用中的 ID 冲突陷阱】
  • QT的一些介绍
  • Pinia在多步骤表单中的实践应用
  • DDoS防护体系构建——从基础限速到智能调度
  • C++容器之 forward_list (单向链表)使用说明
  • 层级冲突的处理,弹窗生成遮罩问题
  • http通信测试,模拟客户端
  • 力扣-169.多数元素
  • 三种经典算法无人机三维路径规划对比(SMA、HHO、GWO三种算法),Matlab代码实现
  • 医学图像处理期末复习
  • Ruoyi(若依)整合websocket实现信息推送功能(消息铃铛)
  • 网站制作源码版权/刷关键词排名软件
  • 新疆建设云网站/寻找客户资源的网站
  • 浏览器73qcc/站长工具seo诊断
  • perl 动态网站开发/海外广告投放公司
  • 成都手机wap网站制作/站长素材音效网
  • 网站怎么做压力测试/网络营销策划书总结