Skip to content

Commit b1c1f4d

Browse files
committed
[Draft PR] Adds Stream::gt
1 parent 30b5ca5 commit b1c1f4d

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

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

Lines changed: 40 additions & 0 deletions
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 map;
3738
mod min_by;
@@ -53,6 +54,7 @@ use find::FindFuture;
5354
use find_map::FindMapFuture;
5455
use fold::FoldFuture;
5556
use for_each::ForEachFuture;
57+
use gt::GtFuture;
5658
use min_by::MinByFuture;
5759
use next::NextFuture;
5860
use nth::NthFuture;
@@ -1147,6 +1149,44 @@ extension_trait! {
11471149
{
11481150
FromStream::from_stream(self)
11491151
}
1152+
1153+
#[doc = r#"
1154+
Determines if the elements of this `Stream` are lexicographically
1155+
greater than those of another.
1156+
1157+
# Examples
1158+
```
1159+
# fn main() { async_std::task::block_on(async {
1160+
#
1161+
use async_std::prelude::*;
1162+
use std::collections::VecDeque;
1163+
1164+
let single = VecDeque::from(vec![1]);
1165+
let single_gt = VecDeque::from(vec![10]);
1166+
let multi = VecDeque::from(vec![1,2]);
1167+
let multi_gt = VecDeque::from(vec![1,5]);
1168+
1169+
assert_eq!(single.clone().gt(single.clone()).await, false);
1170+
assert_eq!(single_gt.clone().gt(single.clone()).await, true);
1171+
assert_eq!(multi.clone().gt(single_gt.clone()).await, false);
1172+
assert_eq!(multi_gt.clone().gt(multi.clone()).await, true);
1173+
1174+
#
1175+
# }) }
1176+
```
1177+
"#]
1178+
fn gt<S>(
1179+
self,
1180+
other: S
1181+
) -> impl Future<Output = bool> + '_ [GtFuture<Self, S>]
1182+
where
1183+
Self: Sized + Stream,
1184+
S: Stream,
1185+
Self::Item: PartialOrd<S::Item>,
1186+
{
1187+
GtFuture::new(self, other)
1188+
}
1189+
11501190
}
11511191

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

0 commit comments

Comments
 (0)