]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_data_structures/src/temp_dir.rs
Rollup merge of #94267 - pierwill:fast-reject-bound, r=michaelwoerister
[rust.git] / compiler / rustc_data_structures / src / temp_dir.rs
1 use std::mem::ManuallyDrop;
2 use std::path::Path;
3 use tempfile::TempDir;
4
5 /// This is used to avoid TempDir being dropped on error paths unintentionally.
6 #[derive(Debug)]
7 pub struct MaybeTempDir {
8     dir: ManuallyDrop<TempDir>,
9     // Whether the TempDir should be deleted on drop.
10     keep: bool,
11 }
12
13 impl Drop for MaybeTempDir {
14     fn drop(&mut self) {
15         // SAFETY: We are in the destructor, and no further access will
16         // occur.
17         let dir = unsafe { ManuallyDrop::take(&mut self.dir) };
18         if self.keep {
19             dir.into_path();
20         }
21     }
22 }
23
24 impl AsRef<Path> for MaybeTempDir {
25     fn as_ref(&self) -> &Path {
26         self.dir.path()
27     }
28 }
29
30 impl MaybeTempDir {
31     pub fn new(dir: TempDir, keep_on_drop: bool) -> MaybeTempDir {
32         MaybeTempDir { dir: ManuallyDrop::new(dir), keep: keep_on_drop }
33     }
34 }