Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. use futures::{prelude::*, future, stream};
  2. use tokio::timer::Delay;
  3. use std::time::{Duration, Instant};
  4.  
  5. fn upload(val: u32) -> impl Future<Item = u32, Error = &'static str> {
  6. println!("uploading {}", val);
  7.  
  8. // simulating 100ms upload time
  9. Delay::new(Instant::now() + Duration::from_millis(100))
  10. .map_err(|_| "delay failed")
  11. .and_then(move |_| {
  12. if val == 20 {
  13. future::err("val failed")
  14. } else {
  15. future::ok(val)
  16. }
  17. })
  18. }
  19.  
  20. fn do_stuffs(vals: impl Iterator<Item = u32>) -> impl Future<Item = (), Error = &'static str> {
  21. stream::iter_ok(vals)
  22. .and_then(upload)
  23. .for_each(|val| {
  24. println!("Done uploading {}", val);
  25. Ok(())
  26. })
  27. }
  28.  
  29. fn main() {
  30. let uploads = do_stuffs(0..42);
  31.  
  32. tokio::run(uploads);
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement