Keep results stable when using file-finder while scanning files

This commit is contained in:
Max Brunsfeld 2021-04-27 14:02:55 -07:00
parent 6882fdca38
commit a59b75c839
3 changed files with 125 additions and 7 deletions

View file

@ -36,13 +36,17 @@ impl Eq for PathMatch {}
impl PartialOrd for PathMatch {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.score.partial_cmp(&other.score)
Some(self.cmp(other))
}
}
impl Ord for PathMatch {
fn cmp(&self, other: &Self) -> Ordering {
self.partial_cmp(other).unwrap_or(Ordering::Equal)
self.score
.partial_cmp(&other.score)
.unwrap_or(Ordering::Equal)
.then_with(|| self.tree_id.cmp(&other.tree_id))
.then_with(|| Arc::as_ptr(&self.path).cmp(&Arc::as_ptr(&other.path)))
}
}
@ -150,7 +154,7 @@ where
.flatten()
.map(|r| r.0)
.collect::<Vec<_>>();
results.sort_unstable_by(|a, b| b.score.partial_cmp(&a.score).unwrap());
results.sort_unstable_by(|a, b| b.cmp(&a));
results.truncate(max_results);
results
}