physunits · 物理单位库
Type-safe physical quantities with dimensional analysis
带量纲分析的类型安全物理量库
A Rust library for safe unit operations /
Rust实现的类型安全单位计算库
Key Advantages / 核心优势
-
No dependencies - Pure Rust implementation without external dependencies
无依赖库 - 纯Rust实现,不依赖任何外部库 -
Typenum-like constant calculation - Full compile-time dimensional analysis with constant evaluation capabilities
类Typenum的常量计算 - 完整的编译期量纲分析与常量计算能力 -
Var structure bridge - Seamless integration between variable and constant calculations
Var结构桥接 - 完美衔接变量与常量的混合计算 -
Complete operator overloading - Supports all arithmetic operations with dimensional correctness
完整运算符重载 - 支持所有算术运算并保持量纲正确性
Core Architecture / 核心架构
1. Constant System / 常量系统
// 基础常量类型
; // 二进制0节点
; // 二进制1节点
; // 零值
; // +1
; // -1
// 常量运算特性
// 示例实现
### 2. Var Structure / 变量结构
```rust
/// 变量结构体,桥接常量与变量计算
;
// 支持的运算类型
// 与常量的混合运算
3. Dimension System / 量纲系统
/// 国际单位制7大量纲
>);
// 量纲运算示例
4. Unit System / 单位系统
/// SI基础单位结构
>
);
/// 复合单位结构
;
// 单位运算示例
Features / 特性
| Feature | 功能描述 |
|---|---|
| 📏 Compile-time dimensional safety | 编译期量纲安全 |
| ⚡ Zero runtime overhead | 零运行时开销 |
| 🔢 Integer & float support | 支持整数和浮点数 |
| 🔄 Automatic unit conversion | 自动单位转换 |
| 🏷️ Runtime prefix handling | 运行时词头处理 |
| 🧮 Typenum-like constant math | 类Typenum的常量计算 |
| 🌉 Var-based mixed calculation | 基于Var的混合计算 |
| 🔧 Full operator overloading | 完整运算符重载 |
Usage Examples / 使用示例
Basic Operations / 基础运算
use ;
// 常量计算
let force = const(5) * kg * const * m / ;
println!;
// 变量计算
let mass = Var * N;
let acceleration = Var;
let force = mass * acceleration;
Mixed Calculation / 混合计算
use ;
// 编译时常量与运行时变量混合运算
let G = Const * m3 / ;
let m1 = Var; // 地球质量 (kg)
let m2 = Var; // 月球质量 (kg)
let r = Var; // 地月距离 (m)
let f = G * m1 * m2 / ;
println!;
Temperature Conversion / 温度转换
use ;
let boiling = new;
let fahr = boiling.;
println!;
Unit Math / 单位运算
let speed = 60.0 * km / h;
let time = 30.0 * min;
let distance = speed * time; // 自动推导为km单位
Advanced Features / 高级特性
- 常量计算系统
-
二进制编码的常量类型 (B0, B1)
-
基础常量值 (Z0, P1, N1)
-
支持所有算术运算的trait实现
-
常量到运行时的值转换 (to_i32())
- Var结构桥接
-
同时支持变量与常量的运算
-
自动类型转换系统
-
完整的运算符重载 (+, -, *, /, +=, -=, *=, /=)
-
支持i64和f64基础类型
- 量纲系统
-
7大基本量纲的编译期检查
-
量纲运算自动推导
-
支持幂运算 (pow()方法)
-
零开销抽象
Comparison / 对比
| Feature | PhysUnits | uom | nalgebra |
|---|---|---|---|
| Dim Safety | ✅ | ✅ | ❌ |
| Integer Support | ✅ | ⚠️ | ❌ |
| Runtime Prefix | ✅ | ❌ | ❌ |
| No Deps | ✅ | ❌ | ❌ |
| Const Math | ✅ | ⚠️ | ❌ |
| Var Bridge | ✅ | ❌ | ❌ |
| 特性 | PhysUnits | uom | nalgebra |
|---|---|---|---|
| 量纲安全 | ✅ | ✅ | ❌ |
| 整数支持 | ✅ | ⚠️ | ❌ |
| 运行时词头 | ✅ | ❌ | ❌ |
| 无依赖 | ✅ | ❌ | ❌ |
| 常量计算 | ✅ | ⚠️ | ❌ |
| 变量桥接 | ✅ | ❌ | ❌ |
Installation / 安装
[]
= "0.0.4"
Contributing / 贡献指南
We welcome issues and PRs! / 欢迎提交 Issue 和 PR!
Key needs: / 重点需求:
-
More unit definitions / 更多单位定义
-
Real-world physics test cases / 实际物理测试案例
-
Better error messages / 更好的错误提示
-
Constant calculation optimization / 常量计算优化
-
WASM compatibility / WASM兼容性