From bda22fb366c0b8a1f5fea29500ba815cded8cabd Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Wed, 24 Jan 2018 11:19:31 +0000 Subject: [PATCH] Improve error messages on panic --- src/lib.rs | 15 +++++++++++---- src/reseeding.rs | 3 ++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6fe45bc018..691fa64a7d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -941,7 +941,7 @@ thread_local!( const THREAD_RNG_RESEED_THRESHOLD: u64 = 32_768; let mut entropy_source = EntropyRng::new(); let r = StdRng::from_rng(&mut entropy_source) - .expect("could not initialize thread_rng"); + .unwrap_or_else(|err| panic!("could not initialize thread_rng: {}", err)); let rng = ReseedingRng::new(r, THREAD_RNG_RESEED_THRESHOLD, entropy_source); @@ -1031,7 +1031,8 @@ impl Rng for EntropyRng { } fn fill_bytes(&mut self, dest: &mut [u8]) { - self.try_fill_bytes(dest).unwrap(); + self.try_fill_bytes(dest).unwrap_or_else(|err| + panic!("all entropy sources failed; first error: {}", err)) } fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { @@ -1170,10 +1171,10 @@ mod test { use impls; #[cfg(feature="std")] use super::{random, thread_rng}; - use super::{Rng, SeedableRng, StdRng}; + use super::{Rng, SeedableRng, StdRng, EntropyRng}; #[cfg(feature="alloc")] use alloc::boxed::Box; - + pub struct TestRng { inner: R } impl Rng for TestRng { @@ -1216,6 +1217,12 @@ mod test { } } + #[test] + fn test_entropy() { + let mut rng = EntropyRng::new(); + rng.next_u32(); + } + #[test] fn test_fill_bytes_default() { let mut r = ConstRng { i: 0x11_22_33_44_55_66_77_88 }; diff --git a/src/reseeding.rs b/src/reseeding.rs index 008b7a5130..3245cff806 100644 --- a/src/reseeding.rs +++ b/src/reseeding.rs @@ -45,7 +45,8 @@ impl ReseedingRng { /// generated exceed the threshold. pub fn reseed_if_necessary(&mut self) { if self.bytes_generated >= self.generation_threshold { - R::from_rng(&mut self.reseeder).map(|result| self.rng = result).unwrap(); + R::from_rng(&mut self.reseeder).map(|result| self.rng = result) + .unwrap_or_else(|err| panic!("reseeding failed: {}", err)); self.bytes_generated = 0; } }