1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
use std::pin::Pin; use crate::future::Future; use crate::stream::Stream; use crate::task::{Context, Poll}; #[doc(hidden)] #[allow(missing_debug_implementations)] pub struct TryFoldFuture<'a, S, F, T> { stream: &'a mut S, f: F, acc: Option<T>, } impl<'a, S, F, T> Unpin for TryFoldFuture<'a, S, F, T> {} impl<'a, S, F, T> TryFoldFuture<'a, S, F, T> { pub(super) fn new(stream: &'a mut S, init: T, f: F) -> Self { Self { stream, f, acc: Some(init), } } } impl<'a, S, F, T, E> Future for TryFoldFuture<'a, S, F, T> where S: Stream + Unpin, F: FnMut(T, S::Item) -> Result<T, E>, { type Output = Result<T, E>; fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { loop { let next = futures_core::ready!(Pin::new(&mut self.stream).poll_next(cx)); match next { Some(v) => { let old = self.acc.take().unwrap(); let new = (&mut self.f)(old, v); match new { Ok(o) => self.acc = Some(o), Err(e) => return Poll::Ready(Err(e)), } } None => return Poll::Ready(Ok(self.acc.take().unwrap())), } } } }