From 4dc6a6262145c7b98049572dd9306b95eaa828d8 Mon Sep 17 00:00:00 2001 From: arheard Date: Thu, 10 Oct 2019 14:33:21 -0400 Subject: [PATCH 1/4] [Draft PR] Adds Stream::gt --- src/stream/stream/gt.rs | 47 ++++++++++++++++++++++++++++++++++++++++ src/stream/stream/mod.rs | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 src/stream/stream/gt.rs diff --git a/src/stream/stream/gt.rs b/src/stream/stream/gt.rs new file mode 100644 index 000000000..2d52e5c51 --- /dev/null +++ b/src/stream/stream/gt.rs @@ -0,0 +1,47 @@ +use std::cmp::Ordering; +use std::pin::Pin; + +use super::partial_cmp::PartialCmpFuture; +use crate::future::Future; +use crate::prelude::*; +use crate::stream::Stream; +use crate::task::{Context, Poll}; + +// Determines if the elements of this `Stream` are lexicographically +// greater than or equal to those of another. +#[doc(hidden)] +#[allow(missing_debug_implementations)] +pub struct GtFuture { + partial_cmp: PartialCmpFuture, +} + +impl GtFuture +where + L::Item: PartialOrd, +{ + pin_utils::unsafe_pinned!(partial_cmp: PartialCmpFuture); + + pub(super) fn new(l: L, r: R) -> Self { + GtFuture { + partial_cmp: l.partial_cmp(r), + } + } +} + +impl Future for GtFuture +where + L: Stream + Sized, + R: Stream + Sized, + L::Item: PartialOrd, +{ + type Output = bool; + + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + let result = futures_core::ready!(self.as_mut().partial_cmp().poll(cx)); + + match result { + Some(Ordering::Greater) => Poll::Ready(true), + _ => Poll::Ready(false), + } + } +} \ No newline at end of file diff --git a/src/stream/stream/mod.rs b/src/stream/stream/mod.rs index 0e563f6d3..55c713cfc 100644 --- a/src/stream/stream/mod.rs +++ b/src/stream/stream/mod.rs @@ -32,6 +32,7 @@ mod find_map; mod fold; mod for_each; mod fuse; +mod gt; mod inspect; mod map; mod min_by; @@ -55,6 +56,7 @@ use find::FindFuture; use find_map::FindMapFuture; use fold::FoldFuture; use for_each::ForEachFuture; +use gt::GtFuture; use min_by::MinByFuture; use next::NextFuture; use nth::NthFuture; @@ -1231,6 +1233,7 @@ extension_trait! { # use async_std::prelude::*; use std::collections::VecDeque; + use std::cmp::Ordering; let s1 = VecDeque::from(vec![1]); let s2 = VecDeque::from(vec![1, 2]); @@ -1256,6 +1259,43 @@ extension_trait! { { PartialCmpFuture::new(self, other) } + + #[doc = r#" + Determines if the elements of this `Stream` are lexicographically + greater than those of another. + + # Examples + ``` + # fn main() { async_std::task::block_on(async { + # + use async_std::prelude::*; + use std::collections::VecDeque; + + let single = VecDeque::from(vec![1]); + let single_gt = VecDeque::from(vec![10]); + let multi = VecDeque::from(vec![1,2]); + let multi_gt = VecDeque::from(vec![1,5]); + + assert_eq!(single.clone().gt(single.clone()).await, false); + assert_eq!(single_gt.clone().gt(single.clone()).await, true); + assert_eq!(multi.clone().gt(single_gt.clone()).await, false); + assert_eq!(multi_gt.clone().gt(multi.clone()).await, true); + + # + # }) } + ``` + "#] + fn gt( + self, + other: S + ) -> impl Future + '_ [GtFuture] + where + Self: Sized + Stream, + S: Stream, + Self::Item: PartialOrd, + { + GtFuture::new(self, other) + } } impl Stream for Box { From be14af51c4549f119d5ba4205cc738923d01da99 Mon Sep 17 00:00:00 2001 From: arheard Date: Thu, 10 Oct 2019 14:41:17 -0400 Subject: [PATCH 2/4] Applies cargo format and fixes incorrect comment --- src/stream/stream/gt.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/stream/stream/gt.rs b/src/stream/stream/gt.rs index 2d52e5c51..6c480a25a 100644 --- a/src/stream/stream/gt.rs +++ b/src/stream/stream/gt.rs @@ -8,7 +8,7 @@ use crate::stream::Stream; use crate::task::{Context, Poll}; // Determines if the elements of this `Stream` are lexicographically -// greater than or equal to those of another. +// greater than those of another. #[doc(hidden)] #[allow(missing_debug_implementations)] pub struct GtFuture { @@ -44,4 +44,4 @@ where _ => Poll::Ready(false), } } -} \ No newline at end of file +} From 5297c2e012a7f7d08b8615870eb65a61c8def31e Mon Sep 17 00:00:00 2001 From: assemblaj Date: Tue, 15 Oct 2019 09:59:18 -0400 Subject: [PATCH 3/4] cargo fmt --- src/stream/stream/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/stream/stream/mod.rs b/src/stream/stream/mod.rs index 55c713cfc..dfe2db945 100644 --- a/src/stream/stream/mod.rs +++ b/src/stream/stream/mod.rs @@ -1259,7 +1259,7 @@ extension_trait! { { PartialCmpFuture::new(self, other) } - + #[doc = r#" Determines if the elements of this `Stream` are lexicographically greater than those of another. @@ -1288,14 +1288,14 @@ extension_trait! { fn gt( self, other: S - ) -> impl Future + '_ [GtFuture] + ) -> impl Future + '_ [GtFuture] where Self: Sized + Stream, - S: Stream, + S: Stream, Self::Item: PartialOrd, { GtFuture::new(self, other) - } + } } impl Stream for Box { From 8a3b4278cea5ffd5b73d5cea68e409120796777a Mon Sep 17 00:00:00 2001 From: assemblaj Date: Tue, 15 Oct 2019 10:43:49 -0400 Subject: [PATCH 4/4] fixes rustdoc related issues --- 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 dfe2db945..897d2e424 100644 --- a/src/stream/stream/mod.rs +++ b/src/stream/stream/mod.rs @@ -1288,11 +1288,11 @@ extension_trait! { fn gt( self, other: S - ) -> impl Future + '_ [GtFuture] + ) -> impl Future [GtFuture] where Self: Sized + Stream, S: Stream, - Self::Item: PartialOrd, + ::Item: PartialOrd, { GtFuture::new(self, other) }