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§
Source§impl AutoDeletePath
impl AutoDeletePath
Sourcepub fn temp() -> Self
pub fn temp() -> Self
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§
Source§impl AsRef<Path> for AutoDeletePath
impl AsRef<Path> for AutoDeletePath
Source§impl Debug for AutoDeletePath
impl Debug for AutoDeletePath
Auto Trait Implementations§
impl Freeze for AutoDeletePath
impl RefUnwindSafe for AutoDeletePath
impl Send for AutoDeletePath
impl Sync for AutoDeletePath
impl Unpin for AutoDeletePath
impl UnwindSafe for AutoDeletePath
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more