fixes to stream::min_by (#162)

* fixes to stream::min_by

* no reason to split these impls

* remove Debug derive from MinByFuture
This commit is contained in:
Fedor Sakharov 2019-09-10 14:53:30 +03:00 committed by Yoshua Wuyts
parent a8090be3eb
commit 272f74c1da
2 changed files with 17 additions and 17 deletions

View file

@ -2,20 +2,20 @@ use std::cmp::Ordering;
use std::pin::Pin; use std::pin::Pin;
use crate::future::Future; use crate::future::Future;
use crate::stream::Stream;
use crate::task::{Context, Poll}; use crate::task::{Context, Poll};
/// A future that yields the minimum item in a stream by a given comparison function. #[allow(missing_debug_implementations)]
#[derive(Clone, Debug)] pub struct MinByFuture<S, F, T> {
pub struct MinByFuture<S: Stream, F> {
stream: S, stream: S,
compare: F, compare: F,
min: Option<S::Item>, min: Option<T>,
} }
impl<S: Stream + Unpin, F> Unpin for MinByFuture<S, F> {} impl<S, F, T> MinByFuture<S, F, T> {
pin_utils::unsafe_pinned!(stream: S);
pin_utils::unsafe_unpinned!(compare: F);
pin_utils::unsafe_unpinned!(min: Option<T>);
impl<S: Stream + Unpin, F> MinByFuture<S, F> {
pub(super) fn new(stream: S, compare: F) -> Self { pub(super) fn new(stream: S, compare: F) -> Self {
MinByFuture { MinByFuture {
stream, stream,
@ -25,25 +25,25 @@ impl<S: Stream + Unpin, F> MinByFuture<S, F> {
} }
} }
impl<S, F> Future for MinByFuture<S, F> impl<S, F> Future for MinByFuture<S, F, S::Item>
where where
S: futures_core::stream::Stream + Unpin, S: futures_core::stream::Stream + Unpin + Sized,
S::Item: Copy, S::Item: Copy,
F: FnMut(&S::Item, &S::Item) -> Ordering, F: FnMut(&S::Item, &S::Item) -> Ordering,
{ {
type Output = Option<S::Item>; type Output = Option<S::Item>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let next = futures_core::ready!(Pin::new(&mut self.stream).poll_next(cx)); let next = futures_core::ready!(self.as_mut().stream().poll_next(cx));
match next { match next {
Some(new) => { Some(new) => {
cx.waker().wake_by_ref(); cx.waker().wake_by_ref();
match self.as_mut().min.take() { match self.as_mut().min().take() {
None => self.as_mut().min = Some(new), None => *self.as_mut().min() = Some(new),
Some(old) => match (&mut self.as_mut().compare)(&new, &old) { Some(old) => match (&mut self.as_mut().compare())(&new, &old) {
Ordering::Less => self.as_mut().min = Some(new), Ordering::Less => *self.as_mut().min() = Some(new),
_ => self.as_mut().min = Some(old), _ => *self.as_mut().min() = Some(old),
}, },
} }
Poll::Pending Poll::Pending

View file

@ -153,9 +153,9 @@ pub trait Stream {
/// # /// #
/// # }) } /// # }) }
/// ``` /// ```
fn min_by<F>(self, compare: F) -> MinByFuture<Self, F> fn min_by<F>(self, compare: F) -> MinByFuture<Self, F, Self::Item>
where where
Self: Sized + Unpin, Self: Sized,
F: FnMut(&Self::Item, &Self::Item) -> Ordering, F: FnMut(&Self::Item, &Self::Item) -> Ordering,
{ {
MinByFuture::new(self, compare) MinByFuture::new(self, compare)