blob: c7c8454908abf4de758f560f0db2fa95507de185 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
use std::{
env, fs, io,
path::{Path, PathBuf},
};
use uuid::Uuid;
#[derive(Debug)]
pub struct TempFile {
path: PathBuf,
}
impl TempFile {
/// Create a new unique `TempFile`. The file will not exist after this.
pub fn new() -> Self {
TempFile {
path: generate_path(),
}
}
pub fn to_path_buf(&self) -> PathBuf {
self.path.clone()
}
}
impl AsRef<Path> for TempFile {
fn as_ref(&self) -> &Path {
self.path.as_path()
}
}
impl Drop for TempFile {
fn drop(&mut self) {
if let Err(e) = fs::remove_file(&self.path)
&& e.kind() != io::ErrorKind::NotFound
{
log::error!(
"Unable to remove temp file {}: {:?}",
self.path.display(),
e
);
}
}
}
fn generate_path() -> PathBuf {
env::temp_dir().join(Uuid::new_v4().to_string())
}
|