use std::{ path::{Path, PathBuf}, sync::atomic::{AtomicU64, Ordering}, time::{SystemTime, UNIX_EPOCH}, }; static NEXT_TEMP_ID: AtomicU64 = AtomicU64::new(0); pub(crate) struct TempDir(PathBuf); impl TempDir { pub(crate) fn new(prefix: &str) -> Self { let mut path = std::env::temp_dir(); let unique_id = NEXT_TEMP_ID.fetch_add(1, Ordering::Relaxed); path.push(format!( "{prefix}-{}-{}-{}", std::process::id(), unique_id, SystemTime::now() .duration_since(UNIX_EPOCH) .unwrap_or_default() .as_nanos() )); std::fs::create_dir_all(&path).expect("temp dir should be created"); Self(path) } pub(crate) fn path(&self) -> &Path { &self.0 } pub(crate) fn game_root(&self) -> PathBuf { self.0.join("game") } } impl Drop for TempDir { fn drop(&mut self) { let _ = std::fs::remove_dir_all(&self.0); } }