Created
April 9, 2019 05:16
-
-
Save nanpuyue/74067df0284c21f987a07fc0d66f0e06 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // 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); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=74067df0284c21f987a07fc0d66f0e06