Basic idea:
fn do_hazardous_operations<F>(&mut self, mut f: F) -> Result<(), KeyMaterialError>
where
F: FnMut(&mut Self) -> Result<(), KeyMaterialError>,
Self: Sized,
{
self.allow_hazardous_operations();
let ret = f(self);
self.drop_hazardous_operations();
ret
}
}
And then applications can call it like:
key.do_hazardous_operations(|key| {
rng.next_bytes_out(&mut key.mut_ref_to_bytes().unwrap())
.map_err(|_| KeyMaterialError::GenericError("RNG failed."))?;
key.key_len = KEY_LEN;
key.key_type = KeyType::BytesFullEntropy;
key.security_strength = rng.security_strength();
Ok(())
})?;
Basic idea:
And then applications can call it like: