use core::fmt; use core::pin::Pin; use futures_core::future::{FusedFuture, Future}; use futures_core::ready; use futures_core::stream::Stream; use futures_core::task::{Context, Poll}; use pin_project_lite::pin_project;
pin_project! { /// Future for the [`fold`](super::StreamExt::fold) method. #[must_use = "futures do nothing unless you `.await` or poll them"] pubstruct Fold<St, Fut, T, F> { #[pin]
stream: St,
f: F,
accum: Option<T>, #[pin]
future: Option<Fut>,
}
}
impl<St, Fut, T, F> Future for Fold<St, Fut, T, F> where
St: Stream,
F: FnMut(T, St::Item) -> Fut,
Fut: Future<Output = T>,
{ type Output = T;
fn poll(self: Pin<&mutSelf>, cx: &mut Context<'_>) -> Poll<T> { letmut this = self.project();
Poll::Ready(loop { iflet Some(fut) = this.future.as_mut().as_pin_mut() { // we're currently processing a future to produce a new accum value
*this.accum = Some(ready!(fut.poll(cx)));
this.future.set(None);
} elseif this.accum.is_some() { // we're waiting on a new item from the stream let res = ready!(this.stream.as_mut().poll_next(cx)); let a = this.accum.take().unwrap(); iflet Some(item) = res {
this.future.set(Some((this.f)(a, item)));
} else { break a;
}
} else {
panic!("Fold polled after completion")
}
})
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.10 Sekunden
(vorverarbeitet am 2026-06-18)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.