#![allow(dead_code)]
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
static COUNTER: AtomicU64 = AtomicU64::new(0);
pub struct TempDir(pub PathBuf);
impl Drop for TempDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
impl std::ops::Deref for TempDir {
type Target = Path;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl AsRef<Path> for TempDir {
fn as_ref(&self) -> &Path {
&self.0
}
}
impl TempDir {
pub fn new(prefix: &str) -> Self {
let n = COUNTER.fetch_add(1, Ordering::SeqCst);
let path = std::env::temp_dir().join(format!("{prefix}-{}-{n}", std::process::id()));
std::fs::create_dir_all(&path).expect("create temp dir");
Self(path)
}
pub fn with_tag(prefix: &str, tag: &str) -> Self {
let n = COUNTER.fetch_add(1, Ordering::SeqCst);
let path = std::env::temp_dir().join(format!("{prefix}-{tag}-{}-{n}", std::process::id()));
std::fs::create_dir_all(&path).expect("create temp dir");
Self(path)
}
}