Skip to content

Commit 2706961

Browse files
committed
Refactoring - small, miscellaneous code quality improvements
1 parent d5447d2 commit 2706961

File tree

13 files changed

+29
-30
lines changed

13 files changed

+29
-30
lines changed

sim/src/models/batcher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl Batcher {
7878
ports_out: PortsOut { job: job_out_port },
7979
max_batch_time,
8080
max_batch_size,
81-
state: Default::default(),
81+
state: State::default(),
8282
}
8383
}
8484

sim/src/models/exclusive_gateway.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl ExclusiveGateway {
9898
},
9999
port_weights,
100100
store_records,
101-
state: Default::default(),
101+
state: State::default(),
102102
}
103103
}
104104

sim/src/models/gate.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl Gate {
110110
records: default_records_port_name(),
111111
},
112112
store_records,
113-
state: Default::default(),
113+
state: State::default(),
114114
}
115115
}
116116

@@ -238,8 +238,7 @@ impl AsModel for Gate {
238238
Phase::Open => String::from("Open"),
239239
Phase::Closed => String::from("Closed"),
240240
Phase::Pass => format!["Passing {}", self.state.jobs[0].content],
241-
Phase::RespondWhileOpen => String::from("Fetching records"),
242-
Phase::RespondWhileClosed => String::from("Fetching records"),
241+
Phase::RespondWhileOpen | Phase::RespondWhileClosed => String::from("Fetching records"),
243242
}
244243
}
245244

@@ -269,9 +268,7 @@ impl AsModel for Gate {
269268
_services: &mut Services,
270269
) -> Result<Vec<ModelMessage>, SimulationError> {
271270
match &self.state.phase {
272-
Phase::Open => self.send_jobs(),
273-
Phase::Closed => self.send_jobs(),
274-
Phase::Pass => self.send_jobs(),
271+
Phase::Open | Phase::Closed | Phase::Pass => self.send_jobs(),
275272
Phase::RespondWhileOpen => self.send_records_while_open(),
276273
Phase::RespondWhileClosed => self.send_records_while_closed(),
277274
}

sim/src/models/generator.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl Generator {
107107
records: default_records_port_name(),
108108
},
109109
store_records,
110-
state: Default::default(),
110+
state: State::default(),
111111
}
112112
}
113113

@@ -207,8 +207,7 @@ impl AsModel for Generator {
207207
) -> Result<Vec<ModelMessage>, SimulationError> {
208208
match (&self.state.phase, self.store_records) {
209209
(Phase::Generating, true) => self.save_job(services),
210-
(Phase::Generating, false) => self.release_job(services),
211-
(Phase::Saved, _) => self.release_job(services),
210+
(Phase::Generating, false) | (Phase::Saved, _) => self.release_job(services),
212211
(Phase::RecordsFetch, true) => self.release_records(),
213212
(Phase::RecordsFetch, false) => Err(SimulationError::InvalidModelState),
214213
(Phase::Initializing, _) => self.initialize_generation(services),

sim/src/models/load_balancer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl LoadBalancer {
9393
records: default_records_port_name(),
9494
},
9595
store_records,
96-
state: Default::default(),
96+
state: State::default(),
9797
}
9898
}
9999

sim/src/models/parallel_gateway.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl ParallelGateway {
9898
records: default_records_port_name(),
9999
},
100100
store_records,
101-
state: Default::default(),
101+
state: State::default(),
102102
}
103103
}
104104

sim/src/models/processor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl Processor {
119119
records: default_records_port_name(),
120120
},
121121
store_records,
122-
state: Default::default(),
122+
state: State::default(),
123123
}
124124
}
125125

sim/src/models/stochastic_gate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl StochasticGate {
100100
records: default_records_port_name(),
101101
},
102102
store_records,
103-
state: Default::default(),
103+
state: State::default(),
104104
}
105105
}
106106

sim/src/models/stopwatch.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl Stopwatch {
133133
},
134134
metric,
135135
store_records,
136-
state: Default::default(),
136+
state: State::default(),
137137
}
138138
}
139139

@@ -304,8 +304,10 @@ impl AsModel for Stopwatch {
304304
.state
305305
.jobs
306306
.iter()
307-
.filter_map(|job| self.some_duration(job))
308-
.map(|(_, duration)| duration)
307+
.filter_map(|job| {
308+
self.some_duration(job)
309+
.map(|duration_record| duration_record.1)
310+
})
309311
.collect();
310312
format![
311313
"Average {:.3}",
@@ -323,10 +325,12 @@ impl AsModel for Stopwatch {
323325
self.arrival_port(&incoming_message.port_name),
324326
self.store_records,
325327
) {
326-
(ArrivalPort::Start, true) => self.calculate_and_save_job(incoming_message, services),
327-
(ArrivalPort::Stop, true) => self.calculate_and_save_job(incoming_message, services),
328-
(ArrivalPort::Start, false) => self.calculate_job(incoming_message, services),
329-
(ArrivalPort::Stop, false) => self.calculate_job(incoming_message, services),
328+
(ArrivalPort::Start, true) | (ArrivalPort::Stop, true) => {
329+
self.calculate_and_save_job(incoming_message, services)
330+
}
331+
(ArrivalPort::Start, false) | (ArrivalPort::Stop, false) => {
332+
self.calculate_job(incoming_message, services)
333+
}
330334
(ArrivalPort::Metric, _) => self.get_job(),
331335
(ArrivalPort::Records, _) => self.get_records(),
332336
(ArrivalPort::Unknown, _) => Err(SimulationError::InvalidMessage),

sim/src/models/storage.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl Storage {
105105
records: default_records_port_name(),
106106
},
107107
store_records,
108-
state: Default::default(),
108+
state: State::default(),
109109
}
110110
}
111111

sim/src/output_analysis/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ where
4141
/ usize_to_float(points.len())?)
4242
}
4343

44-
/// This function converts a usize to a Float, with an associated SimulationError
45-
/// returned for failed conversions
44+
/// This function converts a usize to a Float, with an associated
45+
/// `SimulationError` returned for failed conversions
4646
fn usize_to_float<T: Float>(unconv: usize) -> Result<T, SimulationError> {
4747
T::from(unconv).ok_or(SimulationError::FloatConvError)
4848
}
@@ -242,9 +242,8 @@ where
242242
// Use that point for deletion determination
243243
self.deletion_point = mser.iter().position(|mser_value| *mser_value == min_mser);
244244
break;
245-
} else {
246-
d -= 1;
247245
}
246+
d -= 1;
248247
}
249248
// Schmeiser [1982] found that, for a fixed total sample size, there
250249
// is little benefit from dividing it into more than k = 30 batches,

sim/src/simulator/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl Simulation {
234234
.collect();
235235
errors?;
236236
self.messages = next_messages;
237-
Ok(self.get_messages().to_vec())
237+
Ok(self.get_messages().clone())
238238
}
239239

240240
/// This method executes simulation `step` calls, until a global time
@@ -264,7 +264,7 @@ impl Simulation {
264264
message_records.extend(self.messages.clone());
265265
Ok(Vec::new())
266266
})
267-
.find(|result| result.is_err())
267+
.find(Result::is_err)
268268
.unwrap_or(Ok(message_records))
269269
}
270270
}

sim/src/utils/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use thiserror::Error;
22

3-
/// SimulationError enumerates all possible errors returned by sim
3+
/// `SimulationError` enumerates all possible errors returned by sim
44
#[derive(Error, Debug)]
55
pub enum SimulationError {
66
/// Represents an invalid model configuration encountered during simulation

0 commit comments

Comments
 (0)