Created
July 11, 2017 06:27
-
-
Save anonymous/9f6bd607f42b1b1e8cc7a2dab6e901ae to your computer and use it in GitHub Desktop.
Rust code shared from the playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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