diff --git a/src/util.rs b/src/util.rs index fda76762a..01beeedea 100644 --- a/src/util.rs +++ b/src/util.rs @@ -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> = Rc::new(UnsafeCell::new(SmallRng::from_entropy())); + static THREAD_RNG: UnsafeCell = 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 }