Skip to content

Commit

Permalink
Remove unnecessary Rc
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Apr 9, 2021
1 parent 42b28dd commit 622b817
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,28 @@ use std::thread_local;
use std::{
cell::UnsafeCell,
path::{Path, PathBuf},
rc::Rc,
};
use std::{io, str};

use crate::error::IoResultExt;

thread_local! {
static THREAD_RNG_KEY: Rc<UnsafeCell<SmallRng>> = Rc::new(UnsafeCell::new(SmallRng::from_entropy()));
static THREAD_RNG: UnsafeCell<SmallRng> = UnsafeCell::new(SmallRng::from_entropy());
}

fn tmpname(prefix: &OsStr, suffix: &OsStr, rand_len: usize) -> OsString {
let rng = THREAD_RNG_KEY.with(|t| t.clone());
let mut buf = OsString::with_capacity(prefix.len() + suffix.len() + rand_len);
buf.push(prefix);

// Push each character in one-by-one. Unfortunately, this is the only
// safe(ish) simple way to do this without allocating a temporary
// String/Vec.
unsafe {
THREAD_RNG.with(|rng| unsafe {
(&mut *rng.get())
.sample_iter(&Alphanumeric)
.take(rand_len)
.for_each(|b| buf.push(str::from_utf8_unchecked(&[b as u8])))
}
});
buf.push(suffix);
buf
}
Expand Down

0 comments on commit 622b817

Please sign in to comment.