Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid deleting temporary files on error
Previously if the compiler error'd, fatally, then temporary directories which should be preserved by -Csave-temps would be deleted due to fatal compiler errors being implemented as panics.
- Loading branch information
1 parent
ceedf1d
commit 2627eed
Showing
6 changed files
with
54 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters