Skip to content

Instantly share code, notes, and snippets.

@nanpuyue
Created April 9, 2019 05:16
Show Gist options
  • Save nanpuyue/74067df0284c21f987a07fc0d66f0e06 to your computer and use it in GitHub Desktop.
Save nanpuyue/74067df0284c21f987a07fc0d66f0e06 to your computer and use it in GitHub Desktop.
// file: select_delay.rs
// date: 2019-04-09
// license: GPLv3 https://www.gnu.org/licenses/gpl-3.0.txt
// author: nanpuyue <[email protected]> https://blog.nanpuyue.com
use std::time::{Duration, Instant};
use futures::Future;
use tokio::timer::Delay;
fn select_delay(timeout1: u64, timeout2: u64) {
let delay1 = Delay::new(Instant::now() + Duration::from_secs(timeout1));
let delay2 = Delay::new(Instant::now() + Duration::from_secs(timeout2));
let start = Instant::now();
let task = delay1.select(delay2).and_then(move |_| {
println!("timeout: {}", (Instant::now() - start).as_secs());
Ok(())
});
tokio::run(task.map(drop).map_err(drop));
}
fn main() {
select_delay(1, 2);
select_delay(2, 1);
select_delay(3, 4);
select_delay(4, 2);
}
@nanpuyue
Copy link
Author

nanpuyue commented Apr 9, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment