Skip to content

Commit 3e0fe74

Browse files
authored
Merge pull request #427 from yjhmelody/stream-ne
Add stream ne
2 parents 65dcaf4 + 204da33 commit 3e0fe74

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

src/stream/stream/mod.rs

+35
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ mod map;
4444
mod max_by;
4545
mod min_by;
4646
mod min_by_key;
47+
mod ne;
4748
mod next;
4849
mod nth;
4950
mod partial_cmp;
@@ -75,6 +76,7 @@ use lt::LtFuture;
7576
use max_by::MaxByFuture;
7677
use min_by::MinByFuture;
7778
use min_by_key::MinByKeyFuture;
79+
use ne::NeFuture;
7880
use next::NextFuture;
7981
use nth::NthFuture;
8082
use partial_cmp::PartialCmpFuture;
@@ -1588,6 +1590,39 @@ extension_trait! {
15881590
CmpFuture::new(self, other)
15891591
}
15901592

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

src/stream/stream/ne.rs

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 => {
57+
continue;
58+
}
59+
_ => {
60+
return Poll::Ready(true);
61+
}
62+
}
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)