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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
use core::{cmp, fmt, ops};
#[derive(Clone, Copy, Debug)]
pub struct Voltage {
raw: i64,
}
impl Voltage {
#[inline]
pub const fn from_micro_volts(value: i64) -> Voltage {
Voltage { raw: value }
}
#[inline]
pub fn micro_volts(&self) -> i64 {
self.raw
}
#[inline]
pub fn milli_volts(&self) -> f64 {
self.raw as f64 / 1_000_f64
}
#[inline]
pub fn volts(&self) -> f64 {
self.raw as f64 / 1_000_000_f64
}
#[inline]
pub fn kilo_volts(&self) -> f64 {
self.raw as f64 / 1_000_000_000_f64
}
#[inline]
pub const fn is_zero(&self) -> bool {
self.raw == 0
}
#[inline]
pub const fn is_positive(&self) -> bool {
self.raw >= 0
}
#[inline]
pub const fn is_negative(&self) -> bool {
self.raw < 0
}
#[inline]
pub const fn abs(&self) -> Voltage {
Voltage::from_micro_volts(self.raw.abs())
}
#[inline]
pub const fn invert(&self) -> Voltage {
Voltage::from_micro_volts(self.raw * -1)
}
#[inline]
pub const fn zero() -> Self {
Voltage::from_micro_volts(0)
}
}
impl PartialEq for Voltage {
#[inline]
fn eq(&self, other: &Voltage) -> bool {
self.raw == other.raw
}
}
impl Eq for Voltage {}
impl PartialOrd for Voltage {
#[inline]
fn partial_cmp(&self, other: &Voltage) -> Option<cmp::Ordering> {
self.raw.partial_cmp(&other.raw)
}
}
impl Ord for Voltage {
#[inline]
fn cmp(&self, other: &Voltage) -> cmp::Ordering {
self.raw.cmp(&other.raw)
}
}
impl ops::Add for Voltage {
type Output = Voltage;
#[inline]
fn add(self, other: Voltage) -> Voltage {
self.raw
.checked_add(other.raw)
.map(Voltage::from_micro_volts)
.expect("Overflow when adding voltage values")
}
}
impl ops::Sub for Voltage {
type Output = Voltage;
#[inline]
fn sub(self, other: Voltage) -> Voltage {
self.raw
.checked_sub(other.raw)
.map(Voltage::from_micro_volts)
.expect("Overflow when subtracting voltage values")
}
}
macro_rules! impl_mul_for_integer {
($i: ty) => {
impl ops::Mul<$i> for Voltage {
type Output = Voltage;
#[inline]
fn mul(self, other: $i) -> Voltage {
self.raw
.checked_mul(other as i64)
.map(Voltage::from_micro_volts)
.expect("Overflow when multiplying voltage value")
}
}
};
}
impl_mul_for_integer!(u8);
impl_mul_for_integer!(u16);
impl_mul_for_integer!(u32);
impl_mul_for_integer!(u64);
impl_mul_for_integer!(i8);
impl_mul_for_integer!(i16);
impl_mul_for_integer!(i32);
impl_mul_for_integer!(i64);
impl ops::Mul<f32> for Voltage {
type Output = Voltage;
#[inline]
fn mul(self, scale_factor: f32) -> Voltage {
self * scale_factor as f64
}
}
impl ops::Mul<f64> for Voltage {
type Output = Voltage;
#[inline]
fn mul(self, scale_factor: f64) -> Voltage {
let result = match scale_factor {
_ if scale_factor.is_infinite() => {
panic!("Cannot multiply voltage value by infinity")
}
_ if scale_factor.is_nan() => panic!("Cannot multiply voltage value by NaN"),
_ => self.raw as f64 * scale_factor,
};
Voltage::from_micro_volts(result as i64)
}
}
macro_rules! impl_div_for_integer {
($i: ty) => {
impl ops::Div<$i> for Voltage {
type Output = Voltage;
#[inline]
fn div(self, divisor: $i) -> Voltage {
if divisor == 0 {
panic!("Cannot divide voltage value by zero");
}
self.raw
.checked_div(divisor as i64)
.map(Voltage::from_micro_volts)
.expect("Overflow when dividing voltage value")
}
}
};
}
impl_div_for_integer!(u8);
impl_div_for_integer!(u16);
impl_div_for_integer!(u32);
impl_div_for_integer!(u64);
impl_div_for_integer!(i8);
impl_div_for_integer!(i16);
impl_div_for_integer!(i32);
impl_div_for_integer!(i64);
impl ops::Div<f32> for Voltage {
type Output = Voltage;
#[inline]
fn div(self, divisor: f32) -> Voltage {
self / divisor as f64
}
}
impl ops::Div<f64> for Voltage {
type Output = Voltage;
#[inline]
fn div(self, divisor: f64) -> Voltage {
let result = match divisor {
_ if divisor == 0f64 => panic!("Cannot divide voltage value by zero"),
_ if divisor.is_infinite() => {
panic!("Cannot divide voltage value by infinity")
}
_ if divisor.is_nan() => panic!("Cannot divide voltage value by NaN"),
_ => (self.raw as f64) / divisor,
};
Voltage::from_micro_volts(result as i64)
}
}
pub trait FromInteger {
fn micro_volts(self) -> Voltage;
fn milli_volts(self) -> Voltage;
fn volts(self) -> Voltage;
fn kilo_volts(self) -> Voltage;
}
macro_rules! impl_voltage_from_integer {
($i: ty) => {
impl FromInteger for $i {
#[inline]
fn micro_volts(self) -> Voltage {
Voltage::from_micro_volts(self as i64)
}
#[inline]
fn milli_volts(self) -> Voltage {
let microvolts = (self as i64)
.checked_mul(1_000)
.expect("Overflow when converting millivolts to microvolts");
Voltage::from_micro_volts(microvolts)
}
#[inline]
fn volts(self) -> Voltage {
let microvolts = (self as i64)
.checked_mul(1_000_000)
.expect("Overflow when converting volts to microvolts");
Voltage::from_micro_volts(microvolts)
}
#[inline]
fn kilo_volts(self) -> Voltage {
let microvolts = (self as i64)
.checked_mul(1_000_000_000)
.expect("Overflow when converting kilovolts to microvolts");
Voltage::from_micro_volts(microvolts)
}
}
};
}
impl_voltage_from_integer!(u8);
impl_voltage_from_integer!(u16);
impl_voltage_from_integer!(u32);
impl_voltage_from_integer!(u64);
impl_voltage_from_integer!(i8);
impl_voltage_from_integer!(i16);
impl_voltage_from_integer!(i32);
impl_voltage_from_integer!(i64);
pub trait FromFloat {
fn micro_volts(self) -> Voltage;
fn milli_volts(self) -> Voltage;
fn volts(self) -> Voltage;
fn kilo_volts(self) -> Voltage;
}
macro_rules! impl_voltage_from_float {
($f: ty) => {
impl FromFloat for $f {
#[inline]
fn micro_volts(self) -> Voltage {
Voltage::from_micro_volts(self as i64)
}
#[inline]
fn milli_volts(self) -> Voltage {
let microvolts = (self as f64) * 1_000f64;
Voltage::from_micro_volts(microvolts as i64)
}
#[inline]
fn volts(self) -> Voltage {
let microvolts = (self as f64) * 1_000_000f64;
Voltage::from_micro_volts(microvolts as i64)
}
#[inline]
fn kilo_volts(self) -> Voltage {
let microvolts = (self as f64) * 1_000_000_000f64;
Voltage::from_micro_volts(microvolts as i64)
}
}
};
}
impl_voltage_from_float!(f32);
impl_voltage_from_float!(f64);
impl fmt::Display for Voltage {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let sign = if self.raw < 0 { "-" } else { "" };
let microvolts = self.micro_volts().abs() as f64;
let kilovolts = microvolts / 1_000_000_000f64;
let volts = microvolts / 1_000_000f64;
let millivolts = microvolts / 1_000f64;
if kilovolts >= 1f64 {
write!(f, "{sign}{kilovolts:.2} kV")
} else if volts >= 1f64 {
write!(f, "{sign}{volts:.2} V")
} else if millivolts > 0f64 {
write!(f, "{sign}{millivolts:.2}mV")
} else {
write!(f, "{sign}{microvolts:.2}μV")
}
}
}