Skip to content

Commit 22afa47

Browse files
committed
Adds partial_cmp.rs file and partial_cmp signature to mod.rs
1 parent 33d2191 commit 22afa47

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

src/stream/stream/mod.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ mod inspect;
3535
mod min_by;
3636
mod next;
3737
mod nth;
38+
mod partial_cmp;
3839
mod scan;
3940
mod skip;
4041
mod skip_while;
@@ -52,6 +53,7 @@ use fold::FoldFuture;
5253
use min_by::MinByFuture;
5354
use next::NextFuture;
5455
use nth::NthFuture;
56+
use partial_cmp::PartialCmpFuture;
5557

5658
pub use chain::Chain;
5759
pub use filter::Filter;
@@ -1030,6 +1032,50 @@ extension_trait! {
10301032
{
10311033
FromStream::from_stream(self)
10321034
}
1035+
1036+
#[doc = r#"
1037+
Lexicographically compares the elements of this `Stream` with those
1038+
of another.
1039+
1040+
# Examples
1041+
```
1042+
# fn main() { async_std::task::block_on(async {
1043+
#
1044+
use async_std::prelude::*;
1045+
use std::collections::VecDeque;
1046+
1047+
use std::cmp::Ordering;
1048+
1049+
let result_equal = vec![1.].into_iter().collect::<VecDeque<f64>>()
1050+
.partial_cmp(vec![1.].into_iter().collect::<VecDeque<f64>>()).await;
1051+
let result_less = vec![1.].into_iter().collect::<VecDeque<f64>>()
1052+
.partial_cmp(vec![1., 2.].into_iter().collect::<VecDeque<f64>>()).await;
1053+
let result_greater = vec![1., 2.].into_iter().collect::<VecDeque<f64>>()
1054+
.partial_cmp(vec![1.].into_iter().collect::<VecDeque<f64>>()).await;
1055+
let result_none = vec![std::f64::NAN].into_iter().collect::<VecDeque<f64>>()
1056+
.partial_cmp(vec![1.].into_iter().collect::<VecDeque<f64>>()).await;
1057+
1058+
assert_eq!(result_equal, Some(Ordering::Equal));
1059+
assert_eq!(result_less, Some(Ordering::Less));
1060+
assert_eq!(result_greater, Some(Ordering::Greater));
1061+
assert_eq!(result_none, None);
1062+
1063+
#
1064+
# }) }
1065+
```
1066+
"#]
1067+
fn partial_cmp<S>(
1068+
self,
1069+
other: S
1070+
) -> impl Future<Output = Option<Ordering>> + '_ [PartialCmpFuture<Self, S>]
1071+
where
1072+
Self: Sized + Stream,
1073+
S: Stream,
1074+
Self::Item: PartialOrd<S::Item>,
1075+
{
1076+
PartialCmpFuture::new(self, other)
1077+
}
1078+
10331079
}
10341080

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

src/stream/stream/partial_cmp.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use std::cmp::Ordering;
2+
use std::pin::Pin;
3+
4+
use crate::future::Future;
5+
use crate::stream::Stream;
6+
use crate::task::{Context, Poll};
7+
8+
// Lexicographically compares the elements of this `Stream` with those
9+
// of another.
10+
#[derive(Debug)]
11+
pub struct PartialCmpFuture<L: Stream, R> {
12+
l: L,
13+
r: R,
14+
}
15+
16+
impl<L: Stream + Unpin, R: Unpin> Unpin for PartialCmpFuture<L, R> {}
17+
18+
impl<L: Stream, R> PartialCmpFuture<L, R> {
19+
pub(crate) fn new(l: L, r: R) -> Self {
20+
PartialCmpFuture {
21+
l,
22+
r,
23+
}
24+
}
25+
26+
pin_utils::unsafe_pinned!(l: L);
27+
pin_utils::unsafe_pinned!(r: R);
28+
}
29+
30+
impl<L: Stream, R: Stream> Future for PartialCmpFuture<L, R>
31+
where
32+
L: Stream + Unpin + Sized,
33+
R: Stream + Unpin + Sized,
34+
L::Item: PartialOrd<R::Item>
35+
{
36+
type Output = Option<Ordering>;
37+
38+
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
39+
let l_next = futures_core::ready!(self.as_mut().l().poll_next(cx));
40+
let r_next = futures_core::ready!(self.as_mut().r().poll_next(cx));
41+
42+
match (l_next, r_next) {
43+
(None, None) => Poll::Ready(Some(Ordering::Equal)),
44+
(None, _ ) => Poll::Ready(Some(Ordering::Less)),
45+
(_, None) => Poll::Ready(Some(Ordering::Greater)),
46+
(Some(x), Some(y)) => match x.partial_cmp(&y) {
47+
Some(Ordering::Equal) => {
48+
// wakes task to pull the next item from stream
49+
cx.waker().wake_by_ref();
50+
Poll::Pending
51+
},
52+
non_eq => Poll::Ready(non_eq),
53+
}
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)