diff --git a/src/stream/stream/gt.rs b/src/stream/stream/gt.rs new file mode 100644 index 0000000..6c480a2 --- /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 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), + } + } +} diff --git a/src/stream/stream/mod.rs b/src/stream/stream/mod.rs index 30b6031..90fde15 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 le; mod lt; @@ -57,6 +58,7 @@ use find::FindFuture; use find_map::FindMapFuture; use fold::FoldFuture; use for_each::ForEachFuture; +use gt::GtFuture; use le::LeFuture; use lt::LtFuture; use min_by::MinByFuture; @@ -1230,14 +1232,16 @@ extension_trait! { #[doc = r#" Lexicographically compares the elements of this `Stream` with those of another. - + # Examples + ``` # fn main() { async_std::task::block_on(async { # 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]); let s3 = VecDeque::from(vec![1, 2, 3]); @@ -1263,17 +1267,54 @@ 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, + ::Item: PartialOrd, + { + GtFuture::new(self, other) + } + #[doc = r#" Determines if the elements of this `Stream` are lexicographically less or equal to 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]); @@ -1301,14 +1342,15 @@ extension_trait! { #[doc = r#" Determines if the elements of this `Stream` are lexicographically less 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]); @@ -1318,7 +1360,6 @@ extension_trait! { assert_eq!(single.clone().lt(single_gt.clone()).await, true); assert_eq!(multi.clone().lt(single_gt.clone()).await, true); assert_eq!(multi_gt.clone().lt(multi.clone()).await, false); - # # }) } ```