Skip to main content

Crate kevy_tmpdir

Crate kevy_tmpdir 

Source
Expand description

A temporary directory that is actually unique, and cleans up after itself.

Nine files in this workspace had invented their own, and five different ways of trying to make the name unique:

kevy-persist/feed_meta.rs    process::id() + Instant::now()
kevy-persist/shards_meta.rs  process::id() + process::id()
kevy-persist/reshard.rs      SystemTime
kevy-cli/backup.rs           nanos
kevy-uring/ring_tests.rs     process::id()
…

None of them is unique. process::id() is the SAME for every test in a binary — cargo runs them as threads, not processes — so two tests in the same file get the same directory and stamp on each other’s files. A clock is unique only until two threads read it inside the same tick, which under parallel test execution is exactly what happens. That is a flake: green on a quiet machine, red under load, and it looks like the code is broken rather than the fixture.

A process id and a monotonic counter, together, cannot collide: the counter separates threads within a process and the pid separates processes. That is the whole trick, and it is why this is one crate instead of nine copies.

let dir = kevy_tmpdir::TmpDir::new("readme");
std::fs::write(dir.path().join("data"), b"x")?;
let path = dir.path().to_path_buf();
drop(dir);
assert!(!path.exists(), "gone with the guard");

Structs§

TmpDir
A unique directory under the system temp dir, removed when dropped.

Functions§

unique_dir
A unique directory as a bare path: created, PRE-CLEARED, and owned by the caller — for call sites that already carry their own cleanup, and for the one production path (kevy-cli’s embed scratch) where the directory must outlive the function that made it.