【quantity】0 README.md文件

PhysUnits · 物理单位库

Type-safe physical quantities with dimensional analysis
带量纲分析的类型安全物理量库

A Rust library for safe unit operations /

Rust实现的类型安全单位计算库

Core Design / 核心设计

1. Dimension / 量纲

rust 复制代码
/// Base SI dimensions / 国际单位制基本量纲
pub struct Dimension<
    L: Integer,  // Length (m) / 长度(米)
    M: Integer,  // Mass (kg) / 质量(千克)
    T: Integer,  // Time (s) / 时间(秒)
    I: Integer,  // Current (A) / 电流(安培)
    Th: Integer, // Temperature (K) / 温度(开尔文) 
    N: Integer,  // Amount (mol) / 物质的量(摩尔)
    J: Integer   // Luminosity (cd) / 发光强度(坎德拉)
>(PhantomData<(L, M, T, I, Th, N, J)>);

2. Unit / 单位

rust 复制代码
/// Unit conversion rules / 单位转换规则
pub trait Unit {
    type Dimension;
    
    /// Convert to base unit / 转换到基准单位
    fn to_base(value: f64) -> f64;
    
    /// Unit symbol / 单位符号
    const SYMBOL: &'static str;
}

3. Quantity / 物理量

rust 复制代码
/// Physical quantity with value and unit / 带单位和值的物理量
pub struct Quantity<V, U: Unit> {
    /// Scalar value / 标量值
    pub value: V,
    _unit: PhantomData<U>
}

impl<V, U: Unit> Quantity<V, U> {
    /// Create new quantity / 创建新物理量
    pub fn new(value: V) -> Self {
        Self { value, _unit: PhantomData }
    }
}

Usage / 使用示例

Basic Conversion / 基础转换

rust 复制代码
use physunits::{Meter, Inch, Quantity};

// Create length / 创建长度
let length = Quantity::<f64, Meter>::new(2.0);

// Convert units / 单位转换
let inches = length.convert::<Inch>();
println!("{} m = {} in", length.value, inches.value);

Temperature / 温度转换

rust 复制代码
use physunits::{Celsius, Fahrenheit};

let boiling = Quantity::<f64, Celsius>::new(100.0);
let fahr = boiling.convert::<Fahrenheit>();
println!("Water boils at {} °F", fahr.value); 

Force Calculation / 力的计算

rust 复制代码
use physunits::{kg, m, s, N};

let mass = 5.0 * kg;
let acceleration = 9.8 * m / (s * s);
let force: Quantity<f64, N> = mass * acceleration;
println!("Force: {} N", force.value);

Features / 特性

Feature 功能描述
📏 Compile-time dimensional safety 编译期量纲安全
⚡ Zero runtime overhead 零运行时开销
🔢 Integer & float support 支持整数和浮点数
🔄 Automatic unit conversion 自动单位转换

Installation / 安装

toml 复制代码
[dependencies]
physunits = "0.0.1"
相关推荐
向上的车轮2 小时前
从零实现一个高性能 HTTP 服务器:深入理解 Tokio 异步运行时与 Pin 机制
rust·系统编程·pin·异步编程·tokio·http服务器
AI自动化工坊6 小时前
OpenFang实战指南:用Rust构建高并发AI Agent操作系统
开发语言·人工智能·ai·rust·agent·ai agent
gsls2008087 小时前
tauri开发环境搭建
rust·npm·tauri
Binarydog_Lee7 小时前
Tauri2 开发入门:应用是如何启动的
前端·rust·tauri
changzehai9 小时前
RustRover + J-Link 一键调试 STM32 教程
stm32·单片机·嵌入式硬件·rust·rustrover
咸甜适中9 小时前
rust序列化和反序列化(json、yaml、toml)详解
开发语言·rust·json
IT 行者10 小时前
CentOS 下源码编译安装完整版 Redis 8.0 指南(附 Rust 工具链详解)
redis·rust·centos
暴躁小师兄数据学院10 小时前
【WEB3.0零基础转换笔记】Rust编程篇-第4讲:控制流
开发语言·笔记·rust·web3·区块链·智能合约
武汉唯众智创10 小时前
Rust系统安全实训入门:唯众网络安全实训室搭建与边缘节点并发优化实操指南
人工智能·rust·网络安全实训室建设·rust系统安全实训
怪我冷i1 天前
解决win11运行cargo run的报错,Blocking waiting for file lock on build directory
rust·web·zed·salvo