Skip to content

Commit a8dc2c6

Browse files
assemblajStjepan Glavina
authored and
Stjepan Glavina
committed
Adds Stream::lt (#337)
1 parent 24cdb2d commit a8dc2c6

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

src/stream/stream/lt.rs

+47
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 than those of another.
12+
#[doc(hidden)]
13+
#[allow(missing_debug_implementations)]
14+
pub struct LtFuture<L: Stream, R: Stream> {
15+
partial_cmp: PartialCmpFuture<L, R>,
16+
}
17+
18+
impl<L: Stream, R: Stream> LtFuture<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+
LtFuture {
26+
partial_cmp: l.partial_cmp(r),
27+
}
28+
}
29+
}
30+
31+
impl<L: Stream, R: Stream> Future for LtFuture<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) => Poll::Ready(true),
44+
_ => Poll::Ready(false),
45+
}
46+
}
47+
}

src/stream/stream/mod.rs

+39
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 lt;
3637
mod map;
3738
mod min_by;
3839
mod next;
@@ -55,6 +56,7 @@ use find::FindFuture;
5556
use find_map::FindMapFuture;
5657
use fold::FoldFuture;
5758
use for_each::ForEachFuture;
59+
use lt::LtFuture;
5860
use min_by::MinByFuture;
5961
use next::NextFuture;
6062
use nth::NthFuture;
@@ -1258,6 +1260,43 @@ extension_trait! {
12581260
{
12591261
PartialCmpFuture::new(self, other)
12601262
}
1263+
1264+
#[doc = r#"
1265+
Determines if the elements of this `Stream` are lexicographically
1266+
less than those of another.
1267+
1268+
# Examples
1269+
```
1270+
# fn main() { async_std::task::block_on(async {
1271+
#
1272+
use async_std::prelude::*;
1273+
use std::collections::VecDeque;
1274+
1275+
let single = VecDeque::from(vec![1]);
1276+
let single_gt = VecDeque::from(vec![10]);
1277+
let multi = VecDeque::from(vec![1,2]);
1278+
let multi_gt = VecDeque::from(vec![1,5]);
1279+
1280+
assert_eq!(single.clone().lt(single.clone()).await, false);
1281+
assert_eq!(single.clone().lt(single_gt.clone()).await, true);
1282+
assert_eq!(multi.clone().lt(single_gt.clone()).await, true);
1283+
assert_eq!(multi_gt.clone().lt(multi.clone()).await, false);
1284+
1285+
#
1286+
# }) }
1287+
```
1288+
"#]
1289+
fn lt<S>(
1290+
self,
1291+
other: S
1292+
) -> impl Future<Output = bool> [LtFuture<Self, S>]
1293+
where
1294+
Self: Sized + Stream,
1295+
S: Stream,
1296+
<Self as Stream>::Item: PartialOrd<S::Item>,
1297+
{
1298+
LtFuture::new(self, other)
1299+
}
12611300
}
12621301

12631302
impl<S: Stream + Unpin + ?Sized> Stream for Box<S> {

0 commit comments

Comments
 (0)