Skip to content

Commit 5f7238e

Browse files
assemblajStjepan Glavina
authored and
Stjepan Glavina
committed
[Draft PR] Adds Stream::gt (#304)
* [Draft PR] Adds Stream::gt * Applies cargo format and fixes incorrect comment * cargo fmt * fixes rustdoc related issues
1 parent f0f279e commit 5f7238e

File tree

2 files changed

+94
-6
lines changed

2 files changed

+94
-6
lines changed

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

src/stream/stream/mod.rs

+47-6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ mod find_map;
3232
mod fold;
3333
mod for_each;
3434
mod fuse;
35+
mod gt;
3536
mod inspect;
3637
mod le;
3738
mod lt;
@@ -57,6 +58,7 @@ use find::FindFuture;
5758
use find_map::FindMapFuture;
5859
use fold::FoldFuture;
5960
use for_each::ForEachFuture;
61+
use gt::GtFuture;
6062
use le::LeFuture;
6163
use lt::LtFuture;
6264
use min_by::MinByFuture;
@@ -1230,14 +1232,16 @@ extension_trait! {
12301232
#[doc = r#"
12311233
Lexicographically compares the elements of this `Stream` with those
12321234
of another.
1233-
1235+
12341236
# Examples
1237+
12351238
```
12361239
# fn main() { async_std::task::block_on(async {
12371240
#
12381241
use async_std::prelude::*;
12391242
use std::collections::VecDeque;
12401243
use std::cmp::Ordering;
1244+
12411245
let s1 = VecDeque::from(vec![1]);
12421246
let s2 = VecDeque::from(vec![1, 2]);
12431247
let s3 = VecDeque::from(vec![1, 2, 3]);
@@ -1263,17 +1267,54 @@ extension_trait! {
12631267
PartialCmpFuture::new(self, other)
12641268
}
12651269

1270+
#[doc = r#"
1271+
Determines if the elements of this `Stream` are lexicographically
1272+
greater than those of another.
1273+
1274+
# Examples
1275+
1276+
```
1277+
# fn main() { async_std::task::block_on(async {
1278+
#
1279+
use async_std::prelude::*;
1280+
use std::collections::VecDeque;
1281+
1282+
let single = VecDeque::from(vec![1]);
1283+
let single_gt = VecDeque::from(vec![10]);
1284+
let multi = VecDeque::from(vec![1,2]);
1285+
let multi_gt = VecDeque::from(vec![1,5]);
1286+
assert_eq!(single.clone().gt(single.clone()).await, false);
1287+
assert_eq!(single_gt.clone().gt(single.clone()).await, true);
1288+
assert_eq!(multi.clone().gt(single_gt.clone()).await, false);
1289+
assert_eq!(multi_gt.clone().gt(multi.clone()).await, true);
1290+
#
1291+
# }) }
1292+
```
1293+
"#]
1294+
fn gt<S>(
1295+
self,
1296+
other: S
1297+
) -> impl Future<Output = bool> [GtFuture<Self, S>]
1298+
where
1299+
Self: Sized + Stream,
1300+
S: Stream,
1301+
<Self as Stream>::Item: PartialOrd<S::Item>,
1302+
{
1303+
GtFuture::new(self, other)
1304+
}
1305+
12661306
#[doc = r#"
12671307
Determines if the elements of this `Stream` are lexicographically
12681308
less or equal to those of another.
1269-
1309+
12701310
# Examples
1311+
12711312
```
12721313
# fn main() { async_std::task::block_on(async {
12731314
#
12741315
use async_std::prelude::*;
12751316
use std::collections::VecDeque;
1276-
1317+
12771318
let single = VecDeque::from(vec![1]);
12781319
let single_gt = VecDeque::from(vec![10]);
12791320
let multi = VecDeque::from(vec![1,2]);
@@ -1301,14 +1342,15 @@ extension_trait! {
13011342
#[doc = r#"
13021343
Determines if the elements of this `Stream` are lexicographically
13031344
less than those of another.
1304-
1345+
13051346
# Examples
1347+
13061348
```
13071349
# fn main() { async_std::task::block_on(async {
13081350
#
13091351
use async_std::prelude::*;
13101352
use std::collections::VecDeque;
1311-
1353+
13121354
let single = VecDeque::from(vec![1]);
13131355
let single_gt = VecDeque::from(vec![10]);
13141356
let multi = VecDeque::from(vec![1,2]);
@@ -1318,7 +1360,6 @@ extension_trait! {
13181360
assert_eq!(single.clone().lt(single_gt.clone()).await, true);
13191361
assert_eq!(multi.clone().lt(single_gt.clone()).await, true);
13201362
assert_eq!(multi_gt.clone().lt(multi.clone()).await, false);
1321-
13221363
#
13231364
# }) }
13241365
```

0 commit comments

Comments
 (0)