652 B
652 B
Solution 1: Runtime Checks
One solution to the problem would be to simply check the locked state and restrict methods to be only callable in the corrent locked state.
let mut pw = CredentialManager::new();
pw.set("foo", "bar")?; // this would bail with an error result
pw.unlock("pssst")?;
pw.unlock("pssst")?; // this would bail with an error result
pw.set("foo", "bar")?;
let val = pw.get("foo")?;
pw.lock()?;
pw.lock()?; // this would bail with an error result
Caveats
Performing runtime checks on the state requires returning Result and also requires the check and handling of erroneous results on every call to the manager.