Use let-else to return early and reduce indentation

This commit is contained in:
Cecylia Bocovich 2024-03-09 16:25:41 -05:00
parent bbfdd08ca1
commit 203b949d43
No known key found for this signature in database
GPG Key ID: 009DE379FD9B7B90
1 changed files with 101 additions and 103 deletions

View File

@ -519,111 +519,109 @@ impl BridgeAuth {
) -> ReplaceSuccess { ) -> ReplaceSuccess {
let mut res = ReplaceSuccess::NotFound; let mut res = ReplaceSuccess::NotFound;
let reachable_bridges = &self.bridge_table.reachable.clone(); let reachable_bridges = &self.bridge_table.reachable.clone();
match reachable_bridges.get(bridge) { let Some(positions) = reachable_bridges.get(bridge) else {
Some(positions) => { return res;
if let Some(replacement) = available_bridge { };
for (bucketnum, offset) in positions.iter() { if let Some(replacement) = available_bridge {
let mut bridgelines = match self.bridge_table.buckets.get(bucketnum) { for (bucketnum, offset) in positions.iter() {
Some(bridgelines) => *bridgelines, let mut bridgelines = match self.bridge_table.buckets.get(bucketnum) {
None => return ReplaceSuccess::NotFound, Some(bridgelines) => *bridgelines,
}; None => return ReplaceSuccess::NotFound,
assert!(bridgelines[*offset] == *bridge); };
bridgelines[*offset] = *replacement; assert!(bridgelines[*offset] == *bridge);
self.bridge_table.buckets.insert(*bucketnum, bridgelines); bridgelines[*offset] = *replacement;
// Remove the bridge from the reachable bridges and add new bridge self.bridge_table.buckets.insert(*bucketnum, bridgelines);
self.bridge_table // Remove the bridge from the reachable bridges and add new bridge
.reachable self.bridge_table
.insert(*replacement, positions.clone()); .reachable
// Remove the bridge from the bucket .insert(*replacement, positions.clone());
self.bridge_table.reachable.remove(bridge); // Remove the bridge from the bucket
} self.bridge_table.reachable.remove(bridge);
res = ReplaceSuccess::Replaced }
} else if !self.bridge_table.unallocated_bridges.is_empty() { res = ReplaceSuccess::Replaced
let replacement = &self.bridge_table.unallocated_bridges.pop().unwrap(); } else if !self.bridge_table.unallocated_bridges.is_empty() {
for (bucketnum, offset) in positions.iter() { let replacement = &self.bridge_table.unallocated_bridges.pop().unwrap();
let mut bridgelines = match self.bridge_table.buckets.get(bucketnum) { for (bucketnum, offset) in positions.iter() {
Some(bridgelines) => *bridgelines, let mut bridgelines = match self.bridge_table.buckets.get(bucketnum) {
// This should not happen if the rest of the function is correct, we can assume unwrap will succeed Some(bridgelines) => *bridgelines,
None => return ReplaceSuccess::NotReplaced, // This should not happen if the rest of the function is correct, we can assume unwrap will succeed
}; None => return ReplaceSuccess::NotReplaced,
assert!(bridgelines[*offset] == *bridge); };
bridgelines[*offset] = *replacement; assert!(bridgelines[*offset] == *bridge);
self.bridge_table.buckets.insert(*bucketnum, bridgelines); bridgelines[*offset] = *replacement;
self.bridge_table self.bridge_table.buckets.insert(*bucketnum, bridgelines);
.reachable self.bridge_table
.insert(*replacement, positions.clone()); .reachable
// Remove the bridge from the bucket .insert(*replacement, positions.clone());
self.bridge_table.reachable.remove(bridge); // Remove the bridge from the bucket
} self.bridge_table.reachable.remove(bridge);
res = ReplaceSuccess::Replaced }
} else if !self.bridge_table.spares.is_empty() { res = ReplaceSuccess::Replaced
// First get the bucketnums for the replacement bridge in case it is a spare } else if !self.bridge_table.spares.is_empty() {
let mut bucketnums: Vec<u32> = Vec::new(); // First get the bucketnums for the replacement bridge in case it is a spare
for (bucketnum, _) in positions.iter() { let mut bucketnums: Vec<u32> = Vec::new();
bucketnums.push(*bucketnum); for (bucketnum, _) in positions.iter() {
} bucketnums.push(*bucketnum);
// Get the first spare and remove it from the spares set. }
let mut spare = *self.bridge_table.spares.iter().next().unwrap(); // Get the first spare and remove it from the spares set.
// Check that the first spare in the list of spares is not the one to be replaced let mut spare = *self.bridge_table.spares.iter().next().unwrap();
if bucketnums.contains(&spare) { // Check that the first spare in the list of spares is not the one to be replaced
// If it is, take the last spare instead if bucketnums.contains(&spare) {
spare = *self.bridge_table.spares.iter().last().unwrap(); // If it is, take the last spare instead
// If this is the same bucketnum, there is only one spare bucket with the bridge spare = *self.bridge_table.spares.iter().last().unwrap();
// to be replaced in it, so don't replace it. // If this is the same bucketnum, there is only one spare bucket with the bridge
if bucketnums.contains(&spare) { // to be replaced in it, so don't replace it.
res = ReplaceSuccess::NotReplaced; if bucketnums.contains(&spare) {
return res; res = ReplaceSuccess::NotReplaced;
} return res;
}
self.bridge_table.spares.remove(&spare);
self.bridge_table.recycleable_keys.push(spare);
// Get the actual bridges from the spare bucket
let spare_bucket = match self.bridge_table.buckets.remove(&spare) {
Some(spare_bucket) => spare_bucket,
// This should not happen if the rest of the functions are correct, we can assume unwrap will succeed
None => return ReplaceSuccess::NotReplaced,
};
// Remove the spare bucket uid from the keys map
self.bridge_table.keys.remove(&spare);
let mut replacement: &BridgeLine = &BridgeLine::default();
// Make the first spare the replacement bridge, add the others to the set of
// unallocated_bridges
for spare_bridge in spare_bucket.iter() {
if replacement.port > 0 {
self.bridge_table.unallocated_bridges.push(*spare_bridge);
// Mark bucket as unreachable while it is unallocated
self.bridge_table.reachable.remove(spare_bridge);
} else {
replacement = spare_bridge;
}
}
for (bucketnum, offset) in positions.iter() {
let mut bridgelines = match self.bridge_table.buckets.get(bucketnum) {
Some(bridgelines) => *bridgelines,
None => return ReplaceSuccess::NotReplaced,
};
assert!(bridgelines[*offset] == *bridge);
bridgelines[*offset] = *replacement;
self.bridge_table.buckets.insert(*bucketnum, bridgelines);
self.bridge_table
.reachable
.insert(*replacement, positions.clone());
// Remove the bridge from the bucket
self.bridge_table.reachable.remove(bridge);
}
res = ReplaceSuccess::Replaced
} else {
println!("No available bridges");
// If there are no available bridges that can be assigned here, the only thing
// that can be done is return an indication that updating the gone bridge
// didn't work.
// In this case, we do not mark the bridge as unreachable or remove it from the
// reachable bridges so that we can still find it when a new bridge does become available
res = ReplaceSuccess::NotReplaced
} }
} }
None => return res, self.bridge_table.spares.remove(&spare);
}; self.bridge_table.recycleable_keys.push(spare);
// Get the actual bridges from the spare bucket
let spare_bucket = match self.bridge_table.buckets.remove(&spare) {
Some(spare_bucket) => spare_bucket,
// This should not happen if the rest of the functions are correct, we can assume unwrap will succeed
None => return ReplaceSuccess::NotReplaced,
};
// Remove the spare bucket uid from the keys map
self.bridge_table.keys.remove(&spare);
let mut replacement: &BridgeLine = &BridgeLine::default();
// Make the first spare the replacement bridge, add the others to the set of
// unallocated_bridges
for spare_bridge in spare_bucket.iter() {
if replacement.port > 0 {
self.bridge_table.unallocated_bridges.push(*spare_bridge);
// Mark bucket as unreachable while it is unallocated
self.bridge_table.reachable.remove(spare_bridge);
} else {
replacement = spare_bridge;
}
}
for (bucketnum, offset) in positions.iter() {
let mut bridgelines = match self.bridge_table.buckets.get(bucketnum) {
Some(bridgelines) => *bridgelines,
None => return ReplaceSuccess::NotReplaced,
};
assert!(bridgelines[*offset] == *bridge);
bridgelines[*offset] = *replacement;
self.bridge_table.buckets.insert(*bucketnum, bridgelines);
self.bridge_table
.reachable
.insert(*replacement, positions.clone());
// Remove the bridge from the bucket
self.bridge_table.reachable.remove(bridge);
}
res = ReplaceSuccess::Replaced
} else {
println!("No available bridges");
// If there are no available bridges that can be assigned here, the only thing
// that can be done is return an indication that updating the gone bridge
// didn't work.
// In this case, we do not mark the bridge as unreachable or remove it from the
// reachable bridges so that we can still find it when a new bridge does become available
res = ReplaceSuccess::NotReplaced
}
res res
} }