1use cancellable_timer::*;
2use std::io;
3use std::time::Duration;
4
5fn main() {
6 let (mut timer, canceller) = Timer::new2().unwrap();
7
8 println!("Wait 2s, uninterrupted.");
9 let r = timer.sleep(Duration::from_secs(2));
10 println!("Done: {:?}", r);
11
12 println!("Wait 2s, cancelled at once.");
13 canceller.cancel().unwrap();
14 let r = timer.sleep(Duration::from_secs(2));
15 println!("Done {:?}", r);
16
17 println!("Wait 2s, not cancelled.");
18 let r = timer.sleep(Duration::from_secs(2));
19 println!("Done {:?}", r);
20
21 println!("Wait 10s, cancel after 2s");
22 let canceller2 = canceller.clone();
23 std::thread::spawn(move || {
24 std::thread::sleep(Duration::from_secs(2));
25 canceller2.cancel().unwrap();
26 });
27 match timer.sleep(Duration::from_secs(10)) {
28 Err(ref e) if e.kind() == io::ErrorKind::Interrupted => println!("Cancelled"),
29 x => panic!("{:?}", x),
30 };
31}