GithubHelp home page GithubHelp logo

Comments (9)

ExpHP avatar ExpHP commented on June 23, 2024 1

I took a similar approach to making my life easier shortly after making the issue.

Personally I think the cases where you'd expect a temporary directory not to be cleared are application specific enough that they're best handled by your application

Further making your point, the conditions I chose to leak it on were completely different from yours. I very much want this in release builds!

/// Leaks the inner TempDir if we are unwinding.
impl Drop for TempDir {
    fn drop(&mut self) {
        if ::std::thread::panicking() {
            ::std::mem::forget(self.0.take())
        }
    }
}

But I do think that panicking is special in this regard. There are many ways you could modify the behavior conditionally on debug_assertions without creating a wrapper type (e.g. a fn() -> Box<AsRef<Path>> that conditionally returns a TempDir or a PathBuf), but there's absolutely no other way to modify the behavior conditionally on unwinding.

from tempdir.

ExpHP avatar ExpHP commented on June 23, 2024 1

Okay. I am closing this because there is a clear case for not building this functionality into the crate, and because I do not feel that the documentation needs to show a solution like this. I feel that somebody who is motivated enough can easily figure it out, and that the presence of the example could create unnecessary confusion.

from tempdir.

KodrAus avatar KodrAus commented on June 23, 2024

Personally I think the cases where you'd expect a temporary directory not to be cleared are application specific enough that they're best handled by your application, building an abstraction around TempDir that persists it in debug builds on drop or something like that. Then you could have something roughly like:

struct Directory {
    inner: Option<TempDir>
}

impl Drop for Directory {
    fn drop(&mut self) {
        if let Some(dir) = mem::replace(&mut self.inner, None) {
            #[cfg(debug_assertions)]
            dir.into_path();
        }
    }
}

from tempdir.

KodrAus avatar KodrAus commented on June 23, 2024

Yeh, I see what you mean there. The main thing making me unsure about whether we should support this as a first-class thing in tempdir are other erroneous situations besides panicking where a user might want similar behaviour or not, or do something besides not deleting the directory.

I definitely think we should at least add an example demonstrating how you can avoid dropping the directory when somebody panics like you've shown.

from tempdir.

otavio avatar otavio commented on June 23, 2024

Isn't it shown at https://docs.rs/tempdir/0.3.5/tempdir/struct.TempDir.html#method.into_path ?

from tempdir.

ExpHP avatar ExpHP commented on June 23, 2024

@otavio: But that does not delete the directory on success, almost entirely defeating the point of making a temp dir (the only remaining advantage being that you don't have to name it).

The snippet I linked earlier is an example of the desired behavior, but it is a bit beefy because it makes an effort to do everything that TempDir can. To slim it down a bit:

//! Shim around the `tempdir` crate which doesn't delete the
//! directories on unwind, in order to facilitate debugging.
extern crate tempdir;
pub use tempdir::TempDir as ActualTempDir;

use ::std::io::Result;
use ::std::path::{Path, PathBuf};

/// Wrapper around `tempdir::TempDir` that does not destroy the directory on unwind.
#[derive(Debug)]
pub struct TempDir(Option<ActualTempDir>);

// Forward inherent methods to the tempdir crate.
impl TempDir {
    pub fn new(prefix: &str) -> Result<TempDir>
    { ActualTempDir::new(prefix).map(Some).map(TempDir) }

    pub fn path(&self) -> &Path
    { self.0.as_ref().unwrap().path() }
}

/// Leaks the inner TempDir if we are unwinding.
impl Drop for TempDir {
    fn drop(&mut self) {
        if ::std::thread::panicking() {
            ::std::mem::forget(self.0.take())
        }
    }
}

from tempdir.

otavio avatar otavio commented on June 23, 2024

I understand but I am unsure it is something we would like to have in production. Possibly, it might be a flag we could configure to enable/disable this behavior.

from tempdir.

ExpHP avatar ExpHP commented on June 23, 2024

I understand but I am unsure it is something we would like to have in production.

What is the "it" you are referring to? The behavior of leaking temp directories, or the presence of a wrapper type around TempDir?

If it is the former, that is a problem that can be easily solved by anybody who wants to use this solution in their application. They could easily put the body of the drop impl in a conditionally compiled block.

(in fact, this seems a perfect example of the kind of issue @KodrAus is talking about, which shows why this functionality shouldn't be built into the tempdir crate itself. Some applications might want to #[cfg(test)] while others may want to e.g. add a feature for the behavior.)

from tempdir.

otavio avatar otavio commented on June 23, 2024

@ExpHP yes, leaking temp directories.

That's exactly what I had in mind, someone desiring it would have it in a feature or cfg flag.

from tempdir.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.