From 6462027b7fce5a315ddf871135e81f09f510be70 Mon Sep 17 00:00:00 2001 From: Cecylia Bocovich Date: Sat, 9 Mar 2024 17:30:21 -0500 Subject: [PATCH] Simplify selection of spare replacement bucket --- crates/lox-library/src/lib.rs | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/crates/lox-library/src/lib.rs b/crates/lox-library/src/lib.rs index 860c98b..35478b4 100644 --- a/crates/lox-library/src/lib.rs +++ b/crates/lox-library/src/lib.rs @@ -564,18 +564,15 @@ impl BridgeAuth { bucketnums.push(*bucketnum); } // Get the first spare and remove it from the spares set. - let mut spare = *self.bridge_table.spares.iter().next().unwrap(); - // Check that the first spare in the list of spares is not the one to be replaced - if bucketnums.contains(&spare) { - // If it is, take the last spare instead - spare = *self.bridge_table.spares.iter().last().unwrap(); - // If this is the same bucketnum, there is only one spare bucket with the bridge - // to be replaced in it, so don't replace it. - if bucketnums.contains(&spare) { - res = ReplaceSuccess::NotReplaced; - return res; - } - } + let Some(spare) = self + .bridge_table + .spares + .iter() + .find(|x| !bucketnums.contains(x)) + .cloned() + else { + return ReplaceSuccess::NotReplaced; + }; self.bridge_table.spares.remove(&spare); self.bridge_table.recycleable_keys.push(spare); // Get the actual bridges from the spare bucket