Expand description
§mod-tempdir
Temporary directory and file management for Rust. Auto-cleanup on Drop, collision-resistant naming, cross-platform paths.
Two types and one orphan-cleanup function:
TempDir: a directory created under the OS temp location, recursively deleted on Drop.NamedTempFile: a single file created under the OS temp location, deleted on Drop.cleanup_orphans: sweeps the OS temp directory for entries left behind by crashed processes and removes those that are both PID-dead and older than a caller-supplied age threshold.
Both types share the same name-generation pipeline, the same
with_prefix / persist / cleanup_on_drop API shape, and the
same silent best-effort Drop semantics.
Designed as a tempfile replacement at MSRV 1.75. The default
build has zero runtime dependencies outside std. An optional
mod-rand feature swaps the built-in name mixer for
mod_rand::tier2::unique_name, which produces a uniformly
distributed name from a SplitMix + Stafford-finisher pipeline.
§Quick example
use mod_tempdir::{NamedTempFile, TempDir};
let dir = TempDir::new().unwrap();
// ... use dir.path() to do work ...
let file = NamedTempFile::new().unwrap();
// ... use file.path() to write into the file ...
// Both are deleted automatically when they go out of scope.§Feature flags
mod-rand(off by default): usemod_rand::tier2::unique_namefor naming. The alphabet is Crockford base32 on both paths, so any caller pattern-matching on the directory or file basename keeps working unchanged when the feature is toggled. Applies to bothTempDirandNamedTempFile.
To enable in Cargo.toml:
mod-tempdir = { version = "0.9", features = ["mod-rand"] }§Cleanup semantics
Drop::drop removes the directory via
std::fs::remove_dir_all (for TempDir) or the file via
std::fs::remove_file (for NamedTempFile). Failures during
cleanup (file in use, permission denied, network filesystem
hiccup) are intentionally silent: a Drop impl must not panic.
Use persist() to keep the entry alive past drop if you need to
inspect it. See NamedTempFile for a Windows-specific note
about open file handles.
Structs§
- Named
Temp File - A temporary file that auto-deletes when dropped.
- Persist
Atomic Error - Error returned by
NamedTempFile::persist_atomicwhen the atomic-persist sequence fails partway through. - TempDir
- A temporary directory that auto-deletes when dropped.
Functions§
- cleanup_
orphans - Sweep the OS temp directory for default-prefix entries this crate could have created and remove those that look orphaned.