Skip to content

Commit d2d522e

Browse files
committed
Adds partial_cmp.rs file and partial_cmp signature to mod.rs
1 parent 30b5ca5 commit d2d522e

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
@@ -37,6 +37,7 @@ mod map;
3737
mod min_by;
3838
mod next;
3939
mod nth;
40+
mod partial_cmp;
4041
mod scan;
4142
mod skip;
4243
mod skip_while;
@@ -56,6 +57,7 @@ use for_each::ForEachFuture;
5657
use min_by::MinByFuture;
5758
use next::NextFuture;
5859
use nth::NthFuture;
60+
use partial_cmp::PartialCmpFuture;
5961
use try_for_each::TryForEeachFuture;
6062

6163
pub use chain::Chain;
@@ -1147,6 +1149,50 @@ extension_trait! {
11471149
{
11481150
FromStream::from_stream(self)
11491151
}
1152+
1153+
#[doc = r#"
1154+
Lexicographically compares the elements of this `Stream` with those
1155+
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+
use std::cmp::Ordering;
1165+
1166+
let result_equal = vec![1.].into_iter().collect::<VecDeque<f64>>()
1167+
.partial_cmp(vec![1.].into_iter().collect::<VecDeque<f64>>()).await;
1168+
let result_less = vec![1.].into_iter().collect::<VecDeque<f64>>()
1169+
.partial_cmp(vec![1., 2.].into_iter().collect::<VecDeque<f64>>()).await;
1170+
let result_greater = vec![1., 2.].into_iter().collect::<VecDeque<f64>>()
1171+
.partial_cmp(vec![1.].into_iter().collect::<VecDeque<f64>>()).await;
1172+
let result_none = vec![std::f64::NAN].into_iter().collect::<VecDeque<f64>>()
1173+
.partial_cmp(vec![1.].into_iter().collect::<VecDeque<f64>>()).await;
1174+
1175+
assert_eq!(result_equal, Some(Ordering::Equal));
1176+
assert_eq!(result_less, Some(Ordering::Less));
1177+
assert_eq!(result_greater, Some(Ordering::Greater));
1178+
assert_eq!(result_none, None);
1179+
1180+
#
1181+
# }) }
1182+
```
1183+
"#]
1184+
fn partial_cmp<S>(
1185+
self,
1186+
other: S
1187+
) -> impl Future<Output = Option<Ordering>> + '_ [PartialCmpFuture<Self, S>]
1188+
where
1189+
Self: Sized + Stream,
1190+
S: Stream,
1191+
Self::Item: PartialOrd<S::Item>,
1192+
{
1193+
PartialCmpFuture::new(self, other)
1194+
}
1195+
11501196
}
11511197

11521198
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)