Skip to content

Instantly share code, notes, and snippets.

Created July 11, 2017 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/9f6bd607f42b1b1e8cc7a2dab6e901ae to your computer and use it in GitHub Desktop.
Save anonymous/9f6bd607f42b1b1e8cc7a2dab6e901ae to your computer and use it in GitHub Desktop.
Rust code shared from the playground
extern crate futures;
use futures::{future, Async, Future, Poll};
struct State<T, F: FnMut(T) -> U, U: Future> {
elements: std::vec::IntoIter<T>,
func: F,
current_future: Option<U>,
errors: Vec<U::Error>,
}
impl<T, F: FnMut(T) -> U, U: Future> Future for State<T, F, U> {
type Item = U::Item;
type Error = Vec<U::Error>;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
let mut current_future = self.current_future.take();
if let Some(mut current_future) = current_future {
match current_future.poll() {
Ok(Async::NotReady) => {
self.current_future = Some(current_future);
Ok(Async::NotReady)
},
Ok(Async::Ready(v)) =>
return Ok(Async::Ready(v)),
Err(err) => {
self.errors.push(err);
self.poll()
},
}
}
else {
match self.elements.next() {
Some(v) => {
self.current_future = Some((self.func)(v));
self.poll()
},
None => {
let errors = ::std::mem::replace(&mut self.errors, vec![]);
Err(errors)
},
}
}
}
}
fn main() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment