Skip to main content

TempDir

Struct TempDir 

Source
pub struct TempDir { /* private fields */ }
Expand description

A temporary directory that auto-deletes when dropped.

§Example

use mod_tempdir::TempDir;

let dir = TempDir::new().unwrap();
let file_path = dir.path().join("test.txt");
std::fs::write(&file_path, b"hello").unwrap();
// dir and its contents are deleted at end of scope

Implementations§

Source§

impl TempDir

Source

pub fn new() -> Result<Self>

Create a new temporary directory in the system’s temp location (/tmp on Linux/macOS, %TEMP% on Windows).

The basename is .tmp-{pid}-{name12} where {pid} is the current process ID (used by cleanup_orphans to identify entries left behind by crashed processes) and {name12} is a 12-character Crockford base32 string from the shared name generator. With the mod-rand feature enabled, the name fragment comes from mod_rand::tier2::unique_name; without it, from an internal process-unique mixer.

§Errors

Returns the underlying io::Error from std::fs::create_dir if the directory cannot be created.

§Example
use mod_tempdir::TempDir;

let dir = TempDir::new().unwrap();
assert!(dir.path().is_dir());
Examples found in repository?
examples/basic.rs (line 8)
7fn main() -> std::io::Result<()> {
8    let dir = TempDir::new()?;
9    println!("Created: {}", dir.path().display());
10
11    let file = dir.path().join("greeting.txt");
12    std::fs::write(&file, b"Hello from mod-tempdir!\n")?;
13    println!("Wrote:   {}", file.display());
14
15    println!("Contents: {}", std::fs::read_to_string(&file)?);
16    println!("\n(directory will be deleted automatically when this program exits)");
17
18    Ok(())
19}
Source

pub fn with_prefix(prefix: &str) -> Result<Self>

Create a new temporary directory with the given prefix.

The final basename is {prefix}-{12-char-name}. The prefix is joined verbatim and is the caller’s responsibility to sanitize.

§Errors

Returns the underlying io::Error from std::fs::create_dir if the directory cannot be created.

§Example
use mod_tempdir::TempDir;

let dir = TempDir::with_prefix("my-app").unwrap();
assert!(dir
    .path()
    .file_name()
    .unwrap()
    .to_string_lossy()
    .starts_with("my-app-"));
Source

pub fn path(&self) -> &Path

Return the path of this temporary directory.

§Example
use mod_tempdir::TempDir;

let dir = TempDir::new().unwrap();
let log = dir.path().join("output.log");
std::fs::write(&log, b"hello").unwrap();
Examples found in repository?
examples/basic.rs (line 9)
7fn main() -> std::io::Result<()> {
8    let dir = TempDir::new()?;
9    println!("Created: {}", dir.path().display());
10
11    let file = dir.path().join("greeting.txt");
12    std::fs::write(&file, b"Hello from mod-tempdir!\n")?;
13    println!("Wrote:   {}", file.display());
14
15    println!("Contents: {}", std::fs::read_to_string(&file)?);
16    println!("\n(directory will be deleted automatically when this program exits)");
17
18    Ok(())
19}
Source

pub fn persist(self) -> PathBuf

Consume this TempDir and return the path, disabling cleanup on drop. The directory and its contents will persist.

Use this when you want to inspect contents after a test fails.

§Example
use mod_tempdir::TempDir;

let dir = TempDir::new().unwrap();
let kept = dir.persist();
// `kept` survives past the original `dir` going out of scope.
Source

pub fn cleanup_on_drop(&self) -> bool

Return true if the directory will be deleted on drop.

§Example
use mod_tempdir::TempDir;

let dir = TempDir::new().unwrap();
assert!(dir.cleanup_on_drop());

Trait Implementations§

Source§

impl Debug for TempDir

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for TempDir

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.