Skip to content

Commit 17db7ff

Browse files
committed
Add stream ne
1 parent cc949f4 commit 17db7ff

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

src/stream/stream/mod.rs

+35
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ mod map;
4343
mod max_by;
4444
mod min_by;
4545
mod min_by_key;
46+
mod ne;
4647
mod next;
4748
mod nth;
4849
mod partial_cmp;
@@ -73,6 +74,7 @@ use lt::LtFuture;
7374
use max_by::MaxByFuture;
7475
use min_by::MinByFuture;
7576
use min_by_key::MinByKeyFuture;
77+
use ne::NeFuture;
7678
use next::NextFuture;
7779
use nth::NthFuture;
7880
use partial_cmp::PartialCmpFuture;
@@ -1586,6 +1588,39 @@ extension_trait! {
15861588
CmpFuture::new(self, other)
15871589
}
15881590

1591+
#[doc = r#"
1592+
Determines if the elements of this `Stream` are lexicographically
1593+
not equal to those of another.
1594+
# Examples
1595+
```
1596+
# fn main() { async_std::task::block_on(async {
1597+
#
1598+
use async_std::prelude::*;
1599+
use std::collections::VecDeque;
1600+
let single: VecDeque<isize> = vec![1].into_iter().collect();
1601+
let single_ne: VecDeque<isize> = vec![10].into_iter().collect();
1602+
let multi: VecDeque<isize> = vec![1,2].into_iter().collect();
1603+
let multi_ne: VecDeque<isize> = vec![1,5].into_iter().collect();
1604+
assert_eq!(single.clone().ne(single.clone()).await, false);
1605+
assert_eq!(single_ne.clone().ne(single.clone()).await, true);
1606+
assert_eq!(multi.clone().ne(single_ne.clone()).await, true);
1607+
assert_eq!(multi_ne.clone().ne(multi.clone()).await, true);
1608+
#
1609+
# }) }
1610+
```
1611+
"#]
1612+
fn ne<S>(
1613+
self,
1614+
other: S
1615+
) -> impl Future<Output = bool> [NeFuture<Self, S>]
1616+
where
1617+
Self: Sized + Stream,
1618+
S: Sized + Stream,
1619+
<Self as Stream>::Item: PartialEq<S::Item>,
1620+
{
1621+
NeFuture::new(self, other)
1622+
}
1623+
15891624
#[doc = r#"
15901625
Determines if the elements of this `Stream` are lexicographically
15911626
greater than or equal to those of another.

src/stream/stream/ne.rs

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use std::pin::Pin;
2+
3+
use pin_project_lite::pin_project;
4+
5+
use super::fuse::Fuse;
6+
use crate::future::Future;
7+
use crate::prelude::*;
8+
use crate::stream::Stream;
9+
use crate::task::{Context, Poll};
10+
11+
pin_project! {
12+
// Lexicographically compares the elements of this `Stream` with those
13+
// of another.
14+
#[doc(hidden)]
15+
#[allow(missing_debug_implementations)]
16+
pub struct NeFuture<L: Stream, R: Stream> {
17+
#[pin]
18+
l: Fuse<L>,
19+
#[pin]
20+
r: Fuse<R>,
21+
}
22+
}
23+
24+
impl<L: Stream, R: Stream> NeFuture<L, R>
25+
where
26+
L::Item: PartialEq<R::Item>,
27+
{
28+
pub(super) fn new(l: L, r: R) -> Self {
29+
Self {
30+
l: l.fuse(),
31+
r: r.fuse(),
32+
}
33+
}
34+
}
35+
36+
impl<L: Stream, R: Stream> Future for NeFuture<L, R>
37+
where
38+
L: Stream + Sized,
39+
R: Stream + Sized,
40+
L::Item: PartialEq<R::Item>,
41+
{
42+
type Output = bool;
43+
44+
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
45+
let mut this = self.project();
46+
47+
loop {
48+
let l_val = futures_core::ready!(this.l.as_mut().poll_next(cx));
49+
let r_val = futures_core::ready!(this.r.as_mut().poll_next(cx));
50+
51+
if this.l.done || this.r.done {
52+
return Poll::Ready(false);
53+
}
54+
55+
match (l_val, r_val) {
56+
(Some(l), Some(r)) if l == r => {continue;},
57+
_ => { return Poll::Ready(true); },
58+
}
59+
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)