futures_util::stream

Function poll_fn

Source
pub fn poll_fn<T, E, F>(f: F) -> PollFn<F>
where F: FnMut(&mut Context<'_>) -> Poll<Option<T>, E>,
Expand description

Creates a new stream wrapping around a function returning Poll.

Polling the returned stream delegates to the wrapped function.

ยงExamples

use futures::prelude::*;
use futures::stream::poll_fn;

let mut counter = 1usize;

let read_stream = poll_fn(move |_| -> Poll<Option<String>, std::io::Error> {
    if counter == 0 { return Ok(Async::Ready(None)); }
    counter -= 1;
    Ok(Async::Ready(Some("Hello, World!".to_owned())))
});