Last active
April 6, 2021 23:45
-
-
Save Lucretiel/cfb45b2254d12982b18b9bcac645098d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait PrioritySearcher<'a, T> { | |
fn test(&mut self, item: &'a T) -> bool; | |
fn next_best(self) -> Option<&'a T>; | |
} | |
impl<'a, F, T> PrioritySearcher<'a, T> for F | |
where F: FnMut(&'a T) -> bool | |
{ | |
fn test(&mut self, item: &'a T) -> bool { | |
(self)(item) | |
} | |
fn next_best(self) -> Option<&'a T> { | |
None | |
} | |
} | |
struct OrElseSearcher<'a, T, A, B> { | |
primary: A, | |
secondary: B, | |
secondary_best: Option<&'a T>, | |
} | |
impl<'a, T, A, B> PrioritySearcher<'a, T> for OrElseSearcher<'a, T, A, B> | |
where | |
A: PrioritySearcher<'a, T>, | |
B: PrioritySearcher<'a, T>, | |
{ | |
fn test(&mut self, item: &'a T) -> bool { | |
if self.primary.test(item) { | |
return true; | |
} | |
if self.secondary_best.is_none() && self.secondary.test(item) { | |
self.secondary_best = Some(item); | |
} | |
false | |
} | |
fn next_best(self) -> Option<&'a T> { | |
self.primary | |
.next_best() | |
.or(self.secondary_best) | |
.or_else(move || self.secondary.next_best()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment