space_units/quantities/
waves.rs1use super::DisplayWithUnit;
2use super::Length;
3use super::Time;
4
5const SPEED_OF_LIGHT_MPS: f64 = 299_792_458.0;
7
8#[must_use]
23#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
24pub struct Frequency(pub(crate) f64);
25
26#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28pub enum FrequencyUnit {
29 Hertz,
31 KiloHertz,
33 MegaHertz,
35 GigaHertz,
37 TeraHertz,
39 Rpm,
41}
42
43impl FrequencyUnit {
44 const fn symbol(self) -> &'static str {
45 match self {
46 Self::Hertz => "Hz",
47 Self::KiloHertz => "kHz",
48 Self::MegaHertz => "MHz",
49 Self::GigaHertz => "GHz",
50 Self::TeraHertz => "THz",
51 Self::Rpm => "rpm",
52 }
53 }
54
55 const fn hz_per_unit(self) -> f64 {
56 match self {
57 Self::Hertz => 1.0,
58 Self::KiloHertz => 1e3,
59 Self::MegaHertz => 1e6,
60 Self::GigaHertz => 1e9,
61 Self::TeraHertz => 1e12,
62 Self::Rpm => 1.0 / 60.0,
63 }
64 }
65}
66
67impl Frequency {
68 pub const fn from_hz(val: f64) -> Self {
70 Self(val)
71 }
72
73 pub const fn from_khz(val: f64) -> Self {
75 Self(val * 1e3)
76 }
77
78 pub const fn from_mhz(val: f64) -> Self {
80 Self(val * 1e6)
81 }
82
83 pub const fn from_ghz(val: f64) -> Self {
85 Self(val * 1e9)
86 }
87
88 pub const fn from_thz(val: f64) -> Self {
90 Self(val * 1e12)
91 }
92
93 pub const fn from_rpm(val: f64) -> Self {
95 Self(val / 60.0)
96 }
97
98 pub const fn in_hz(self) -> f64 {
100 self.0
101 }
102
103 pub const fn in_khz(self) -> f64 {
105 self.0 / 1e3
106 }
107
108 pub const fn in_mhz(self) -> f64 {
110 self.0 / 1e6
111 }
112
113 pub const fn in_ghz(self) -> f64 {
115 self.0 / 1e9
116 }
117
118 pub const fn in_thz(self) -> f64 {
120 self.0 / 1e12
121 }
122
123 pub const fn in_rpm(self) -> f64 {
125 self.0 * 60.0
126 }
127
128 pub fn in_unit(self, unit: FrequencyUnit) -> f64 {
130 self.0 / unit.hz_per_unit()
131 }
132
133 pub fn display_as(self, unit: FrequencyUnit) -> DisplayWithUnit {
135 DisplayWithUnit {
136 value: self.in_unit(unit),
137 symbol: unit.symbol(),
138 }
139 }
140
141 pub fn period(self) -> Time {
143 Time::from_s(1.0 / self.0)
144 }
145
146 pub fn wavelength(self) -> Length {
148 Length::from_m(SPEED_OF_LIGHT_MPS / self.0)
149 }
150
151 pub fn abs(self) -> Self {
153 Self(self.0.abs())
154 }
155}
156
157impl_quantity_display!(Frequency, "Hz");
158
159impl_common_ops!(Frequency);
160
161impl Time {
163 pub fn frequency(self) -> Frequency {
165 Frequency(1.0 / self.0)
166 }
167}
168
169#[must_use]
181#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
182pub struct DataRate(pub(crate) f64);
183
184#[derive(Debug, Clone, Copy, PartialEq, Eq)]
186pub enum DataRateUnit {
187 BitsPerSecond,
189 KiloBitsPerSecond,
191 MegaBitsPerSecond,
193 GigaBitsPerSecond,
195 BytesPerSecond,
197 KiloBytesPerSecond,
199 MegaBytesPerSecond,
201 GigaBytesPerSecond,
203}
204
205impl DataRateUnit {
206 const fn symbol(self) -> &'static str {
207 match self {
208 Self::BitsPerSecond => "bps",
209 Self::KiloBitsPerSecond => "kbps",
210 Self::MegaBitsPerSecond => "Mbps",
211 Self::GigaBitsPerSecond => "Gbps",
212 Self::BytesPerSecond => "B/s",
213 Self::KiloBytesPerSecond => "KB/s",
214 Self::MegaBytesPerSecond => "MB/s",
215 Self::GigaBytesPerSecond => "GB/s",
216 }
217 }
218
219 const fn bps_per_unit(self) -> f64 {
220 match self {
221 Self::BitsPerSecond => 1.0,
222 Self::KiloBitsPerSecond => 1e3,
223 Self::MegaBitsPerSecond => 1e6,
224 Self::GigaBitsPerSecond => 1e9,
225 Self::BytesPerSecond => 8.0,
226 Self::KiloBytesPerSecond => 8e3,
227 Self::MegaBytesPerSecond => 8e6,
228 Self::GigaBytesPerSecond => 8e9,
229 }
230 }
231}
232
233impl DataRate {
234 pub const fn from_bps(val: f64) -> Self {
236 Self(val)
237 }
238
239 pub const fn from_kbps(val: f64) -> Self {
241 Self(val * 1e3)
242 }
243
244 pub const fn from_mbps(val: f64) -> Self {
246 Self(val * 1e6)
247 }
248
249 pub const fn from_gbps(val: f64) -> Self {
251 Self(val * 1e9)
252 }
253
254 pub const fn from_bytes_ps(val: f64) -> Self {
256 Self(val * 8.0)
257 }
258
259 pub const fn from_kbytes_ps(val: f64) -> Self {
261 Self(val * 8e3)
262 }
263
264 pub const fn from_mbytes_ps(val: f64) -> Self {
266 Self(val * 8e6)
267 }
268
269 pub const fn from_gbytes_ps(val: f64) -> Self {
271 Self(val * 8e9)
272 }
273
274 pub const fn in_bps(self) -> f64 {
276 self.0
277 }
278
279 pub const fn in_kbps(self) -> f64 {
281 self.0 / 1e3
282 }
283
284 pub const fn in_mbps(self) -> f64 {
286 self.0 / 1e6
287 }
288
289 pub const fn in_gbps(self) -> f64 {
291 self.0 / 1e9
292 }
293
294 pub const fn in_bytes_ps(self) -> f64 {
296 self.0 / 8.0
297 }
298
299 pub const fn in_kbytes_ps(self) -> f64 {
301 self.0 / 8e3
302 }
303
304 pub const fn in_mbytes_ps(self) -> f64 {
306 self.0 / 8e6
307 }
308
309 pub const fn in_gbytes_ps(self) -> f64 {
311 self.0 / 8e9
312 }
313
314 pub fn in_unit(self, unit: DataRateUnit) -> f64 {
316 self.0 / unit.bps_per_unit()
317 }
318
319 pub fn display_as(self, unit: DataRateUnit) -> DisplayWithUnit {
321 DisplayWithUnit {
322 value: self.in_unit(unit),
323 symbol: unit.symbol(),
324 }
325 }
326
327 pub fn abs(self) -> Self {
329 Self(self.0.abs())
330 }
331}
332
333impl_quantity_display!(DataRate, "bps");
334
335impl_common_ops!(DataRate);