Skip to main content

negation2/
negation2.rs

1use std::time::Duration;
2use waitforit::Wait;
3
4const CHECK_DURATION: Duration = Duration::from_secs(1);
5
6// This example shows how negations of `Wait` (and `Waits`) work.
7// It checks for existence/non-existence of a file named "something.lock",
8// which, for demonstration, we presume will never exist.
9fn main() {
10    let ten_sec = Wait::new_elapsed_from_duration(Duration::from_secs(10));
11    let lockfile = !Wait::new_file_exists("something.lock");
12
13    // wait until ten seconds has passed and the lockfile is gone
14    let w = ten_sec & lockfile;
15    let start = std::time::Instant::now();
16    w.wait(CHECK_DURATION);
17    println!("Step 1 complete after {:?}", start.elapsed());
18
19    // w      is     (ten seconds has passed) and (not(lockfile exists))
20    // not(w) is not((ten seconds has passed) and (not(lockfile exists)))
21    //        -> (not(ten seconds has passed) or (lockfile exists))
22    let ten_sec = Wait::new_elapsed_from_duration(Duration::from_secs(10));
23    let lockfile = !Wait::new_file_exists("something.lock");
24    let w = ten_sec & lockfile;
25    let not_w = !w;
26    let start = std::time::Instant::now();
27    not_w.wait(CHECK_DURATION);
28    println!("Step 2 complete after {:?}", start.elapsed());
29}