Skip to main content

Crate mod_tempdir

Crate mod_tempdir 

Source
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): use mod_rand::tier2::unique_name for 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 both TempDir and NamedTempFile.

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§

NamedTempFile
A temporary file that auto-deletes when dropped.
PersistAtomicError
Error returned by NamedTempFile::persist_atomic when 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.