Skip to content

Commit ba6949a

Browse files
committed
Revert "Generalize "unfold" combinator."
This reverts commit 84a4cd0.
1 parent ca3f634 commit ba6949a

File tree

3 files changed

+24
-27
lines changed

3 files changed

+24
-27
lines changed

src/stream/unfold.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use stream::Stream;
1313
/// for the returned `Future` to complete with `(a, b)`. It will then yield the
1414
/// value `a`, and use `b` as the next internal state.
1515
///
16-
/// If the future returns `None` instead of `Some`, then the `unfold()`
16+
/// If the closure returns `None` instead of `Some(Future)`, then the `unfold()`
1717
/// will stop producing items and return `Ok(Async::Ready(None))` in future
1818
/// calls to `poll()`.
1919
///
@@ -33,23 +33,22 @@ use stream::Stream;
3333
/// use futures::future::{self, Future};
3434
///
3535
/// let mut stream = stream::unfold(0, |state| {
36-
/// future::ok::<_, u32>(
37-
/// if state <= 2 {
38-
/// let next_state = state + 1;
39-
/// let yielded = state * 2;
40-
/// Some((yielded, next_state))
41-
/// } else {
42-
/// None
43-
/// }
44-
/// )
36+
/// if state <= 2 {
37+
/// let next_state = state + 1;
38+
/// let yielded = state * 2;
39+
/// let fut = future::ok::<_, u32>((yielded, next_state));
40+
/// Some(fut)
41+
/// } else {
42+
/// None
43+
/// }
4544
/// });
4645
///
4746
/// let result = stream.collect().wait();
4847
/// assert_eq!(result, Ok(vec![0, 2, 4]));
4948
/// ```
5049
pub fn unfold<T, F, Fut, It>(init: T, f: F) -> Unfold<T, F, Fut>
51-
where F: FnMut(T) -> Fut,
52-
Fut: IntoFuture<Item = Option<(It, T)>>,
50+
where F: FnMut(T) -> Option<Fut>,
51+
Fut: IntoFuture<Item = (It, T)>,
5352
{
5453
Unfold {
5554
f: f,
@@ -68,8 +67,8 @@ pub struct Unfold<T, F, Fut> where Fut: IntoFuture {
6867
}
6968

7069
impl <T, F, Fut, It> Stream for Unfold<T, F, Fut>
71-
where F: FnMut(T) -> Fut,
72-
Fut: IntoFuture<Item = Option<(It, T)>>,
70+
where F: FnMut(T) -> Option<Fut>,
71+
Fut: IntoFuture<Item = (It, T)>,
7372
{
7473
type Item = It;
7574
type Error = Fut::Error;
@@ -80,18 +79,17 @@ impl <T, F, Fut, It> Stream for Unfold<T, F, Fut>
8079
// State::Empty may happen if the future returned an error
8180
State::Empty => { return Ok(Async::Ready(None)); }
8281
State::Ready(state) => {
83-
let fut = (self.f)(state);
84-
self.state = State::Processing(fut.into_future());
82+
match (self.f)(state) {
83+
Some(fut) => { self.state = State::Processing(fut.into_future()); }
84+
None => { return Ok(Async::Ready(None)); }
85+
}
8586
}
8687
State::Processing(mut fut) => {
8788
match fut.poll()? {
88-
Async::Ready(Some((item, next_state))) => {
89+
Async:: Ready((item, next_state)) => {
8990
self.state = State::Ready(next_state);
9091
return Ok(Async::Ready(Some(item)));
9192
}
92-
Async::Ready(None) => {
93-
return Ok(Async::Ready(None));
94-
}
9593
Async::NotReady => {
9694
self.state = State::Processing(fut);
9795
return Ok(Async::NotReady);

tests/unfold.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@ extern crate futures;
33
mod support;
44

55
use futures::stream;
6-
use futures::future::{ok, Either};
76

87
use support::*;
98

109
#[test]
1110
fn unfold1() {
1211
let mut stream = stream::unfold(0, |state| {
1312
if state <= 2 {
14-
let res = ok::<_, ()>(Some((state * 2, state + 1)));
15-
Either::A(delay_future(res))
13+
let res: Result<_,()> = Ok((state * 2, state + 1));
14+
Some(delay_future(res))
1615
} else {
17-
Either::B(ok(None))
16+
None
1817
}
1918
});
2019
// Creates the future with the closure
@@ -38,9 +37,9 @@ fn unfold1() {
3837
fn unfold_err1() {
3938
let mut stream = stream::unfold(0, |state| {
4039
if state <= 2 {
41-
Ok(Some((state * 2, state + 1)))
40+
Some(Ok((state * 2, state + 1)))
4241
} else {
43-
Err(-1)
42+
Some(Err(-1))
4443
}
4544
});
4645
sassert_next(&mut stream, 0);

tests/unsync.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ fn mpsc_send_unpark() {
139139
#[test]
140140
fn spawn_sends_items() {
141141
let core = Core::new();
142-
let stream = unfold(0, |i| Ok::<_,u8>(Some((i, i + 1))));
142+
let stream = unfold(0, |i| Some(Ok::<_,u8>((i, i + 1))));
143143
let rx = mpsc::spawn(stream, &core, 1);
144144
assert_eq!(core.run(rx.take(4).collect()).unwrap(),
145145
[0, 1, 2, 3]);

0 commit comments

Comments
 (0)