From 314a75da288367f92481f20424e728fe177e8fed Mon Sep 17 00:00:00 2001 From: k-nasa Date: Wed, 20 Nov 2019 00:00:54 +0900 Subject: [PATCH 1/6] fix typo --- src/stream/stream/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/stream/stream/mod.rs b/src/stream/stream/mod.rs index f876576..c61da55 100644 --- a/src/stream/stream/mod.rs +++ b/src/stream/stream/mod.rs @@ -916,7 +916,7 @@ extension_trait! { let max = s.clone().max_by_key(|x| x.abs()).await; assert_eq!(max, Some(3)); - let max = stream::empty::().min_by_key(|x| x.abs()).await; + let max = stream::empty::().max_by_key(|x| x.abs()).await; assert_eq!(max, None); # # }) } From 64b2e10b930caf1f5f047b81060f415b570b3768 Mon Sep 17 00:00:00 2001 From: k-nasa Date: Wed, 20 Nov 2019 00:17:29 +0900 Subject: [PATCH 2/6] fix max_by_key mistake --- src/stream/stream/max_by_key.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/stream/stream/max_by_key.rs b/src/stream/stream/max_by_key.rs index b5bc7e0..5ebc25d 100644 --- a/src/stream/stream/max_by_key.rs +++ b/src/stream/stream/max_by_key.rs @@ -1,6 +1,6 @@ use std::cmp::Ordering; -use std::pin::Pin; use std::future::Future; +use std::pin::Pin; use pin_project_lite::pin_project; @@ -13,7 +13,7 @@ pin_project! { pub struct MaxByKeyFuture { #[pin] stream: S, - max: Option, + max: Option<(T, T)>, key_by: K, } } @@ -37,24 +37,32 @@ where type Output = Option; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + fn key(mut f: impl FnMut(&T) -> B) -> impl FnMut(T) -> (B, T) { + move |x| (f(&x), x) + } + let this = self.project(); let next = futures_core::ready!(this.stream.poll_next(cx)); match next { Some(new) => { - let new = (this.key_by)(&new); + let (key, value) = key(this.key_by)(new); cx.waker().wake_by_ref(); + match this.max.take() { - None => *this.max = Some(new), + None => *this.max = Some((key, value)), - Some(old) => match new.cmp(&old) { - Ordering::Greater => *this.max = Some(new), + Some(old) => match key.cmp(&old.0) { + Ordering::Greater => *this.max = Some((key, value)), _ => *this.max = Some(old), }, } Poll::Pending } - None => Poll::Ready(this.max.take()), + None => Poll::Ready(match this.max.take() { + None => None, + Some(max) => Some(max.1), + }), } } } From 667bbc1019783d0af0d8424a31c59b178c503eab Mon Sep 17 00:00:00 2001 From: k-nasa Date: Wed, 20 Nov 2019 00:18:11 +0900 Subject: [PATCH 3/6] doc: update doc test --- src/stream/stream/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/stream/stream/mod.rs b/src/stream/stream/mod.rs index c61da55..ca386e0 100644 --- a/src/stream/stream/mod.rs +++ b/src/stream/stream/mod.rs @@ -911,10 +911,10 @@ extension_trait! { use async_std::prelude::*; use async_std::stream; - let s = stream::from_iter(vec![-1isize, -2, -3]); + let s = stream::from_iter(vec![-3_i32, 0, 1, 5, -10]); let max = s.clone().max_by_key(|x| x.abs()).await; - assert_eq!(max, Some(3)); + assert_eq!(max, Some(-10)); let max = stream::empty::().max_by_key(|x| x.abs()).await; assert_eq!(max, None); From ca71ad073bba886f84aaf0fa14e3801f54b203f9 Mon Sep 17 00:00:00 2001 From: k-nasa Date: Wed, 20 Nov 2019 00:25:35 +0900 Subject: [PATCH 4/6] fix stream min_by_key mistake --- src/stream/stream/min_by_key.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/stream/stream/min_by_key.rs b/src/stream/stream/min_by_key.rs index 8179fb3..3cd0014 100644 --- a/src/stream/stream/min_by_key.rs +++ b/src/stream/stream/min_by_key.rs @@ -1,6 +1,6 @@ use std::cmp::Ordering; -use std::pin::Pin; use std::future::Future; +use std::pin::Pin; use pin_project_lite::pin_project; @@ -13,7 +13,7 @@ pin_project! { pub struct MinByKeyFuture { #[pin] stream: S, - min: Option, + min: Option<(T, T)>, key_by: K, } } @@ -37,24 +37,32 @@ where type Output = Option; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + fn key(mut f: impl FnMut(&T) -> B) -> impl FnMut(T) -> (B, T) { + move |x| (f(&x), x) + } + let this = self.project(); let next = futures_core::ready!(this.stream.poll_next(cx)); match next { Some(new) => { - let new = (this.key_by)(&new); + let (key, value) = key(this.key_by)(new); cx.waker().wake_by_ref(); + match this.min.take() { - None => *this.min = Some(new), + None => *this.min = Some((key, value)), - Some(old) => match new.cmp(&old) { - Ordering::Less => *this.min = Some(new), + Some(old) => match key.cmp(&old.0) { + Ordering::Less => *this.min = Some((key, value)), _ => *this.min = Some(old), }, } Poll::Pending } - None => Poll::Ready(this.min.take()), + None => Poll::Ready(match this.min.take() { + None => None, + Some(max) => Some(max.1), + }), } } } From 080875edc9effe5387b37b99d56b92302599c9ba Mon Sep 17 00:00:00 2001 From: k-nasa Date: Wed, 20 Nov 2019 00:25:48 +0900 Subject: [PATCH 5/6] update min_by_key doc --- src/stream/stream/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/stream/stream/mod.rs b/src/stream/stream/mod.rs index ca386e0..48d865e 100644 --- a/src/stream/stream/mod.rs +++ b/src/stream/stream/mod.rs @@ -875,10 +875,10 @@ extension_trait! { use async_std::prelude::*; use async_std::stream; - let s = stream::from_iter(vec![1isize, 2, -3]); + let s = stream::from_iter(vec![-1isize, 2, -3]); let min = s.clone().min_by_key(|x| x.abs()).await; - assert_eq!(min, Some(1)); + assert_eq!(min, Some(-1)); let min = stream::empty::().min_by_key(|x| x.abs()).await; assert_eq!(min, None); From b5e66c4f93d699e986ab2681e94b6b79170cec4c Mon Sep 17 00:00:00 2001 From: k-nasa Date: Wed, 20 Nov 2019 00:38:51 +0900 Subject: [PATCH 6/6] refactor: Refactoring option type handling --- src/stream/stream/max_by_key.rs | 5 +---- src/stream/stream/min_by_key.rs | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/stream/stream/max_by_key.rs b/src/stream/stream/max_by_key.rs index 5ebc25d..e421f94 100644 --- a/src/stream/stream/max_by_key.rs +++ b/src/stream/stream/max_by_key.rs @@ -59,10 +59,7 @@ where } Poll::Pending } - None => Poll::Ready(match this.max.take() { - None => None, - Some(max) => Some(max.1), - }), + None => Poll::Ready(this.max.take().map(|max| max.1)), } } } diff --git a/src/stream/stream/min_by_key.rs b/src/stream/stream/min_by_key.rs index 3cd0014..142dfe1 100644 --- a/src/stream/stream/min_by_key.rs +++ b/src/stream/stream/min_by_key.rs @@ -59,10 +59,7 @@ where } Poll::Pending } - None => Poll::Ready(match this.min.take() { - None => None, - Some(max) => Some(max.1), - }), + None => Poll::Ready(this.min.take().map(|min| min.1)), } } }