Skip to content

Commit 665f36b

Browse files
authored
crypto-common: use UnwrapErr in Generate (#2234)
Replaces `expect` with the `UnwrapErr` type
1 parent 67b0cd6 commit 665f36b

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

crypto-common/src/generate.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use hybrid_array::{Array, ArraySize};
22
use rand_core::{CryptoRng, TryCryptoRng};
33

4+
#[cfg(feature = "getrandom")]
5+
use getrandom::{SysRng, rand_core::UnwrapErr};
6+
47
/// Secure random generation.
58
pub trait Generate: Sized {
69
/// Generate random key using the provided [`TryCryptoRng`].
@@ -20,7 +23,7 @@ pub trait Generate: Sized {
2023
/// failure.
2124
#[cfg(feature = "getrandom")]
2225
fn try_generate() -> Result<Self, getrandom::Error> {
23-
Self::try_generate_from_rng(&mut getrandom::SysRng)
26+
Self::try_generate_from_rng(&mut SysRng)
2427
}
2528

2629
/// Randomly generate a value of this type using the system's ambient cryptographically secure
@@ -33,7 +36,7 @@ pub trait Generate: Sized {
3336
/// This shouldn't happen on most modern operating systems.
3437
#[cfg(feature = "getrandom")]
3538
fn generate() -> Self {
36-
Self::try_generate().expect("RNG failure")
39+
Self::generate_from_rng(&mut UnwrapErr(SysRng))
3740
}
3841
}
3942

0 commit comments

Comments
 (0)