diff --git a/Cargo.toml b/Cargo.toml index 6682f83f3..4d476b936 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ description = "A library for managing temporary files and directories." [dependencies] cfg-if = "1" -rand = { version = "0.8", features = ["small_rng", "getrandom"], default_features = false } +fastrand = "1.2.3" remove_dir_all = "0.5" [target.'cfg(any(unix, target_os = "wasi"))'.dependencies] diff --git a/src/util.rs b/src/util.rs index 01beeedea..8c91b9c69 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,32 +1,17 @@ -use rand::{self, Rng, SeedableRng}; -use rand::{distributions::Alphanumeric, rngs::SmallRng}; +use fastrand; use std::ffi::{OsStr, OsString}; -use std::thread_local; -use std::{ - cell::UnsafeCell, - path::{Path, PathBuf}, -}; -use std::{io, str}; +use std::path::{Path, PathBuf}; +use std::{io, iter::repeat_with}; use crate::error::IoResultExt; -thread_local! { - static THREAD_RNG: UnsafeCell = UnsafeCell::new(SmallRng::from_entropy()); -} - fn tmpname(prefix: &OsStr, suffix: &OsStr, rand_len: usize) -> OsString { 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. - 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]))) - }); + let mut char_buf = [0u8; 4]; + for c in repeat_with(fastrand::alphanumeric).take(rand_len) { + buf.push(c.encode_utf8(&mut char_buf)); + } buf.push(suffix); buf }