Compare commits
2 Commits
dc88a4b79a
...
5031157fea
Author | SHA1 | Date |
---|---|---|
elfeiin | 5031157fea | |
elfeiin | 292f61d582 |
|
@ -1,4 +1,4 @@
|
|||
[package]
|
||||
name = "unidades"
|
||||
version = "1.1.2"
|
||||
version = "1.2.0"
|
||||
edition = "2021"
|
||||
|
|
|
@ -4,6 +4,8 @@ pub mod dimensions;
|
|||
pub mod units;
|
||||
pub mod prefixes;
|
||||
pub mod measurement;
|
||||
pub use units::unit;
|
||||
pub use prefixes::prefix_group;
|
||||
|
||||
pub trait Unit<D> {
|
||||
fn new(v: f64) -> D;
|
||||
|
|
|
@ -28,10 +28,8 @@ impl<
|
|||
const AMOUNT: i8,
|
||||
const INTENSITY: i8,
|
||||
> Measurement<TIME, LENGTH, MASS, CURRENT, TEMPERATURE, AMOUNT, INTENSITY> {
|
||||
pub fn new<U>(v: f64) -> Self
|
||||
where
|
||||
U: Unit<Self> {
|
||||
Self(v.mul(U::scale()).add(U::offset()))
|
||||
pub fn new(v: f64) -> Self {
|
||||
Self(v)
|
||||
}
|
||||
|
||||
pub fn to_units<U>(self) -> f64
|
||||
|
|
|
@ -4,7 +4,8 @@ use crate::{
|
|||
};
|
||||
use core::marker::PhantomData;
|
||||
|
||||
macro_rules! prefixes{
|
||||
#[macro_export]
|
||||
macro_rules! prefix_group{
|
||||
($(($nym: ident, $scale: expr)), *) => {
|
||||
$(
|
||||
pub struct $nym < T >(PhantomData < T >);
|
||||
|
@ -48,7 +49,7 @@ macro_rules! prefixes{
|
|||
};
|
||||
}
|
||||
|
||||
prefixes!{
|
||||
prefix_group!{
|
||||
(Quetta, 1e30),
|
||||
(Ronna, 1e27),
|
||||
(Yotta, 1e24),
|
||||
|
|
18
src/units.rs
18
src/units.rs
|
@ -2,15 +2,17 @@ use crate::{
|
|||
Unit,
|
||||
dimensions::*,
|
||||
};
|
||||
use core::ops::{Mul, Add};
|
||||
|
||||
const YEAR: f64 = 31_557_600.0;
|
||||
const DAY: f64 = 60.0 * 60.0 * 24.0;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! unit{
|
||||
($dimension: ident => $(($nym: ident, ($scale: expr, $offset: expr))), *) => {
|
||||
$(pub struct $nym; impl Unit < $dimension > for $nym {
|
||||
fn new(v: f64) -> $dimension {
|
||||
$dimension:: new::< Self >(v)
|
||||
$dimension:: new(v.mul(Self::scale()).add(Self::offset()))
|
||||
}
|
||||
fn scale() -> f64 {
|
||||
$scale
|
||||
|
@ -22,20 +24,6 @@ macro_rules! unit{
|
|||
};
|
||||
}
|
||||
|
||||
impl Unit<Scalar> for () {
|
||||
fn new(v: f64) -> Scalar {
|
||||
Scalar::new::<()>(v)
|
||||
}
|
||||
|
||||
fn scale() -> f64 {
|
||||
1.0
|
||||
}
|
||||
|
||||
fn offset() -> f64 {
|
||||
0.0
|
||||
}
|
||||
}
|
||||
|
||||
unit!{
|
||||
Time =>(Second, (1.0, 0.0)),
|
||||
(Kalpa, (YEAR * 1e9 * 4.32, 0.0)),
|
||||
|
|
Loading…
Reference in New Issue