Skip to content

Commit 66385ba

Browse files
committed
cargo fmt
1 parent 782f48c commit 66385ba

5 files changed

Lines changed: 100 additions & 90 deletions

File tree

src/elec.rs

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
1-
use core::ops::{Add, BitOr, Neg};
21
use core::fmt;
2+
use core::ops::{Add, BitOr, Neg};
33

44
use crate::units::*;
55

66
#[derive(Debug, Copy, Clone)]
77
pub enum Cct {
8-
Thevenin( Volt, Ohm),
9-
Norton( Amp, Siemen )
8+
Thevenin(Volt, Ohm),
9+
Norton(Amp, Siemen),
1010
}
1111
use self::Cct::*;
1212

1313
impl Cct {
14-
pub fn i_short(self) -> Amp {
14+
pub fn i_short(self) -> Amp {
1515
match self {
16-
Thevenin(v, r) => v/r,
17-
Norton(i, _) => i
16+
Thevenin(v, r) => v / r,
17+
Norton(i, _) => i,
1818
}
1919
}
20-
pub fn v_open(self) -> Volt {
20+
pub fn v_open(self) -> Volt {
2121
match self {
2222
Thevenin(v, _) => v,
23-
Norton(i, g) => i*(1.0/g)
23+
Norton(i, g) => i * (1.0 / g),
2424
}
2525
}
26-
pub fn r_equiv(self) -> Ohm {
26+
pub fn r_equiv(self) -> Ohm {
2727
match self {
2828
Thevenin(_, r) => r,
29-
Norton(_, g) => 1.0/g
29+
Norton(_, g) => 1.0 / g,
3030
}
3131
}
32-
pub fn g_equiv(self) -> Siemen {
32+
pub fn g_equiv(self) -> Siemen {
3333
match self {
34-
Thevenin(_, r) => 1.0/r,
35-
Norton(_, g) => g
34+
Thevenin(_, r) => 1.0 / r,
35+
Norton(_, g) => g,
3636
}
3737
}
3838
}
3939

4040
fn norton_wins(lhs: Cct, rhs: Cct) -> bool {
4141
match (lhs, rhs) {
4242
(Thevenin(_, _), Thevenin(_, _)) => false,
43-
(Norton(_, _), Norton(_, _)) => true,
44-
(Norton(_, g), _) if g.0 < n => true,
45-
(_, Norton(_, g)) if g.0 < n => true,
46-
_ => false
43+
(Norton(_, _), Norton(_, _)) => true,
44+
(Norton(_, g), _) if g.0 < n => true,
45+
(_, Norton(_, g)) if g.0 < n => true,
46+
_ => false,
4747
}
4848
}
4949

@@ -54,12 +54,11 @@ impl BitOr<Cct> for Cct {
5454
let ge = self.g_equiv() + rhs.g_equiv();
5555
let ie = self.i_short() + rhs.i_short();
5656
Norton(ie, ge)
57-
}
58-
else {
57+
} else {
5958
let re = self.r_equiv() | rhs.r_equiv();
6059
let rs = self.r_equiv() + rhs.r_equiv();
61-
let n1 = rhs.r_equiv()/rs;
62-
let n2 = self.r_equiv()/rs;
60+
let n1 = rhs.r_equiv() / rs;
61+
let n2 = self.r_equiv() / rs;
6362
let ve = self.v_open() * n1 + rhs.v_open() * n2;
6463
Thevenin(ve, re)
6564
}
@@ -72,12 +71,11 @@ impl Add<Cct> for Cct {
7271
if norton_wins(self, rhs) {
7372
let ge = self.g_equiv() | rhs.g_equiv();
7473
let gp = self.g_equiv() + rhs.g_equiv();
75-
let n1 = rhs.g_equiv()/gp;
76-
let n2 = self.g_equiv()/gp;
74+
let n1 = rhs.g_equiv() / gp;
75+
let n2 = self.g_equiv() / gp;
7776
let ie = self.i_short() * n1 + rhs.i_short() * n2;
7877
Norton(ie, ge)
79-
}
80-
else {
78+
} else {
8179
let re = self.r_equiv() + rhs.r_equiv();
8280
let ve = self.v_open() + rhs.v_open();
8381
Thevenin(ve, re)
@@ -90,7 +88,7 @@ impl Neg for Cct {
9088
fn neg(self) -> Cct {
9189
match self {
9290
Thevenin(v, r) => Thevenin(-v, r),
93-
Norton(i, g) => Norton(-i, g)
91+
Norton(i, g) => Norton(-i, g),
9492
}
9593
}
9694
}
@@ -99,77 +97,86 @@ impl fmt::Display for Cct {
9997
fn fmt(&self, dst: &mut fmt::Formatter) -> fmt::Result {
10098
match self {
10199
Thevenin(v, r) => write!(dst, "{} + {}", v, r),
102-
Norton(i, g) => write!(dst, "{} | {}", i, g),
100+
Norton(i, g) => write!(dst, "{} | {}", i, g),
103101
}
104-
105102
}
106103
}
107104

108105
// Conversions from elec units to Cct
109106

110107
impl Add<Ohm> for Volt {
111108
type Output = Cct;
112-
fn add(self, rhs: Ohm) -> Cct { Thevenin(self, rhs) }
109+
fn add(self, rhs: Ohm) -> Cct {
110+
Thevenin(self, rhs)
111+
}
113112
}
114113

115114
impl BitOr<Siemen> for Amp {
116115
type Output = Cct;
117-
fn bitor(self, rhs: Siemen) -> Cct { Norton(self, rhs) }
116+
fn bitor(self, rhs: Siemen) -> Cct {
117+
Norton(self, rhs)
118+
}
118119
}
119120

120121
impl BitOr<Ohm> for Cct {
121122
type Output = Cct;
122-
fn bitor(self, rhs: Ohm) -> Cct { self | Thevenin(Volt(0.0), rhs) }
123+
fn bitor(self, rhs: Ohm) -> Cct {
124+
self | Thevenin(Volt(0.0), rhs)
125+
}
123126
}
124127

125128
impl Add<Ohm> for Cct {
126129
type Output = Cct;
127-
fn add(self, rhs: Ohm) -> Cct { self + Thevenin(Volt(0.0), rhs) }
130+
fn add(self, rhs: Ohm) -> Cct {
131+
self + Thevenin(Volt(0.0), rhs)
132+
}
128133
}
129134

130135
impl BitOr<Siemen> for Cct {
131136
type Output = Cct;
132-
fn bitor(self, rhs: Siemen) -> Cct { self | Norton(Amp(0.0), rhs) }
137+
fn bitor(self, rhs: Siemen) -> Cct {
138+
self | Norton(Amp(0.0), rhs)
139+
}
133140
}
134141

135142
impl Add<Siemen> for Cct {
136143
type Output = Cct;
137-
fn add(self, rhs: Siemen) -> Cct { self + Norton(Amp(0.0), rhs) }
144+
fn add(self, rhs: Siemen) -> Cct {
145+
self + Norton(Amp(0.0), rhs)
146+
}
138147
}
139148

140149
#[test]
141150
fn open_evse_cp_example() {
142-
let r7 = 200.0*Ohm(k);
143-
let r6 = 100.0*Ohm(k);
144-
let r5 = 56.0*Ohm(k);
151+
let r7 = 200.0 * Ohm(k);
152+
let r6 = 100.0 * Ohm(k);
153+
let r5 = 56.0 * Ohm(k);
145154

146155
let vcc = Volt(5.0);
147156
let v_cp_hi = Volt(12.0);
148157
let gnd = Volt(0.0);
149158

150-
let cct = v_cp_hi + r7 | gnd + r6 | vcc + r5;
151-
159+
let cct = v_cp_hi + r7 | gnd + r6 | vcc + r5;
160+
152161
fn divider3(v1: Volt, r1: Ohm, v2: Volt, r2: Ohm, v3: Volt, r3: Ohm) -> Volt {
153-
(v1/r1 + v2/r2 + v3/r3)*(r1|r2|r3)
162+
(v1 / r1 + v2 / r2 + v3 / r3) * (r1 | r2 | r3)
154163
}
155164

156165
let v1 = cct.v_open();
157-
let v2 = divider3(v_cp_hi, r7, gnd, r6, vcc,r5);
166+
let v2 = divider3(v_cp_hi, r7, gnd, r6, vcc, r5);
158167

159-
assert_eq!( v1, v2 )
168+
assert_eq!(v1, v2)
160169
}
161170

162171
#[test]
163172
fn doc_example() {
164173
let vcc = Volt(5.0);
165-
let r1 = Ohm(10.0*k);
166-
let r2 = Ohm(5.0*k);
167-
168-
let circuit =
169-
vcc + r1 // A voltage source vcc in series with resistor r1 forms a Cct,
170-
| r2; // which is extended by resistor r2 in parallel
171-
174+
let r1 = Ohm(10.0 * k);
175+
let r2 = Ohm(5.0 * k);
176+
177+
let circuit = vcc + r1 // A voltage source vcc in series with resistor r1 forms a Cct,
178+
| r2; // which is extended by resistor r2 in parallel
179+
172180
let v1 = circuit.v_open(); // v1 is the voltage produced
173-
assert_eq!( v1, r2/(r1 + r2) * vcc)
174-
181+
assert_eq!(v1, r2 / (r1 + r2) * vcc)
175182
}

src/macros.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,62 @@
11
macro_rules! measure {
22
($id:ident, $symbol:expr) => {
3-
43
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Default)]
54
pub struct $id(pub f64);
65

76
impl fmt::Display for $id {
87
fn fmt(&self, dst: &mut fmt::Formatter) -> fmt::Result {
98
let (prefix, scale) = engineering(self.0);
10-
let value = self.0/scale;
9+
let value = self.0 / scale;
1110
let fract = precision(value);
1211
write!(dst, "{:.*}{}{}", fract, value, prefix, $symbol)
1312
}
1413
}
1514

1615
impl Neg for $id {
1716
type Output = $id;
18-
fn neg(self) -> $id { $id(-self.0) }
17+
fn neg(self) -> $id {
18+
$id(-self.0)
19+
}
1920
}
2021

2122
impl Add<$id> for $id {
2223
type Output = $id;
23-
fn add(self, rhs: $id) -> $id {
24+
fn add(self, rhs: $id) -> $id {
2425
$id(self.0 + rhs.0)
2526
}
2627
}
27-
2828

2929
impl Sub<$id> for $id {
3030
type Output = $id;
31-
fn sub(self, rhs: $id) -> $id {
31+
fn sub(self, rhs: $id) -> $id {
3232
$id(self.0 - rhs.0)
3333
}
3434
}
35-
35+
3636
impl Div<$id> for $id {
3737
type Output = f64;
38-
fn div(self, rhs: $id) -> f64 {
38+
fn div(self, rhs: $id) -> f64 {
3939
self.0 / rhs.0
4040
}
4141
}
4242

4343
impl Div<f64> for $id {
4444
type Output = $id;
45-
fn div(self, rhs: f64) -> $id {
45+
fn div(self, rhs: f64) -> $id {
4646
$id(self.0 / rhs)
4747
}
4848
}
4949

5050
impl Mul<f64> for $id {
5151
type Output = $id;
52-
fn mul(self, rhs: f64) -> $id {
52+
fn mul(self, rhs: f64) -> $id {
5353
$id(self.0 * rhs)
5454
}
5555
}
5656

5757
impl Mul<$id> for f64 {
5858
type Output = $id;
59-
fn mul(self, rhs: $id) -> $id {
59+
fn mul(self, rhs: $id) -> $id {
6060
$id(self * rhs.0)
6161
}
6262
}
@@ -65,7 +65,7 @@ macro_rules! measure {
6565

6666
pub(crate) use measure;
6767

68-
macro_rules! product {
68+
macro_rules! product {
6969
($a:ident, $b:ident, $c:ident) => {
7070
impl Mul<$b> for $a {
7171
type Output = $c;
@@ -100,12 +100,11 @@ macro_rules! inverse_sum_inverse {
100100
($id:ident) => {
101101
impl BitOr<$id> for $id {
102102
type Output = $id;
103-
fn bitor(self, rhs: $id) -> $id {
103+
fn bitor(self, rhs: $id) -> $id {
104104
if self.0 == 0.0 && rhs.0 == 0.0 {
105105
$id(0.0)
106-
}
107-
else {
108-
$id(self.0 * rhs.0/(self.0 + rhs.0))
106+
} else {
107+
$id(self.0 * rhs.0 / (self.0 + rhs.0))
109108
}
110109
}
111110
}
@@ -114,7 +113,7 @@ macro_rules! inverse_sum_inverse {
114113

115114
pub(crate) use inverse_sum_inverse;
116115

117-
macro_rules! inverse {
116+
macro_rules! inverse {
118117
($a:ident, $b:ident) => {
119118
impl Mul<$b> for $a {
120119
type Output = f64;
@@ -143,4 +142,4 @@ macro_rules! inverse {
143142
};
144143
}
145144

146-
pub(crate) use inverse;
145+
pub(crate) use inverse;

src/rcnet.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
use core::fmt::Display;
2-
use crate::units::*;
31
use crate::elec::*;
2+
use crate::units::*;
3+
use core::fmt::Display;
44

5-
pub struct Net<Drive : Display + Copy + Default> {
5+
pub struct Net<Drive: Display + Copy + Default> {
66
pub title: &'static str,
77
pub drive: [Drive; 3],
88
pub cap: Farad,
99
pub cct: fn(Drive) -> Cct,
1010
}
1111

12-
impl<Drive : Display + Copy + Default> Net<Drive> {
12+
impl<Drive: Display + Copy + Default> Net<Drive> {
1313
pub fn describe_ac(&self) {
1414
let x: Drive = Default::default();
1515
let r: Ohm = (self.cct)(x).r_equiv();
1616
println!("{} 3db f = {}", self.title, f3db(r * self.cap))
1717
}
1818

19-
2019
pub fn describe_dc(&self) {
2120
println!("{} in -> out", self.title);
2221
for &x in &self.drive {
@@ -33,25 +32,26 @@ impl<Drive : Display + Copy + Default> Net<Drive> {
3332
}
3433
}
3534

36-
3735
/// Design a voltage divider to convert +/-vin to 0/+vout
3836
/// The assumed circuit is:
39-
///
37+
///
4038
/// ```
4139
/// vout + r1 | r2 | vin + r_input
4240
/// ```
43-
///
41+
///
4442
/// Provide atten = vout/vin
4543
/// and r_input
46-
///
44+
///
4745
/// The fn returns (r1, r2)
48-
///
46+
///
4947
pub fn double_to_single_ended(atten: f64, r_input: Ohm) -> (Ohm, Ohm) {
50-
let r1 = atten*r_input;
51-
let r2 = r1/(1.0 - atten);
48+
let r1 = atten * r_input;
49+
let r2 = r1 / (1.0 - atten);
5250
(r1, r2)
5351
}
5452

5553
use core::f64::consts::PI;
5654

57-
pub fn f3db(t: Second) -> Hertz { 1.0 / (2.0 * PI * t) }
55+
pub fn f3db(t: Second) -> Hertz {
56+
1.0 / (2.0 * PI * t)
57+
}

0 commit comments

Comments
 (0)