Skip to content

Commit 64216b8

Browse files
committed
Take a normal closure, not an async one
1 parent 786a52a commit 64216b8

File tree

1 file changed

+12
-39
lines changed

1 file changed

+12
-39
lines changed

src/stream/successors.rs

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use std::pin::Pin;
22
use std::mem;
33

4-
use crate::future::Future;
54
use crate::stream::Stream;
6-
use crate::task::{Context, Poll, ready};
5+
use crate::task::{Context, Poll};
76

87
use pin_project_lite::pin_project;
98

@@ -18,42 +17,26 @@ use pin_project_lite::pin_project;
1817
/// use async_std::prelude::*;
1918
/// use async_std::stream;
2019
///
21-
/// let s = stream::successors(Some(22), |&val| {
22-
/// async move {
23-
/// Some(val + 1)
24-
/// }
25-
/// });
20+
/// let s = stream::successors(Some(22), |&val| Some(val + 1) );
2621
///
2722
/// pin_utils::pin_mut!(s);
2823
/// assert_eq!(s.next().await, Some(22));
2924
/// assert_eq!(s.next().await, Some(23));
3025
/// assert_eq!(s.next().await, Some(24));
3126
/// assert_eq!(s.next().await, Some(25));
3227
///
33-
///
34-
///let never = stream::successors(None, |_| {
35-
/// async move {
36-
/// Some(1)
37-
/// }
38-
/// });
39-
///
40-
/// pin_utils::pin_mut!(never);
41-
/// assert_eq!(never.next().await, None);
42-
/// assert_eq!(never.next().await, None);
4328
/// #
4429
/// # }) }
4530
///
4631
/// ```
4732
#[cfg(feature = "unstable")]
4833
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
49-
pub fn successors<F, Fut, T>(first: Option<T>, succ: F) -> Successors<F, Fut, T>
34+
pub fn successors<F, T>(first: Option<T>, succ: F) -> Successors<F, T>
5035
where
51-
F: FnMut(&T) -> Fut,
52-
Fut: Future<Output = Option<T>>,
36+
F: FnMut(&T) -> Option<T>,
5337
{
5438
Successors {
55-
succ: succ,
56-
future: None,
39+
succ,
5740
slot: first,
5841
}
5942
}
@@ -68,39 +51,29 @@ pin_project! {
6851
#[cfg(feature = "unstable")]
6952
#[cfg_attr(feature = "docs", doc(cfg(unstable)))]
7053
#[derive(Debug)]
71-
pub struct Successors<F, Fut, T>
54+
pub struct Successors<F, T>
7255
where
73-
Fut: Future<Output = Option<T>>,
56+
F: FnMut(&T) -> Option<T>
7457
{
7558
succ: F,
76-
#[pin]
77-
future: Option<Fut>,
7859
slot: Option<T>,
7960
}
8061
}
8162

82-
impl<F, Fut, T> Stream for Successors<F, Fut, T>
63+
impl<F, T> Stream for Successors<F, T>
8364
where
84-
Fut: Future<Output = Option<T>>,
85-
F: FnMut(&T) -> Fut,
65+
F: FnMut(&T) -> Option<T>,
8666
{
8767
type Item = T;
8868

89-
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
90-
let mut this = self.project();
69+
fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
70+
let this = self.project();
9171

9272
if this.slot.is_none() {
9373
return Poll::Ready(None);
9474
}
9575

96-
if this.future.is_none() {
97-
let fut = (this.succ)(this.slot.as_ref().unwrap());
98-
this.future.set(Some(fut));
99-
}
100-
101-
let mut next = ready!(this.future.as_mut().as_pin_mut().unwrap().poll(cx));
102-
103-
this.future.set(None);
76+
let mut next = (this.succ)(&this.slot.as_ref().unwrap());
10477

10578
// 'swapping' here means 'slot' will hold the next value and next will be th one from the previous iteration
10679
mem::swap(this.slot, &mut next);

0 commit comments

Comments
 (0)