use rand::{distributions::Alphanumeric, prelude::*};
use std::cell::UnsafeCell;
use std::ffi::{OsStr, OsString};
use std::path;
use std::str;
std::thread_local! {
static RNG: UnsafeCell<SmallRng> = UnsafeCell::new(SmallRng::from_entropy());
}
fn tmpname(prefix: &OsStr, rand_len: usize) -> OsString {
let mut buf = OsString::with_capacity(prefix.len() + rand_len);
buf.push(prefix);
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
}
pub(crate) fn tmppath_in(dir: &path::Path) -> path::PathBuf {
const LEN: usize = 10;
let mut buf = path::PathBuf::new();
buf.push(dir);
buf.push(&tmpname(OsStr::new("tmp"), LEN));
buf
}