From 4ecac45d67d243331836877abc62a3a10f1791c6 Mon Sep 17 00:00:00 2001 From: Katherine Kiefer Date: Sat, 2 Dec 2023 00:01:04 +1100 Subject: [PATCH] fix stats, convert some things to iter --- crates/auxcallback/src/lib.rs | 9 +++++---- src/gas/mixture.rs | 33 +++++++++++++++------------------ src/turfs/processing.rs | 24 +++++++++++------------- 3 files changed, 31 insertions(+), 35 deletions(-) diff --git a/crates/auxcallback/src/lib.rs b/crates/auxcallback/src/lib.rs index 49abd743..eccf750b 100644 --- a/crates/auxcallback/src/lib.rs +++ b/crates/auxcallback/src/lib.rs @@ -39,16 +39,17 @@ pub fn byond_callback_sender() -> flume::Sender { fn process_callbacks() { //let stack_trace = Proc::find("/proc/auxtools_stack_trace").unwrap(); with_callback_receiver(|receiver| { - for callback in receiver.try_iter() { - if let Err(e) = callback() { + receiver + .try_iter() + .filter_map(|cb| cb().err()) + .for_each(|e| { let error_string = format!("{e:?}").try_into().unwrap(); byondapi::global_call::call_global_id( byond_string!("stack_trace"), &[error_string], ) .unwrap(); - } - } + }) }) } diff --git a/src/gas/mixture.rs b/src/gas/mixture.rs index e2afe8c4..ce07ffd8 100644 --- a/src/gas/mixture.rs +++ b/src/gas/mixture.rs @@ -140,9 +140,7 @@ impl Mixture { /// # Errors /// If the closure errors. pub fn for_each_gas(&self, mut f: impl FnMut(GasIDX, f32) -> Result<()>) -> Result<()> { - for (i, g) in self.enumerate() { - f(i, g)?; - } + self.enumerate().try_for_each(|(i, g)| f(i, g))?; Ok(()) } /// As `for_each_gas`, but with mut refs to the mole counts instead of copies. @@ -152,9 +150,10 @@ impl Mixture { &mut self, mut f: impl FnMut(GasIDX, &mut f32) -> Result<()>, ) -> Result<()> { - for (i, g) in self.moles.iter_mut().enumerate() { - f(i, g)?; - } + self.moles + .iter_mut() + .enumerate() + .try_for_each(|(i, g)| f(i, g))?; Ok(()) } /// Returns (by value) the amount of moles of a given index the mix has. M @@ -271,9 +270,10 @@ impl Mixture { let our_heat_capacity = self.heat_capacity(); let other_heat_capacity = giver.heat_capacity(); self.maybe_expand(giver.moles.len()); - for (a, b) in self.moles.iter_mut().zip(giver.moles.iter()) { - *a += b; - } + self.moles + .iter_mut() + .zip(giver.moles.iter()) + .for_each(|(a, b)| *a += b); let combined_heat_capacity = our_heat_capacity + other_heat_capacity; if combined_heat_capacity > MINIMUM_HEAT_CAPACITY { self.set_temperature( @@ -293,9 +293,10 @@ impl Mixture { let our_heat_capacity = self.heat_capacity(); let other_heat_capacity = giver.heat_capacity() * ratio; self.maybe_expand(giver.moles.len()); - for (a, b) in self.moles.iter_mut().zip(giver.moles.iter()) { - *a += b * ratio; - } + self.moles + .iter_mut() + .zip(giver.moles.iter()) + .for_each(|(a, b)| *a += b * ratio); let combined_heat_capacity = our_heat_capacity + other_heat_capacity; if combined_heat_capacity > MINIMUM_HEAT_CAPACITY { self.set_temperature( @@ -465,18 +466,14 @@ impl Mixture { /// Multiplies every gas molage with this value. pub fn multiply(&mut self, multiplier: f32) { if !self.immutable { - for amt in self.moles.iter_mut() { - *amt *= multiplier; - } + self.moles.iter_mut().for_each(|amt| *amt *= multiplier); self.cached_heat_capacity.invalidate(); self.garbage_collect(); } } pub fn add(&mut self, num: f32) { if !self.immutable { - for amt in self.moles.iter_mut() { - *amt += num; - } + self.moles.iter_mut().for_each(|amt| *amt += num); self.cached_heat_capacity.invalidate(); self.garbage_collect(); } diff --git a/src/turfs/processing.rs b/src/turfs/processing.rs index 2a8a19ab..1d39a09b 100644 --- a/src/turfs/processing.rs +++ b/src/turfs/processing.rs @@ -41,7 +41,7 @@ fn process_turf( remaining: Duration, fdm_max_steps: i32, equalize_enabled: bool, - ssair: ByondValue, + mut ssair: ByondValue, ) -> Result<()> { //this will block until process_turfs is called let (low_pressure_turfs, _high_pressure_turfs) = { @@ -51,15 +51,12 @@ fn process_turf( let bench = start_time.elapsed().as_millis(); let (lpt, hpt) = (low_pressure_turfs.len(), high_pressure_turfs.len()); let prev_cost = ssair.read_number_id(byond_string!("cost_turfs"))?; - ssair - .read_var_id(byond_string!("cost_turfs"))? - .set_number(0.8 * prev_cost + 0.2 * (bench as f32)); - ssair - .read_var_id(byond_string!("low_pressure_turfs"))? - .set_number(lpt as f32); - ssair - .read_var_id(byond_string!("high_pressure_turfs"))? - .set_number(hpt as f32); + ssair.write_var_id( + byond_string!("cost_turfs"), + &(0.8 * prev_cost + 0.2 * (bench as f32)).into(), + )?; + ssair.write_var_id(byond_string!("low_pressure_turfs"), &(lpt as f32).into())?; + ssair.write_var_id(byond_string!("high_pressure_turfs"), &(hpt as f32).into())?; (low_pressure_turfs, high_pressure_turfs) }; { @@ -67,9 +64,10 @@ fn process_turf( post_process(); let bench = start_time.elapsed().as_millis(); let prev_cost = ssair.read_number_id(byond_string!("cost_post_process"))?; - ssair - .read_var_id(byond_string!("cost_post_process"))? - .set_number(0.8 * prev_cost + 0.2 * (bench as f32)); + ssair.write_var_id( + byond_string!("cost_post_process"), + &(0.8 * prev_cost + 0.2 * (bench as f32)).into(), + )?; } { planet_process();