waitforit 0.1.0

A library to aid in synchronously waiting for some condition to be met.
Documentation
  • Coverage
  • 31.71%
    13 out of 41 items documented0 out of 0 items with examples
  • Size
  • Source code size: 67.72 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 745.97 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 33s Average build duration of successful builds.
  • all releases: 33s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • aeshirey/waitforit
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • aeshirey

waitforit

From my waitfor app¹, this crate extracts out the functionality to wait for certain events, including:

  • elapsed time
  • file (non-)existence
  • file updates (timestamp or file size)
  • TCP host:port (un)availablity
  • HTTP GET response codes
  • Arbitrary user-defined (fn() -> bool)

Usage

waitforit exposes the Wait and Waits structs. The former is some condition (eg, as above) that the user wants to wait to complete. The latter is simply a combination other conditions. Both structs expose two methods for checking their conditions:

  • .condition_met() -> bool checks and (nearly immediately) returns whether the condition is met.
  • .wait(interval: Duration) blocks until .condition_met() is true, checking every interval

All Wait conditions can be ! negated (or, when manually constructed from the Wait enum's variant's, by specifying a not:bool parameter). For example, the Wait::Exists variant checks for the existence of a file (ie, .wait will block until that file exists). When negated, it is satisfied when the file doesn't exist.

Crate Features

By default, this crate includes the ureq and url crates to support making HTTP requests. This increases the number of dependencies and compile time, so if you wish to disable these, you can do so in your Cargo.toml:

waitforit = { version = "0.1.0", default_features = false }

Negations

Any Wait or Waits value can be negated:

let foo_exists = Wait::new_file_exists("foo.txt");
let foo_doesnt_exist = !foo_exists;

This even applies to the Elapsed variant which means the condition will be met until the elapsed duration. This may prove useful when combining with other values. For example, wait for a file to be updated in the first ten seconds, and if that doesn't happen, then wait for the file to be deleted:

let filename = "my_dataset.json"; // assumed that this file exists
let first_10sec = !Wait::new_elapsed_from_duration(Duration::from_secs(10));
let file_updated = Wait::new_file_update(filename);
let file_not_exists = !Wait::new_file_exists(filename);

// either we update the file in the first 10 sec or we wait for it to be deleted
let w = (first_10sec & file_updated) | file_not_exists;
w.wait(Duration::from_secs(1));

TODO

  • Expand the Custom variant to be Fn or possibly FnMut?
  • Monitor running processes (possibly with sysinfo crate?)