Skip to content

Commit f0f279e

Browse files
assemblajStjepan Glavina
authored and
Stjepan Glavina
committed
Adds Stream::le (#336)
1 parent a8dc2c6 commit f0f279e

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

src/stream/stream/le.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use std::cmp::Ordering;
2+
use std::pin::Pin;
3+
4+
use super::partial_cmp::PartialCmpFuture;
5+
use crate::future::Future;
6+
use crate::prelude::*;
7+
use crate::stream::Stream;
8+
use crate::task::{Context, Poll};
9+
10+
/// Determines if the elements of this `Stream` are lexicographically
11+
/// less or equal to those of another.
12+
#[doc(hidden)]
13+
#[allow(missing_debug_implementations)]
14+
pub struct LeFuture<L: Stream, R: Stream> {
15+
partial_cmp: PartialCmpFuture<L, R>,
16+
}
17+
18+
impl<L: Stream, R: Stream> LeFuture<L, R>
19+
where
20+
L::Item: PartialOrd<R::Item>,
21+
{
22+
pin_utils::unsafe_pinned!(partial_cmp: PartialCmpFuture<L, R>);
23+
24+
pub(super) fn new(l: L, r: R) -> Self {
25+
LeFuture {
26+
partial_cmp: l.partial_cmp(r),
27+
}
28+
}
29+
}
30+
31+
impl<L: Stream, R: Stream> Future for LeFuture<L, R>
32+
where
33+
L: Stream + Sized,
34+
R: Stream + Sized,
35+
L::Item: PartialOrd<R::Item>,
36+
{
37+
type Output = bool;
38+
39+
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
40+
let result = futures_core::ready!(self.as_mut().partial_cmp().poll(cx));
41+
42+
match result {
43+
Some(Ordering::Less) | Some(Ordering::Equal) => Poll::Ready(true),
44+
_ => Poll::Ready(false),
45+
}
46+
}
47+
}

src/stream/stream/mod.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ mod fold;
3333
mod for_each;
3434
mod fuse;
3535
mod inspect;
36+
mod le;
3637
mod lt;
3738
mod map;
3839
mod min_by;
@@ -56,6 +57,7 @@ use find::FindFuture;
5657
use find_map::FindMapFuture;
5758
use fold::FoldFuture;
5859
use for_each::ForEachFuture;
60+
use le::LeFuture;
5961
use lt::LtFuture;
6062
use min_by::MinByFuture;
6163
use next::NextFuture;
@@ -1261,6 +1263,41 @@ extension_trait! {
12611263
PartialCmpFuture::new(self, other)
12621264
}
12631265

1266+
#[doc = r#"
1267+
Determines if the elements of this `Stream` are lexicographically
1268+
less or equal to those of another.
1269+
1270+
# Examples
1271+
```
1272+
# fn main() { async_std::task::block_on(async {
1273+
#
1274+
use async_std::prelude::*;
1275+
use std::collections::VecDeque;
1276+
1277+
let single = VecDeque::from(vec![1]);
1278+
let single_gt = VecDeque::from(vec![10]);
1279+
let multi = VecDeque::from(vec![1,2]);
1280+
let multi_gt = VecDeque::from(vec![1,5]);
1281+
assert_eq!(single.clone().le(single.clone()).await, true);
1282+
assert_eq!(single.clone().le(single_gt.clone()).await, true);
1283+
assert_eq!(multi.clone().le(single_gt.clone()).await, true);
1284+
assert_eq!(multi_gt.clone().le(multi.clone()).await, false);
1285+
#
1286+
# }) }
1287+
```
1288+
"#]
1289+
fn le<S>(
1290+
self,
1291+
other: S
1292+
) -> impl Future<Output = bool> [LeFuture<Self, S>]
1293+
where
1294+
Self: Sized + Stream,
1295+
S: Stream,
1296+
<Self as Stream>::Item: PartialOrd<S::Item>,
1297+
{
1298+
LeFuture::new(self, other)
1299+
}
1300+
12641301
#[doc = r#"
12651302
Determines if the elements of this `Stream` are lexicographically
12661303
less than those of another.

0 commit comments

Comments
 (0)