Skip to content

Commit f102588

Browse files
authored
Merge pull request #428 from zhangguyu6/stream-position
Add stream position
2 parents 3e0fe74 + 07d21e5 commit f102588

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

src/stream/stream/mod.rs

+41
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ mod ne;
4848
mod next;
4949
mod nth;
5050
mod partial_cmp;
51+
mod position;
5152
mod scan;
5253
mod skip;
5354
mod skip_while;
@@ -80,6 +81,7 @@ use ne::NeFuture;
8081
use next::NextFuture;
8182
use nth::NthFuture;
8283
use partial_cmp::PartialCmpFuture;
84+
use position::PositionFuture;
8385
use try_fold::TryFoldFuture;
8486
use try_for_each::TryForEeachFuture;
8587

@@ -1552,6 +1554,45 @@ extension_trait! {
15521554
PartialCmpFuture::new(self, other)
15531555
}
15541556

1557+
#[doc = r#"
1558+
Searches for an element in a Stream that satisfies a predicate, returning
1559+
its index.
1560+
1561+
# Examples
1562+
1563+
```
1564+
# fn main() { async_std::task::block_on(async {
1565+
#
1566+
use async_std::prelude::*;
1567+
use std::collections::VecDeque;
1568+
1569+
let s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();
1570+
let res = s.clone().position(|x| *x == 1).await;
1571+
assert_eq!(res, Some(0));
1572+
1573+
let res = s.clone().position(|x| *x == 2).await;
1574+
assert_eq!(res, Some(1));
1575+
1576+
let res = s.clone().position(|x| *x == 3).await;
1577+
assert_eq!(res, Some(2));
1578+
1579+
let res = s.clone().position(|x| *x == 4).await;
1580+
assert_eq!(res, None);
1581+
#
1582+
# }) }
1583+
```
1584+
"#]
1585+
fn position<P>(
1586+
self,
1587+
predicate: P
1588+
) -> impl Future<Output = Option<usize>> [PositionFuture<Self, P>]
1589+
where
1590+
Self: Sized,
1591+
P: FnMut(&Self::Item) -> bool,
1592+
{
1593+
PositionFuture::new(self, predicate)
1594+
}
1595+
15551596
#[doc = r#"
15561597
Lexicographically compares the elements of this `Stream` with those
15571598
of another using 'Ord'.

src/stream/stream/position.rs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use std::pin::Pin;
2+
3+
use pin_project_lite::pin_project;
4+
5+
use crate::future::Future;
6+
use crate::stream::Stream;
7+
use crate::task::{Context, Poll};
8+
9+
pin_project! {
10+
#[doc(hidden)]
11+
#[allow(missing_debug_implementations)]
12+
pub struct PositionFuture<S, P> {
13+
#[pin]
14+
stream: S,
15+
predicate: P,
16+
index:usize,
17+
}
18+
}
19+
20+
impl<S, P> PositionFuture<S, P> {
21+
pub(super) fn new(stream: S, predicate: P) -> Self {
22+
PositionFuture {
23+
stream,
24+
predicate,
25+
index: 0,
26+
}
27+
}
28+
}
29+
30+
impl<S, P> Future for PositionFuture<S, P>
31+
where
32+
S: Stream,
33+
P: FnMut(&S::Item) -> bool,
34+
{
35+
type Output = Option<usize>;
36+
37+
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
38+
let this = self.project();
39+
let next = futures_core::ready!(this.stream.poll_next(cx));
40+
41+
match next {
42+
Some(v) if (this.predicate)(&v) => Poll::Ready(Some(*this.index)),
43+
Some(_) => {
44+
cx.waker().wake_by_ref();
45+
*this.index += 1;
46+
Poll::Pending
47+
}
48+
None => Poll::Ready(None),
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)