use std::path::{Path, PathBuf};
pub(crate) struct TempDir(PathBuf);
impl TempDir {
pub(crate) fn new(tag: &str) -> Self {
let unique = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|since| since.as_nanos())
.unwrap_or(0);
let path = std::env::temp_dir().join(format!("rudb-io-{tag}-{unique}"));
std::fs::create_dir_all(&path).expect("could not make a temporary directory");
Self(path)
}
pub(crate) fn join(&self, name: &str) -> PathBuf {
self.0.join(name)
}
#[allow(dead_code, reason = "only the Linux tests in machine.rs want the root on its own")]
pub(crate) fn path(&self) -> &Path {
&self.0
}
}
impl Drop for TempDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}