pub struct AutoDeletePath { /* private fields */ }
Expand description

This struct simply holds an instance of std::path::PathBuf. However, when such an instance goes out of scope and is destroyed, its destructor will be called, which attempts to delete the owned path (either file or directory).

This works even if the program panics.

Useful for creating temporary files that you want to be deleted automatically.

Implementations

Creates an AutoDeletePath in the default temp directory. This method just returns a path; If you want to actually make a file or folder, you have to do that manually.

Examples

File:

let mut temp_path_clone = std::path::PathBuf::new();
{
    let temp_path = auto_delete_path::AutoDeletePath::temp();

    temp_path_clone = temp_path.as_ref().to_owned();
    assert!(!temp_path_clone.exists());
    std::fs::write(&temp_path, "spam").unwrap();
    assert!(temp_path_clone.exists());
} // temp_path dies here, so the file is deleted
assert!(!temp_path_clone.exists());

Directory:

let mut temp_path_clone = std::path::PathBuf::new();
{
    let temp_path = auto_delete_path::AutoDeletePath::temp();
    temp_path_clone = temp_path.as_ref().to_owned();
    assert!(!temp_path_clone.exists());
    std::fs::create_dir(&temp_path).unwrap();
    assert!(temp_path_clone.exists());
} // temp_path dies here, so the directory is deleted
assert!(!temp_path_clone.exists());

Trait Implementations

Converts this type into a shared reference of the (usually inferred) input type.

Formats the value using the given formatter. Read more

Executes the destructor for this type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.