diff --git a/src/stream.rs b/src/stream.rs index 80e6264..5bd7f0b 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -30,10 +30,17 @@ pub fn map(stream: St, f: F) -> impl Stream where St: Stream, F: FnMut(St::Item) -> U, { + // stream::unfold accepts the initial state and a closure accepting the state + // the closure returns Future(None) as a terminate value + // to produce a value for the stream the closure returns Future( Some(value, state) ) + + // So State is a named struct to be destructured in the closure arg to improve readability + struct State { stream: Pin>, f: F } + let stream = Box::pin(stream); - futures::stream::unfold((stream, f), async move | (mut stream, mut f)| { - let item = await!(next(&mut stream)); - item.map(|item| (f(item), (stream, f))) + futures::stream::unfold(State { stream, f }, async move |State { mut stream, mut f }| { + let item : Option = await!(next(&mut stream)); + item.map(|item| (f(item), State { stream, f })) }) }