Skip to content

Replace KeyMaterial .allow_hazardous_operations() with a closure-based solution #39

Description

@ounsworth

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(())
        })?;

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestrefactorThis task is primarily about performing a code refactor
No fields configured for Feature.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions