Skip to content

Commit 48c82a9

Browse files
committed
Add stream position
1 parent cc949f4 commit 48c82a9

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
@@ -46,6 +46,7 @@ mod min_by_key;
4646
mod next;
4747
mod nth;
4848
mod partial_cmp;
49+
mod position;
4950
mod scan;
5051
mod skip;
5152
mod skip_while;
@@ -76,6 +77,7 @@ use min_by_key::MinByKeyFuture;
7677
use next::NextFuture;
7778
use nth::NthFuture;
7879
use partial_cmp::PartialCmpFuture;
80+
use position::PositionFuture;
7981
use try_fold::TryFoldFuture;
8082
use try_for_each::TryForEeachFuture;
8183

@@ -1548,6 +1550,45 @@ extension_trait! {
15481550
PartialCmpFuture::new(self, other)
15491551
}
15501552

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