Skip to content

Implement stream::poll_fn #545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ mod merge;
mod once;
mod or_else;
mod peek;
mod poll_fn;
mod select;
mod skip;
mod skip_while;
Expand Down Expand Up @@ -70,6 +71,7 @@ pub use self::merge::{Merge, MergedItem};
pub use self::once::{Once, once};
pub use self::or_else::OrElse;
pub use self::peek::Peekable;
pub use self::poll_fn::{poll_fn, PollFn};
pub use self::select::Select;
pub use self::skip::Skip;
pub use self::skip_while::SkipWhile;
Expand Down
49 changes: 49 additions & 0 deletions src/stream/poll_fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//! Definition of the `PollFn` combinator

use {Stream, Poll};

/// A stream which adapts a function returning `Poll`.
///
/// Created by the `poll_fn` function.
#[derive(Debug)]
#[must_use = "streams do nothing unless polled"]
pub struct PollFn<F> {
inner: F,
}

/// Creates a new stream wrapping around a function returning `Poll`.
///
/// Polling the returned stream delegates to the wrapped function.
///
/// # Examples
///
/// ```
/// use futures::stream::poll_fn;
/// use futures::{Async, Poll};
///
/// 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())))
/// });
/// ```
pub fn poll_fn<T, E, F>(f: F) -> PollFn<F>
where
F: FnMut() -> Poll<Option<T>, E>,
{
PollFn { inner: f }
}

impl<T, E, F> Stream for PollFn<F>
where
F: FnMut() -> Poll<Option<T>, E>,
{
type Item = T;
type Error = E;

fn poll(&mut self) -> Poll<Option<T>, E> {
(self.inner)()
}
}
21 changes: 18 additions & 3 deletions tests/stream.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#[macro_use]
extern crate futures;

use futures::{Poll, Future, Stream, Sink};
use futures::{Async, Future, Poll, Sink, Stream};
use futures::executor;
use futures::future::{ok, err};
use futures::stream::{empty, iter, Peekable, BoxStream};
use futures::future::{err, ok};
use futures::stream::{empty, iter, poll_fn, BoxStream, Peekable};
use futures::sync::oneshot;
use futures::sync::mpsc;

Expand Down Expand Up @@ -356,3 +356,18 @@ fn concat2() {
let c = empty::<Vec<()>, ()>();
assert_done(move || c.concat2(), Ok(vec![]))
}

#[test]
fn stream_poll_fn() {
let mut counter = 5usize;

let read_stream = poll_fn(move || -> Poll<Option<usize>, std::io::Error> {
if counter == 0 {
return Ok(Async::Ready(None));
}
counter -= 1;
Ok(Async::Ready(Some(counter)))
});

assert_eq!(read_stream.wait().count(), 5);
}