Skip to content

Commit 2941923

Browse files
committed
Make simulator non optional
1 parent 6339c90 commit 2941923

File tree

2 files changed

+23
-47
lines changed

2 files changed

+23
-47
lines changed

src/app.rs

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ pub struct App {
966966
pub label_edit_buffer: String,
967967
// Simulation service - holds simulation state and results
968968
#[serde(skip)]
969-
pub simulator: Option<Simulator>,
969+
pub simulator: Simulator,
970970
}
971971

972972
impl Default for App {
@@ -993,7 +993,7 @@ impl Default for App {
993993
context_menu_pos: Pos2::ZERO,
994994
editing_label: None,
995995
label_edit_buffer: String::new(),
996-
simulator: None,
996+
simulator: Simulator::new(),
997997
}
998998
}
999999
}
@@ -1058,10 +1058,7 @@ impl App {
10581058
}
10591059

10601060
fn is_on(&self, pin: Pin) -> bool {
1061-
let Some(sim) = &self.simulator else {
1062-
return false;
1063-
};
1064-
let Some(v) = sim.current.get(&pin) else {
1061+
let Some(v) = self.simulator.current.get(&pin) else {
10651062
return false;
10661063
};
10671064

@@ -1148,7 +1145,7 @@ impl App {
11481145
self.selected.clear();
11491146
self.drag = None;
11501147
self.connection_manager = ConnectionManager::new(&self.db);
1151-
self.simulator = None;
1148+
self.simulator = Simulator::new();
11521149
}
11531150
});
11541151
}
@@ -1446,10 +1443,8 @@ impl App {
14461443
// self.context_menu_pos = mouse_world - self.viewport_offset; // Convert to screen coordinates
14471444
// }
14481445

1449-
if self.current_dirty || self.simulator.is_none() {
1450-
let mut sim = Simulator::new();
1451-
sim.compute(&self.db);
1452-
self.simulator = Some(sim);
1446+
if self.current_dirty {
1447+
self.simulator.compute(&self.db);
14531448
self.current_dirty = false;
14541449
}
14551450

@@ -2160,39 +2155,24 @@ impl App {
21602155

21612156
// Simulation status
21622157
writeln!(out, "\n=== Simulation Status ===").ok();
2163-
if let Some(sim) = &self.simulator {
2164-
match sim.status {
2165-
SimulationStatus::Stable { iterations } => {
2166-
writeln!(out, "Status: STABLE (after {iterations} iterations)").ok();
2167-
}
2168-
SimulationStatus::Unstable { max_reached } => {
2169-
if max_reached {
2170-
let iters = sim.last_iterations;
2171-
writeln!(out, "Status: UNSTABLE (max iterations: {iters})").ok();
2172-
} else {
2173-
writeln!(out, "Status: UNSTABLE").ok();
2174-
}
2175-
}
2176-
SimulationStatus::Running => {
2177-
writeln!(out, "Status: ⏳ RUNNING...").ok();
2158+
match self.simulator.status {
2159+
SimulationStatus::Stable { iterations } => {
2160+
writeln!(out, "Status: STABLE (after {iterations} iterations)").ok();
2161+
}
2162+
SimulationStatus::Unstable { max_reached } => {
2163+
if max_reached {
2164+
let iters = self.simulator.last_iterations;
2165+
writeln!(out, "Status: UNSTABLE (max iterations: {iters})").ok();
2166+
} else {
2167+
writeln!(out, "Status: UNSTABLE").ok();
21782168
}
21792169
}
2180-
let iters = sim.last_iterations;
2181-
writeln!(out, "Iterations: {iters}").ok();
2182-
let powered = if let Some(simulator) = &self.simulator {
2183-
simulator
2184-
.current
2185-
.iter()
2186-
.filter(|v| *v.1 == Value::One)
2187-
.count()
2188-
} else {
2189-
0
2190-
};
2191-
writeln!(out, "Powered pins: {powered}").ok();
2192-
} else {
2193-
writeln!(out, "Status: No simulation yet").ok();
2170+
SimulationStatus::Running => {
2171+
writeln!(out, "Status: ⏳ RUNNING...").ok();
2172+
}
21942173
}
2195-
2174+
let iters = self.simulator.last_iterations;
2175+
writeln!(out, "Iterations: {iters}").ok();
21962176
writeln!(out, "\nGates:").ok();
21972177
for (id, g) in &self.db.gates {
21982178
writeln!(out, " {}", g.display(&self.db)).ok();

src/simulator.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ impl Value {
7676
}
7777
}
7878

79+
#[derive(Default)]
7980
pub struct Simulator {
8081
/// Final result - maps each pin to its current value
8182
pub current: HashMap<Pin, Value>,
@@ -89,12 +90,7 @@ pub struct Simulator {
8990

9091
impl Simulator {
9192
pub(crate) fn new() -> Self {
92-
Self {
93-
current: Default::default(),
94-
last_iterations: 0,
95-
status: SimulationStatus::Running,
96-
current_iteration: 0,
97-
}
93+
Self::default()
9894
}
9995

10096
fn rebuild_sorted_instances(&self, db: &DB) -> Vec<InstanceId> {

0 commit comments

Comments
 (0)