From a7041be6f291db6f465e9ac8a823ed387c7066fe Mon Sep 17 00:00:00 2001 From: assemblaj <7599535+assemblaj@users.noreply.github.com> Date: Tue, 15 Oct 2019 19:24:27 -0400 Subject: [PATCH] Adds Stream:ge (#285) * Adds partial_cmp.rs file and partial_cmp signature to mod.rs * adds tests that compare streams of same length * Adds Stream::ge * cargo fmt * fixes rustdoc error --- src/stream/stream/ge.rs | 47 ++++++++++++++++++++++++++++++++++++++++ src/stream/stream/mod.rs | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 src/stream/stream/ge.rs diff --git a/src/stream/stream/ge.rs b/src/stream/stream/ge.rs new file mode 100644 index 0000000..eb9786b --- /dev/null +++ b/src/stream/stream/ge.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 GeFuture { + partial_cmp: PartialCmpFuture, +} + +impl GeFuture +where + L::Item: PartialOrd, +{ + pin_utils::unsafe_pinned!(partial_cmp: PartialCmpFuture); + + pub(super) fn new(l: L, r: R) -> Self { + GeFuture { + partial_cmp: l.partial_cmp(r), + } + } +} + +impl Future for GeFuture +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) | Some(Ordering::Equal) => Poll::Ready(true), + _ => Poll::Ready(false), + } + } +} diff --git a/src/stream/stream/mod.rs b/src/stream/stream/mod.rs index 90fde15..fbbd8f6 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 ge; mod gt; mod inspect; mod le; @@ -58,6 +59,7 @@ use find::FindFuture; use find_map::FindMapFuture; use fold::FoldFuture; use for_each::ForEachFuture; +use ge::GeFuture; use gt::GtFuture; use le::LeFuture; use lt::LtFuture; @@ -1240,6 +1242,7 @@ extension_trait! { # use async_std::prelude::*; use std::collections::VecDeque; + use std::cmp::Ordering; let s1 = VecDeque::from(vec![1]); @@ -1267,6 +1270,43 @@ extension_trait! { PartialCmpFuture::new(self, other) } + + #[doc = r#" + Determines if the elements of this `Stream` are lexicographically + greater than 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 = vec![1].into_iter().collect(); + let single_gt: VecDeque = vec![10].into_iter().collect(); + let multi: VecDeque = vec![1,2].into_iter().collect(); + let multi_gt: VecDeque = vec![1,5].into_iter().collect(); + assert_eq!(single.clone().ge(single.clone()).await, true); + assert_eq!(single_gt.clone().ge(single.clone()).await, true); + assert_eq!(multi.clone().ge(single_gt.clone()).await, false); + assert_eq!(multi_gt.clone().ge(multi.clone()).await, true); + # + # }) } + ``` + "#] + fn ge( + self, + other: S + ) -> impl Future [GeFuture] + where + Self: Sized + Stream, + S: Stream, + ::Item: PartialOrd, + { + GeFuture::new(self, other) + } + #[doc = r#" Determines if the elements of this `Stream` are lexicographically greater than those of another.