Skip to content

Commit 654ce8b

Browse files
committed
Adds Stream::ge
1 parent c6e5df6 commit 654ce8b

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

src/stream/stream/ge.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+
// 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

Lines changed: 41 additions & 1 deletion
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 inspect;
3637
mod map;
3738
mod min_by;
@@ -54,6 +55,7 @@ use find::FindFuture;
5455
use find_map::FindMapFuture;
5556
use fold::FoldFuture;
5657
use for_each::ForEachFuture;
58+
use ge::GeFuture;
5759
use min_by::MinByFuture;
5860
use next::NextFuture;
5961
use nth::NthFuture;
@@ -1197,7 +1199,45 @@ extension_trait! {
11971199
{
11981200
PartialCmpFuture::new(self, other)
11991201
}
1200-
1202+
1203+
#[doc = r#"
1204+
Determines if the elements of this `Stream` are lexicographically
1205+
greater than or equal to those of another.
1206+
1207+
# Examples
1208+
```
1209+
# fn main() { async_std::task::block_on(async {
1210+
#
1211+
use async_std::prelude::*;
1212+
use std::collections::VecDeque;
1213+
1214+
let single: VecDeque<isize> = vec![1].into_iter().collect();
1215+
let single_gt: VecDeque<isize> = vec![10].into_iter().collect();
1216+
let multi: VecDeque<isize> = vec![1,2].into_iter().collect();
1217+
let multi_gt: VecDeque<isize> = vec![1,5].into_iter().collect();
1218+
1219+
assert_eq!(single.clone().ge(single.clone()).await, true);
1220+
assert_eq!(single_gt.clone().ge(single.clone()).await, true);
1221+
1222+
assert_eq!(multi.clone().ge(single_gt.clone()).await, false);
1223+
assert_eq!(multi_gt.clone().ge(multi.clone()).await, true);
1224+
1225+
1226+
#
1227+
# }) }
1228+
```
1229+
"#]
1230+
fn ge<S>(
1231+
self,
1232+
other: S
1233+
) -> impl Future<Output = bool> + '_ [GeFuture<Self, S>]
1234+
where
1235+
Self: Sized + Stream,
1236+
S: Stream,
1237+
Self::Item: PartialOrd<S::Item>,
1238+
{
1239+
GeFuture::new(self, other)
1240+
}
12011241
}
12021242

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

0 commit comments

Comments
 (0)