Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use futures::{prelude::*, future, stream};
- use tokio::timer::Delay;
- use std::time::{Duration, Instant};
- fn upload(val: u32) -> impl Future<Item = u32, Error = &'static str> {
- println!("uploading {}", val);
- // simulating 100ms upload time
- Delay::new(Instant::now() + Duration::from_millis(100))
- .map_err(|_| "delay failed")
- .and_then(move |_| {
- if val == 20 {
- future::err("val failed")
- } else {
- future::ok(val)
- }
- })
- }
- fn do_stuffs(vals: impl Iterator<Item = u32>) -> impl Future<Item = (), Error = &'static str> {
- stream::iter_ok(vals)
- .and_then(upload)
- .for_each(|val| {
- println!("Done uploading {}", val);
- Ok(())
- })
- }
- fn main() {
- let uploads = do_stuffs(0..42);
- tokio::run(uploads);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement