[][src]Struct auto_delete_path::AutoDeletePath

pub struct AutoDeletePath { /* fields omitted */ }

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

impl AutoDeletePath[src]

pub fn temp() -> Self[src]

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

impl AsRef<Path> for AutoDeletePath[src]

impl Drop for AutoDeletePath[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.