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
use std::mem::ManuallyDrop;
use std::path::Path;
use tempfile::TempDir;
#[derive(Debug)]
pub struct MaybeTempDir {
dir: ManuallyDrop<TempDir>,
keep: bool,
}
impl Drop for MaybeTempDir {
fn drop(&mut self) {
let dir = unsafe { ManuallyDrop::take(&mut self.dir) };
if self.keep {
dir.into_path();
}
}
}
impl AsRef<Path> for MaybeTempDir {
fn as_ref(&self) -> &Path {
self.dir.path()
}
}
impl MaybeTempDir {
pub fn new(dir: TempDir, keep_on_drop: bool) -> MaybeTempDir {
MaybeTempDir { dir: ManuallyDrop::new(dir), keep: keep_on_drop }
}
}