| 1 | use core::pin::Pin; |
| 2 | |
| 3 | use pin_project_lite::pin_project; |
| 4 | |
| 5 | use crate::stream::Stream; |
| 6 | use crate::task::{Context, Poll}; |
| 7 | |
| 8 | pin_project! { |
| 9 | /// A stream to maintain state while polling another stream. |
| 10 | /// |
| 11 | /// This `struct` is created by the [`scan`] method on [`Stream`]. See its |
| 12 | /// documentation for more. |
| 13 | /// |
| 14 | /// [`scan`]: trait.Stream.html#method.scan |
| 15 | /// [`Stream`]: trait.Stream.html |
| 16 | #[derive (Debug)] |
| 17 | pub struct Scan<S, St, F> { |
| 18 | #[pin] |
| 19 | stream: S, |
| 20 | state_f: (St, F), |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | impl<S, St, F> Scan<S, St, F> { |
| 25 | pub(crate) fn new(stream: S, initial_state: St, f: F) -> Self { |
| 26 | Self { |
| 27 | stream, |
| 28 | state_f: (initial_state, f), |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | impl<S, St, F, B> Stream for Scan<S, St, F> |
| 34 | where |
| 35 | S: Stream, |
| 36 | F: FnMut(&mut St, S::Item) -> Option<B>, |
| 37 | { |
| 38 | type Item = B; |
| 39 | |
| 40 | fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<B>> { |
| 41 | let mut this: Projection<'_, S, St, F> = self.project(); |
| 42 | let poll_result: Poll = this.stream.as_mut().poll_next(cx); |
| 43 | poll_result.map(|item: Option<::Item>| { |
| 44 | item.and_then(|item: ::Item| { |
| 45 | let (state: &mut St, f: &mut F) = this.state_f; |
| 46 | f(state, item) |
| 47 | }) |
| 48 | }) |
| 49 | } |
| 50 | } |
| 51 | |