From debb969cf5b591598578c29bd29903e13910df93 Mon Sep 17 00:00:00 2001 From: Fedor Sakharov Date: Sun, 8 Sep 2019 17:49:09 +0300 Subject: [PATCH 1/3] fixes to stream::min_by --- src/stream/stream/min_by.rs | 31 +++++++++++++++++-------------- src/stream/stream/mod.rs | 4 ++-- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/stream/stream/min_by.rs b/src/stream/stream/min_by.rs index b65d88db3..5b0a66e92 100644 --- a/src/stream/stream/min_by.rs +++ b/src/stream/stream/min_by.rs @@ -2,20 +2,23 @@ use std::cmp::Ordering; use std::pin::Pin; use crate::future::Future; -use crate::stream::Stream; use crate::task::{Context, Poll}; /// A future that yields the minimum item in a stream by a given comparison function. -#[derive(Clone, Debug)] -pub struct MinByFuture { +#[derive(Debug)] +pub struct MinByFuture { stream: S, compare: F, - min: Option, + min: Option, } -impl Unpin for MinByFuture {} +impl MinByFuture { + pin_utils::unsafe_pinned!(stream: S); + pin_utils::unsafe_unpinned!(compare: F); + pin_utils::unsafe_unpinned!(min: Option); +} -impl MinByFuture { +impl MinByFuture { pub(super) fn new(stream: S, compare: F) -> Self { MinByFuture { stream, @@ -25,25 +28,25 @@ impl MinByFuture { } } -impl Future for MinByFuture +impl Future for MinByFuture where - S: futures_core::stream::Stream + Unpin, + S: futures_core::stream::Stream + Unpin + Sized, S::Item: Copy, F: FnMut(&S::Item, &S::Item) -> Ordering, { type Output = Option; fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - 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 { Some(new) => { cx.waker().wake_by_ref(); - match self.as_mut().min.take() { - None => self.as_mut().min = Some(new), - Some(old) => match (&mut self.as_mut().compare)(&new, &old) { - Ordering::Less => self.as_mut().min = Some(new), - _ => self.as_mut().min = Some(old), + match self.as_mut().min().take() { + None => *self.as_mut().min() = Some(new), + Some(old) => match (&mut self.as_mut().compare())(&new, &old) { + Ordering::Less => *self.as_mut().min() = Some(new), + _ => *self.as_mut().min() = Some(old), }, } Poll::Pending diff --git a/src/stream/stream/mod.rs b/src/stream/stream/mod.rs index 91b111e2e..4e1e4168a 100644 --- a/src/stream/stream/mod.rs +++ b/src/stream/stream/mod.rs @@ -153,9 +153,9 @@ pub trait Stream { /// # /// # }) } /// ``` - fn min_by(self, compare: F) -> MinByFuture + fn min_by(self, compare: F) -> MinByFuture where - Self: Sized + Unpin, + Self: Sized, F: FnMut(&Self::Item, &Self::Item) -> Ordering, { MinByFuture::new(self, compare) From 14eda7cea9a91b306fa6ce100fad3004b2f4c2f3 Mon Sep 17 00:00:00 2001 From: Fedor Sakharov Date: Sun, 8 Sep 2019 18:11:02 +0300 Subject: [PATCH 2/3] no reason to split these impls --- src/stream/stream/min_by.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/stream/stream/min_by.rs b/src/stream/stream/min_by.rs index 5b0a66e92..db81c670f 100644 --- a/src/stream/stream/min_by.rs +++ b/src/stream/stream/min_by.rs @@ -16,9 +16,7 @@ impl MinByFuture { pin_utils::unsafe_pinned!(stream: S); pin_utils::unsafe_unpinned!(compare: F); pin_utils::unsafe_unpinned!(min: Option); -} -impl MinByFuture { pub(super) fn new(stream: S, compare: F) -> Self { MinByFuture { stream, From 1b5eea303bdfc648d620b9e6230ea636b09aadfe Mon Sep 17 00:00:00 2001 From: Fedor Sakharov Date: Mon, 9 Sep 2019 12:45:37 +0300 Subject: [PATCH 3/3] remove Debug derive from MinByFuture --- src/stream/stream/min_by.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/stream/stream/min_by.rs b/src/stream/stream/min_by.rs index db81c670f..f63d52b87 100644 --- a/src/stream/stream/min_by.rs +++ b/src/stream/stream/min_by.rs @@ -4,8 +4,7 @@ use std::pin::Pin; use crate::future::Future; use crate::task::{Context, Poll}; -/// A future that yields the minimum item in a stream by a given comparison function. -#[derive(Debug)] +#[allow(missing_debug_implementations)] pub struct MinByFuture { stream: S, compare: F,