Skip to content

Commit

Permalink
Make fn best_backup generic, and fix a bug in it
Browse files Browse the repository at this point in the history
  • Loading branch information
DRiKE committed Jun 3, 2024
1 parent 84a9cd3 commit a841344
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions src/bgp/path_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,9 +498,9 @@ pub fn best_backup_vec<'a, OS: OrdStrat>(
(best, backup)
}

pub fn best_backup<'a, OS: OrdStrat>(
it: impl Iterator<Item = OrdRoute<'a, OS>>
) -> (Option<OrdRoute<'a, OS>>, Option<OrdRoute<'a, OS>>)
pub fn best_backup<T: Ord>(
it: impl Iterator<Item = T>
) -> (Option<T>, Option<T>)
{
let mut best = None;
let mut backup = None;
Expand All @@ -519,13 +519,17 @@ pub fn best_backup<'a, OS: OrdStrat>(
best = Some(cur_best);

// c is not better than best, now check backup
if let Some(bkup) = backup.take() {
if c < bkup {
// c is preferred over backup
backup = Some(c);
continue;
match backup.take() {
None => { backup = Some(c); }
Some(cur_backup) => {
if c < cur_backup {
// c is preferred over backup
backup = Some(c);
} else {
backup = Some(cur_backup);
}

}
backup = Some(bkup);
}
}
}
Expand Down Expand Up @@ -617,7 +621,7 @@ mod tests {


#[test]
fn helpers() {
fn helpers_ordroute() {

let tiebreakers = TiebreakerInfo {
source: RouteSource::Ebgp,
Expand Down Expand Up @@ -657,8 +661,17 @@ mod tests {
assert_ne!(best2, backup2);
assert_ne!(best3, backup3);

dbg!(&best1);
dbg!(&backup2);
//dbg!(&best1);
//dbg!(&backup2);
}

#[test]
fn helpers_generic() {
let (a,b,c) = ("2".to_string(), "1".to_string(), "3".to_string());
//let (a,b,c) = (2, 1, 3);
let values = vec![&a, &b, &c];
let (best, backup) = best_backup(values.into_iter());
assert_eq!(best, Some(&b));
assert_eq!(backup, Some(&a));
}
}

0 comments on commit a841344

Please sign in to comment.