【unitrix】1.2 unitrix 物理量计算库(lib.rs)

发布于:2025-06-15 ⋅ 阅读:(17) ⋅ 点赞:(0)

一、源码

这是一个用纯 Rust 实现的物理量计算库,具有单位感知功能。

//! A pure Rust library for physical quantity calculations with unit awareness.
//! 纯 Rust 实现的物理量计算库,支持单位感知
//!
//! This library provides:
//! 本库提供以下功能:
//! - Compile-time dimensional analysis with zero runtime cost
//!   - 零运行时开销的编译期量纲分析
//! - Type-safe physical quantity operations
//!   - 类型安全的物理量运算
//! - Mixed constant and variable calculations
//!   - 常量与变量的混合计算
//! - 2D transformation matrices with unit preservation
//!   - 保持单位的2D转换矩阵
//!
//! # Current Features
//! # 当前功能
//! - Basic constant/variable arithmetic operations
//!   - 基础常量/变量算术运算
//! - Physical unit system foundation
//!   - 物理单位系统基础
//! - Dimension checking at compile time
//!   - 编译期量纲检查
//!
//! # Planned Features
//! # 计划功能
//! 1. **Mixed Constant/Variable Calculations**:
//!    1. **常量变量混合计算**:
//!    - Allow expressions combining compile-time constants with runtime variables
//!      - 支持编译期常量与运行时变量的组合表达式
//!    - Optimize constant portions of expressions at compile time
//!      - 在编译期优化表达式中的常量部分
//!
//! 2. **Enhanced Physical Units**:
//!    2. **增强物理单位系统**:
//!    - Comprehensive SI unit system
//!      - 完整的国际单位制
//!    - Unit conversions with dimensional analysis
//!      - 带量纲分析的单位转换
//!    - Custom unit definitions
//!      - 自定义单位
//!
//! 3. **2D Transformation Matrices**:
//!    3. **2D转换矩阵**:
//!    - Homogeneous coordinates for 2D graphics
//!      - 用于2D图形的齐次坐标
//!    - Unit-preserving transformations (translation, rotation, scaling)
//!      - 保持单位的变换(平移/旋转/缩放)
//!    - Matrix operations with physical meaning
//!      - 具有物理意义的矩阵运算
//!
//! # Examples
//! # 示例
//! ```
//! use unitrix::quantity::Length;
//! 
//! // Compile-time unit checking
//! // 编译期单位检查
//! let distance = Length::meters(5.0) + Length::kilometers(2.0);
//! ```
//!
//! # Safety
//! # 安全性
//! - No unsafe code (`#![forbid(unsafe_code)]`)
//!   - 禁用不安全代码
//! - No standard library dependencies (`#![no_std]`)
//!   - 无标准库依赖

#![no_std] // 不依赖标准库
#![forbid(unsafe_code)] // 禁止不安全代码,保证内存安全
#![deny(missing_docs)]  // 无条件使用注释文档
#![doc(html_root_url = "https://docs.rs/unitrix/0.0.4")]  // 文档根URL
#![cfg_attr(docsrs, feature(doc_auto_cfg, doc_cfg))]  // 文档生成时的特性配置

/// Private module containing sealed traits implementation details
/// 私有模块,包含密封trait的实现细节
pub(crate) mod sealed;

/// Numeric types and operations foundation
/// 数字类型和运算基础
pub mod number;

/// Physical quantities with units implementation
/// 带单位的物理量实现
pub mod quantity;

二、库的核心特性

  1. 编译期量纲分析:
  • 在编译时进行单位检查,零运行时开销

  • 确保物理量运算的类型安全

  1. 功能范围:
  • 支持基础算术运算(常量/变量)

  • 提供物理单位系统基础

  • 未来计划支持常量变量混合计算、增强单位系统和2D转换矩阵

三、代码结构

  1. 属性标记:
  • #![no_std]:不依赖 Rust 标准库,适合嵌入式等场景

  • #![forbid(unsafe_code)]:完全禁用不安全代码,保证内存安全

  • #![deny(missing_docs)]:强制要求文档注释,提高代码可维护性

  1. 模块划分:
  • sealed:私有模块,实现密封模式(防止外部实现特定trait)

  • number:提供数字类型和运算基础功能

  • quantity:实现带单位的物理量功能

四、设计特点

  1. 安全性:
  • 完全避免不安全代码

  • 编译期单位检查防止运行时单位错误

  1. 性能:
  • 零成本抽象,大部分检查在编译期完成
  1. 扩展性:
  • 计划支持完整的国际单位制

  • 将支持2D图形变换等高级功能

示例用法

use unitrix::quantity::Length;

// 编译期会自动检查单位一致性
// 自动处理单位转换(千米转米)
let distance = Length::meters(5.0) + Length::kilometers(2.0);

这个库适合需要精确物理计算的场景,如科学计算、工程应用、游戏物理引擎等,能在编译期捕获单位错误,提高代码可靠性。


网站公告

今日签到

点亮在社区的每一天
去签到