Skip to content

Commit a7041be

Browse files
assemblajStjepan Glavina
authored and
Stjepan Glavina
committed
Adds Stream:ge (#285)
* Adds partial_cmp.rs file and partial_cmp signature to mod.rs * adds tests that compare streams of same length * Adds Stream::ge * cargo fmt * fixes rustdoc error
1 parent 5f7238e commit a7041be

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

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

src/stream/stream/mod.rs

+40
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 ge;
3536
mod gt;
3637
mod inspect;
3738
mod le;
@@ -58,6 +59,7 @@ use find::FindFuture;
5859
use find_map::FindMapFuture;
5960
use fold::FoldFuture;
6061
use for_each::ForEachFuture;
62+
use ge::GeFuture;
6163
use gt::GtFuture;
6264
use le::LeFuture;
6365
use lt::LtFuture;
@@ -1240,6 +1242,7 @@ extension_trait! {
12401242
#
12411243
use async_std::prelude::*;
12421244
use std::collections::VecDeque;
1245+
12431246
use std::cmp::Ordering;
12441247
12451248
let s1 = VecDeque::from(vec![1]);
@@ -1267,6 +1270,43 @@ extension_trait! {
12671270
PartialCmpFuture::new(self, other)
12681271
}
12691272

1273+
1274+
#[doc = r#"
1275+
Determines if the elements of this `Stream` are lexicographically
1276+
greater than or equal to those of another.
1277+
1278+
# Examples
1279+
1280+
```
1281+
# fn main() { async_std::task::block_on(async {
1282+
#
1283+
use async_std::prelude::*;
1284+
use std::collections::VecDeque;
1285+
1286+
let single: VecDeque<isize> = vec![1].into_iter().collect();
1287+
let single_gt: VecDeque<isize> = vec![10].into_iter().collect();
1288+
let multi: VecDeque<isize> = vec![1,2].into_iter().collect();
1289+
let multi_gt: VecDeque<isize> = vec![1,5].into_iter().collect();
1290+
assert_eq!(single.clone().ge(single.clone()).await, true);
1291+
assert_eq!(single_gt.clone().ge(single.clone()).await, true);
1292+
assert_eq!(multi.clone().ge(single_gt.clone()).await, false);
1293+
assert_eq!(multi_gt.clone().ge(multi.clone()).await, true);
1294+
#
1295+
# }) }
1296+
```
1297+
"#]
1298+
fn ge<S>(
1299+
self,
1300+
other: S
1301+
) -> impl Future<Output = bool> [GeFuture<Self, S>]
1302+
where
1303+
Self: Sized + Stream,
1304+
S: Stream,
1305+
<Self as Stream>::Item: PartialOrd<S::Item>,
1306+
{
1307+
GeFuture::new(self, other)
1308+
}
1309+
12701310
#[doc = r#"
12711311
Determines if the elements of this `Stream` are lexicographically
12721312
greater than those of another.

0 commit comments

Comments
 (0)