summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_data_structures/src/temp_dir.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /compiler/rustc_data_structures/src/temp_dir.rs
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_data_structures/src/temp_dir.rs')
-rw-r--r--compiler/rustc_data_structures/src/temp_dir.rs34
1 files changed, 34 insertions, 0 deletions
diff --git a/compiler/rustc_data_structures/src/temp_dir.rs b/compiler/rustc_data_structures/src/temp_dir.rs
new file mode 100644
index 000000000..a780d2386
--- /dev/null
+++ b/compiler/rustc_data_structures/src/temp_dir.rs
@@ -0,0 +1,34 @@
+use std::mem::ManuallyDrop;
+use std::path::Path;
+use tempfile::TempDir;
+
+/// This is used to avoid TempDir being dropped on error paths unintentionally.
+#[derive(Debug)]
+pub struct MaybeTempDir {
+ dir: ManuallyDrop<TempDir>,
+ // Whether the TempDir should be deleted on drop.
+ keep: bool,
+}
+
+impl Drop for MaybeTempDir {
+ fn drop(&mut self) {
+ // SAFETY: We are in the destructor, and no further access will
+ // occur.
+ 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 }
+ }
+}