futures_util/stream/
poll_fn.rs
1use super::assert_stream;
4use core::fmt;
5use core::pin::Pin;
6use futures_core::stream::Stream;
7use futures_core::task::{Context, Poll};
8
9#[must_use = "streams do nothing unless polled"]
11pub struct PollFn<F> {
12 f: F,
13}
14
15impl<F> Unpin for PollFn<F> {}
16
17impl<F> fmt::Debug for PollFn<F> {
18 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19 f.debug_struct("PollFn").finish()
20 }
21}
22
23pub fn poll_fn<T, F>(f: F) -> PollFn<F>
42where
43 F: FnMut(&mut Context<'_>) -> Poll<Option<T>>,
44{
45 assert_stream::<T, _>(PollFn { f })
46}
47
48impl<T, F> Stream for PollFn<F>
49where
50 F: FnMut(&mut Context<'_>) -> Poll<Option<T>>,
51{
52 type Item = T;
53
54 fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<T>> {
55 (&mut self.f)(cx)
56 }
57}