Skip to content

Commit

Permalink
Improve error messages on panic
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Jan 24, 2018
1 parent b6339f5 commit bda22fb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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<R> { inner: R }

impl<R: Rng> Rng for TestRng<R> {
Expand Down Expand Up @@ -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 };
Expand Down
3 changes: 2 additions & 1 deletion src/reseeding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ impl<R: Rng+SeedableRng, Rsdr: Rng> ReseedingRng<R, Rsdr> {
/// 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;
}
}
Expand Down

0 comments on commit bda22fb

Please sign in to comment.