Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ThreadRng: remove usage of Rc #615

Merged
merged 1 commit into from Sep 24, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/rngs/thread.rs
Expand Up @@ -9,7 +9,6 @@
//! Thread-local random number generator

use std::cell::UnsafeCell;
use std::rc::Rc;

use {RngCore, CryptoRng, SeedableRng, Error};
use rngs::adapter::ReseedingRng;
Expand Down Expand Up @@ -72,18 +71,19 @@ const THREAD_RNG_RESEED_THRESHOLD: u64 = 32*1024*1024; // 32 MiB
/// [HC-128]: ../prng/hc128/struct.Hc128Rng.html
#[derive(Clone, Debug)]
pub struct ThreadRng {
rng: Rc<UnsafeCell<ReseedingRng<Hc128Core, EntropyRng>>>,
// use of raw pointer implies type is neither Send nor Sync
rng: *mut ReseedingRng<Hc128Core, EntropyRng>,
}

thread_local!(
static THREAD_RNG_KEY: Rc<UnsafeCell<ReseedingRng<Hc128Core, EntropyRng>>> = {
static THREAD_RNG_KEY: UnsafeCell<ReseedingRng<Hc128Core, EntropyRng>> = {
let mut entropy_source = EntropyRng::new();
let r = Hc128Core::from_rng(&mut entropy_source).unwrap_or_else(|err|
panic!("could not initialize thread_rng: {}", err));
let rng = ReseedingRng::new(r,
THREAD_RNG_RESEED_THRESHOLD,
entropy_source);
Rc::new(UnsafeCell::new(rng))
UnsafeCell::new(rng)
}
);

Expand All @@ -96,26 +96,26 @@ thread_local!(
///
/// [`ThreadRng`]: rngs/struct.ThreadRng.html
pub fn thread_rng() -> ThreadRng {
ThreadRng { rng: THREAD_RNG_KEY.with(|t| t.clone()) }
ThreadRng { rng: THREAD_RNG_KEY.with(|t| t.get()) }
}

impl RngCore for ThreadRng {
#[inline(always)]
fn next_u32(&mut self) -> u32 {
unsafe { (*self.rng.get()).next_u32() }
unsafe { (*self.rng).next_u32() }
}

#[inline(always)]
fn next_u64(&mut self) -> u64 {
unsafe { (*self.rng.get()).next_u64() }
unsafe { (*self.rng).next_u64() }
}

fn fill_bytes(&mut self, dest: &mut [u8]) {
unsafe { (*self.rng.get()).fill_bytes(dest) }
unsafe { (*self.rng).fill_bytes(dest) }
}

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
unsafe { (*self.rng.get()).try_fill_bytes(dest) }
unsafe { (*self.rng).try_fill_bytes(dest) }
}
}

Expand Down