8 个版本
| 0.5.2 | 2024年6月22日 |
|---|---|
| 0.5.1 | 2024年6月22日 |
| 0.4.1 | 2022年12月31日 |
| 0.3.1 | 2022年1月24日 |
| 0.2.1 | 2022年1月18日 |
#689 in Rust 模式
每月60次下载
在 dims 中使用
32KB
488 行
dims_macro
这允许轻松创建存储值,如距离或质量,同时防止将克加到英尺上。
目的是将这些值保留在内存中作为 Mass 或 Length,无需担心单位,除非在创建值时,以及访问它时(用于显示、存储等)。
重要
这仍然是一个进行中的项目。请期待边缘粗糙,但我正在努力使它们变得平滑。
系统和单位创建
这些特定的系统已经在 dims 中设置好了,但你也可以设置自己的系统
#[macro_use]
use dims_macro;
// You can use this macro to generate a full system.
// It requires a length for use as debug.
measure_system!(name: LengthSystem, debug_unit: FOOT,data_type: f32);
// Allow for conversion between the systems
// Multiple `MultiplyBy` and `DivideBy` traits can be applied for each `MeasureSystem`
impl MultiplyBy<LengthSystem> for LengthSystem {
type Output = AreaSystem;
}
measure_system!(name: AreaSystem, debug_unit: SQUARE_FOOT,data_type: f32);
impl DivideBy<LengthSystem> for AreaSystem {
type Output = LengthSystem;
}
measure_system!(name: TemperatureSystem, debug_unit: FAHRENHEIT,data_type: f32);
// Set up some units, now
pub const FAHRENHEIT: UnitSimple<TemperatureSystem> = UnitSimple {
/// The system is used for compiler warnings, but has no memory impact in production code
// Offset is used to change the zero point. Most of the time, this is zero
offset: 459.67,
// Ratio is multiplied to get to the base unit
ratio: 5.0 / 9.0,
};
pub const CELCIUS: UnitSimple<TemperatureSystem> = UnitSimple {
offset: 273.15,
ratio: 1.0,
};
pub const FOOT: UnitSimple<LengthSystem> = UnitSimple {
offset: 0.0,
ratio: 0.3048,
};
pub const SQUARE_FOOT: UnitSimple<AreaSystem> = UnitSimple {
offset: 0.0,
ratio: 0.09290304,
};
这还包括 si_unit! 宏,它将根据给定信息生成一组(或单个)SI 单位。
待办事项:示例
MeasureSystem 宏
这用于轻松生成一个新的测量系统,使用方式如下
measure_system! {name: Mass, debug_unit: GRAM, data_type: f32}
展开为(使用绝对路径)
#[derive(PartialEq, PartialOrd, Eq, Clone, Copy)]
pub struct Mass;
impl MeasureSystem for Mass {
type N = f32;
#[cfg(feature = "str")]
const DEBUG_UNIT: UnitFormat<Self> = GRAM;
}
DEBUG_UNIT 是调试时值将如何显示。这 可能 是你想要的显示方式,但应该由消费代码明确指定。
其他说明
dims 和 dims_core 的包选项
dims 和 dims_macro 的包选项
-
str(默认)将利用UnitFormat并存储abbr:单位缩写名称(如m或ft)singular:单位的单数名称(如metre或foot)plural:单位的复数名称(如metres或feet)
-
std是默认选项,默认使用标准库。
这启用了UnitFormatTrait(因为函数返回String),但仍然可以使用no_std与str(见下文)来存储单位名称信息。
依赖项
~0.4–0.9MB
~20K SLoC