1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//! Kinds distinguish quantities whose dimensions coincide but whose
//! meanings differ (torque vs energy, both M·L²·T⁻²).
//!
//! Every quantity has a kind; the default [`Anon`] is invisible and behaves
//! exactly like a crate without kinds. A named kind only mixes with itself:
//! adding torque to torque works, adding torque to energy does not compile,
//! and multiplication requires erasing the kind first with
//! [`erase_kind`](crate::Quantity::erase_kind). Enter a kind explicitly with
//! [`cast_kind`](crate::Quantity::cast_kind):
//!
//! ```
//! use danwi::{kind::Torque, prelude::*};
//!
//! let torque = (50.0.N() * 0.4.m()).cast_kind::<Torque>();
//! assert_eq!(torque + torque, (40.0.J()).cast_kind());
//! assert_eq!(torque.erase_kind(), 20.0.J());
//! ```
//!
//! Custom kinds are declared with [`kinds!`](crate::kinds):
//!
//! ```
//! danwi::kinds! {
//! /// Radioactive activity (Bq), distinguished from frequency.
//! Activity;
//! }
//!
//! use danwi::{Quantity, dimension::Frequency, prelude::*};
//!
//! let decay: Quantity<f64, Frequency, Activity> = (37.0.kHz()).cast_kind();
//! assert_eq!((decay + decay).value(), 74_000.0);
//! ```
/// Kind arithmetic for `+`. `Lhs + Rhs` is only defined for kind pairs that
/// implement this; the output kind is the associated type.
/// Kind arithmetic for `-`.
/// Kind arithmetic for `*`. Only [`Anon`] multiplies; named kinds must be
/// erased first.
/// Kind arithmetic for `/`.
/// The anonymous kind: the default for every quantity, with unrestricted
/// arithmetic. Quantities only leave it via
/// [`cast_kind`](crate::Quantity::cast_kind).
/// Declare kinds: zero-sized markers that add and subtract with themselves
/// and nothing else.
///
/// ```
/// danwi::kinds! {
/// /// Moment of force (N·m), distinguished from energy.
/// Torque;
/// }
/// ```
;
}
cratekinds!