Skip to content

Commit

Permalink
Implement Default for OsRng, JitterRng and ThreadRng
Browse files Browse the repository at this point in the history
Also implement `ThreadRng::new`.

Refs rust-random#378.
  • Loading branch information
vks committed Apr 10, 2018
1 parent 628a952 commit c7cb0a0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/jitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,12 @@ impl JitterRng {
}
}

impl Default for JitterRng {
fn default() -> JitterRng {
JitterRng::new().expect("failed to initialize `JitterRng`")
}
}

#[cfg(feature="std")]
mod platform {
#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "windows",
Expand Down
6 changes: 6 additions & 0 deletions src/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ impl OsRng {
}
}

impl Default for OsRng {
fn default() -> OsRng {
OsRng::new().expect("failed to initialize `OsRng`")
}
}

impl RngCore for OsRng {
fn next_u32(&mut self) -> u32 {
impls::next_u32_via_fill(self)
Expand Down
15 changes: 15 additions & 0 deletions src/thread_rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ pub fn thread_rng() -> ThreadRng {
ThreadRng { rng: THREAD_RNG_KEY.with(|t| t.clone()) }
}

impl ThreadRng {
/// See [`thread_rng`].
///
/// [`thread_rng`]: fn.thread_rng.html
pub fn new() -> ThreadRng {
thread_rng()
}
}

impl Default for ThreadRng {
fn default() -> ThreadRng {
ThreadRng::new()
}
}

impl RngCore for ThreadRng {
#[inline(always)]
fn next_u32(&mut self) -> u32 {
Expand Down

0 comments on commit c7cb0a0

Please sign in to comment.