Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extra::tempfile: replace mkdtemp with an RAII wrapper (Closes #9763) #9802

Merged
merged 1 commit into from
Oct 11, 2013

Conversation

ben0x539
Copy link
Contributor

this incidentally stops make check from leaving directories in /tmp (Closes #9764)

@ben0x539
Copy link
Contributor Author

Not sure about the right interface for the whole RAII thing, I just wrote something up as a first draft and to see how it would work at call sites. I took the name of the unwrap function from std::c_str::CString, but other than that I'm not sure what other similar types to take cues from.

@ben0x539
Copy link
Contributor Author

tagging @catamorphism

@alexcrichton
Copy link
Member

NIce job! This is pretty much exactly what I would expect from an RAII-style tempdir. The only request that I would have for this is some tests ensuring that the directory is indeed removed upon running out of scope and on a failure. Could you add a task or two to ensure that happens?

Otherwise r+ from me.

@ben0x539
Copy link
Contributor Author

Added some tests for the destructor behavior, also involving tasks.

@alexcrichton
Copy link
Member

r+ from me, nice job, and thanks!

Needs a rebase though :(

@ben0x539
Copy link
Contributor Author

Working on (make checking) a rebase. :)

this incidentally stops `make check` from leaving directories in `/tmp`
bors added a commit that referenced this pull request Oct 11, 2013
this incidentally stops `make check` from leaving directories in `/tmp` (Closes #9764)
@bors bors closed this Oct 11, 2013
@bors bors merged commit 63e9e49 into rust-lang:master Oct 11, 2013
@catamorphism
Copy link
Contributor

@ben0x539 I just rebased one of my branches off this, and I wanted to note that it's a great change. I don't know why I/we didn't do it this way in the first place (well, maybe destructors didn't work yet then :-) Kudos!

/// Unwrap the wrapped `std::path::Path` from the `TempDir` wrapper.
/// This discards the wrapper so that the automatic deletion of the
/// temporary directory is prevented.
pub fn unwrap(self) -> Path {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, why is self passed by value, instead of by reference?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hatahet I figured it was easier to have unwrap discard the TempDir rather than keeping the "disarmed" TempDir around. So it is passed by value so that it is consumed by call to unwrap and you get compile-time errors if you try to use your TempDir object after unwrapping the path from it, instead of runtime errors when it turns out the Option value has been set to None.

I hope that this way it is easier to use than if for example TempDir just had a Path field and a delete_on_drop: bool field that could be set to false instead of using unwrap, and it felt rather usable when I was working on the tests. Of course it might be different in "real" code that wants to keep temporary directories around for longer, and maybe there is a better way. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

rustpkg creates temporary directories but does not delete them
5 participants