diff --git a/src/stream/stream/le.rs b/src/stream/stream/le.rs new file mode 100644 index 00000000..37b62d83 --- /dev/null +++ b/src/stream/stream/le.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 or equal to those of another. +#[doc(hidden)] +#[allow(missing_debug_implementations)] +pub struct LeFuture { + partial_cmp: PartialCmpFuture, +} + +impl LeFuture +where + L::Item: PartialOrd, +{ + pin_utils::unsafe_pinned!(partial_cmp: PartialCmpFuture); + + pub(super) fn new(l: L, r: R) -> Self { + LeFuture { + partial_cmp: l.partial_cmp(r), + } + } +} + +impl Future for LeFuture +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) | Some(Ordering::Equal) => Poll::Ready(true), + _ => Poll::Ready(false), + } + } +} diff --git a/src/stream/stream/mod.rs b/src/stream/stream/mod.rs index dfd8d49f..30b6031e 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 le; mod lt; mod map; mod min_by; @@ -56,6 +57,7 @@ use find::FindFuture; use find_map::FindMapFuture; use fold::FoldFuture; use for_each::ForEachFuture; +use le::LeFuture; use lt::LtFuture; use min_by::MinByFuture; use next::NextFuture; @@ -1261,6 +1263,41 @@ extension_trait! { PartialCmpFuture::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]); + let multi_gt = VecDeque::from(vec![1,5]); + assert_eq!(single.clone().le(single.clone()).await, true); + assert_eq!(single.clone().le(single_gt.clone()).await, true); + assert_eq!(multi.clone().le(single_gt.clone()).await, true); + assert_eq!(multi_gt.clone().le(multi.clone()).await, false); + # + # }) } + ``` + "#] + fn le( + self, + other: S + ) -> impl Future [LeFuture] + where + Self: Sized + Stream, + S: Stream, + ::Item: PartialOrd, + { + LeFuture::new(self, other) + } + #[doc = r#" Determines if the elements of this `Stream` are lexicographically less than those of another.