|
|
@ -1,6 +1,6 @@
|
|
|
|
use std::cmp::Ordering;
|
|
|
|
use std::cmp::Ordering;
|
|
|
|
use std::pin::Pin;
|
|
|
|
|
|
|
|
use std::future::Future;
|
|
|
|
use std::future::Future;
|
|
|
|
|
|
|
|
use std::pin::Pin;
|
|
|
|
|
|
|
|
|
|
|
|
use pin_project_lite::pin_project;
|
|
|
|
use pin_project_lite::pin_project;
|
|
|
|
|
|
|
|
|
|
|
@ -13,7 +13,7 @@ pin_project! {
|
|
|
|
pub struct MaxByKeyFuture<S, T, K> {
|
|
|
|
pub struct MaxByKeyFuture<S, T, K> {
|
|
|
|
#[pin]
|
|
|
|
#[pin]
|
|
|
|
stream: S,
|
|
|
|
stream: S,
|
|
|
|
max: Option<T>,
|
|
|
|
max: Option<(T, T)>,
|
|
|
|
key_by: K,
|
|
|
|
key_by: K,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -37,24 +37,29 @@ where
|
|
|
|
type Output = Option<S::Item>;
|
|
|
|
type Output = Option<S::Item>;
|
|
|
|
|
|
|
|
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
|
|
|
|
fn key<B, T>(mut f: impl FnMut(&T) -> B) -> impl FnMut(T) -> (B, T) {
|
|
|
|
|
|
|
|
move |x| (f(&x), x)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let this = self.project();
|
|
|
|
let this = self.project();
|
|
|
|
let next = futures_core::ready!(this.stream.poll_next(cx));
|
|
|
|
let next = futures_core::ready!(this.stream.poll_next(cx));
|
|
|
|
|
|
|
|
|
|
|
|
match next {
|
|
|
|
match next {
|
|
|
|
Some(new) => {
|
|
|
|
Some(new) => {
|
|
|
|
let new = (this.key_by)(&new);
|
|
|
|
let (key, value) = key(this.key_by)(new);
|
|
|
|
cx.waker().wake_by_ref();
|
|
|
|
cx.waker().wake_by_ref();
|
|
|
|
|
|
|
|
|
|
|
|
match this.max.take() {
|
|
|
|
match this.max.take() {
|
|
|
|
None => *this.max = Some(new),
|
|
|
|
None => *this.max = Some((key, value)),
|
|
|
|
|
|
|
|
|
|
|
|
Some(old) => match new.cmp(&old) {
|
|
|
|
Some(old) => match key.cmp(&old.0) {
|
|
|
|
Ordering::Greater => *this.max = Some(new),
|
|
|
|
Ordering::Greater => *this.max = Some((key, value)),
|
|
|
|
_ => *this.max = Some(old),
|
|
|
|
_ => *this.max = Some(old),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Poll::Pending
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => Poll::Ready(this.max.take()),
|
|
|
|
None => Poll::Ready(this.max.take().map(|max| max.1)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|