mag/
lib.rs

1// lib.rs
2//
3// Copyright (C) 2019-2021  Minnesota Department of Transportation
4// Copyright (C) 2019-2021  Douglas P Lau
5//
6#![doc = include_str!("../README.md")]
7#![forbid(unsafe_code)]
8#![no_std]
9
10// Implement basic ops for a quantity struct
11macro_rules! impl_base_ops {
12    ($quan:ident, $unit:path) => {
13        // <quan> + <quan> => <quan>
14        impl<U> Add for $quan<U>
15        where
16            U: $unit,
17        {
18            type Output = Self;
19            fn add(self, other: Self) -> Self::Output {
20                Self::new(self.quantity + other.quantity)
21            }
22        }
23
24        // <quan> - <quan> => <quan>
25        impl<U> Sub for $quan<U>
26        where
27            U: $unit,
28        {
29            type Output = Self;
30            fn sub(self, other: Self) -> Self::Output {
31                Self::new(self.quantity - other.quantity)
32            }
33        }
34
35        // <quan> * f64 => <quan>
36        impl<U> Mul<f64> for $quan<U>
37        where
38            U: $unit,
39        {
40            type Output = Self;
41            fn mul(self, scalar: f64) -> Self::Output {
42                Self::new(self.quantity * scalar)
43            }
44        }
45
46        // <quan> * i32 => <quan>
47        impl<U> Mul<i32> for $quan<U>
48        where
49            U: $unit,
50        {
51            type Output = Self;
52            fn mul(self, scalar: i32) -> Self::Output {
53                Self::new(self.quantity * f64::from(scalar))
54            }
55        }
56
57        // f64 * <quan> => <quan>
58        impl<U> Mul<$quan<U>> for f64
59        where
60            U: $unit,
61        {
62            type Output = $quan<U>;
63            fn mul(self, other: $quan<U>) -> Self::Output {
64                Self::Output::new(self * other.quantity)
65            }
66        }
67
68        // <quan> / f64 => <quan>
69        impl<U> Div<f64> for $quan<U>
70        where
71            U: $unit,
72        {
73            type Output = Self;
74            fn div(self, scalar: f64) -> Self::Output {
75                Self::new(self.quantity / scalar)
76            }
77        }
78    };
79}
80
81pub mod length;
82pub mod mass;
83pub mod quan;
84mod speed;
85pub mod temp;
86pub mod time;
87
88pub use length::lenpriv::{Area, Length, Volume};
89pub use speed::Speed;
90pub use time::timepriv::{Frequency, Period};