Structures for acquiring guards based on a keyed name.
Acquire many distinct guards based on a keyed name.
This means that many RAII guards can be given out for different keys, but only a single instance for the given key can be inflight at once.
For example
let s = InflightSet::new();
let guard = s.acquire("job_id_123").expect("known unique key");
let guard_two = s.acquire("job_id_567").expect("known unique key");
// do things
// Would error! Attempting to acquire guard for a pre-existing key.
let another_guard = s.acquire("job_id_123")?;
drop(guard); // key=job_id_123
let guard = s.acquire("job_id_123").expect("RAII guard dropped, this is okay");
// When guards go out of scope, the key is released freeing it for later use.Another valid use case is for when you do want to allow multiple occurences of the same key to be given out, but up to a maximum.
This is the use case for the CountedInflightSet.
let c = CountedInflightSet::new();
let key = "test-key";
let max_guards = 2;
// Keys must be initialised, in order for the maximum number
// of guards that are permitted to be known.
c.initialise_key(key, max_guards)
.expect("unique key for test");
// Acquire the same key twice, up to `max_guards`
let guard_one = c.acquire(key).unwrap();
let guard_two = c.acquire(key).unwrap();
// This is going to error, as the maximum number of guards (2)
// has been exceeded.
//
// One of the guards must been dropped first, before this
// would be a valid operation.
c.acquire(key)?;