From a8dc2c6f9b8516109d7e301ca9de22e1aee41d4c Mon Sep 17 00:00:00 2001 From: assemblaj <7599535+assemblaj@users.noreply.github.com> Date: Tue, 15 Oct 2019 18:32:54 -0400 Subject: [PATCH] Adds Stream::lt (#337) --- src/stream/stream/lt.rs | 47 ++++++++++++++++++++++++++++++++++++++++ src/stream/stream/mod.rs | 39 +++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 src/stream/stream/lt.rs diff --git a/src/stream/stream/lt.rs b/src/stream/stream/lt.rs new file mode 100644 index 0000000..b774d7b --- /dev/null +++ b/src/stream/stream/lt.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 +// less than those of another. +#[doc(hidden)] +#[allow(missing_debug_implementations)] +pub struct LtFuture { + partial_cmp: PartialCmpFuture, +} + +impl LtFuture +where + L::Item: PartialOrd, +{ + pin_utils::unsafe_pinned!(partial_cmp: PartialCmpFuture); + + pub(super) fn new(l: L, r: R) -> Self { + LtFuture { + partial_cmp: l.partial_cmp(r), + } + } +} + +impl Future for LtFuture +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::Less) => Poll::Ready(true), + _ => Poll::Ready(false), + } + } +} diff --git a/src/stream/stream/mod.rs b/src/stream/stream/mod.rs index 2ee01d7..dfd8d49 100644 --- a/src/stream/stream/mod.rs +++ b/src/stream/stream/mod.rs @@ -33,6 +33,7 @@ mod fold; mod for_each; mod fuse; mod inspect; +mod lt; mod map; mod min_by; mod next; @@ -55,6 +56,7 @@ use find::FindFuture; use find_map::FindMapFuture; use fold::FoldFuture; use for_each::ForEachFuture; +use lt::LtFuture; use min_by::MinByFuture; use next::NextFuture; use nth::NthFuture; @@ -1258,6 +1260,43 @@ extension_trait! { { PartialCmpFuture::new(self, other) } + + #[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]); + let multi_gt = VecDeque::from(vec![1,5]); + + assert_eq!(single.clone().lt(single.clone()).await, false); + 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); + + # + # }) } + ``` + "#] + fn lt( + self, + other: S + ) -> impl Future [LtFuture] + where + Self: Sized + Stream, + S: Stream, + ::Item: PartialOrd, + { + LtFuture::new(self, other) + } } impl Stream for Box {