lib.rs
 1  use std::env::temp_dir;
 2  use std::ffi::OsStr;
 3  use std::fs::{create_dir, remove_dir_all};
 4  use std::io;
 5  use std::path::{Path, PathBuf};
 6  
 7  
 8  pub struct TempSubDir(PathBuf);
 9  
10  impl TempSubDir
11  {
12      /// Create a temporary directory with a name that should be unique enough for the tests of the
13      /// parent package.
14      pub fn new(subname: &str) -> io::Result<Self>
15      {
16          const UNIQUE: &'static str = "5a3fa1c4b3ed363f48a23fc7c10c9691";
17          let dir = temp_dir().join(format!("cfg_rust_features-{}-{}", subname, UNIQUE));
18          create_dir(&dir).map(|()| TempSubDir(dir))
19      }
20  
21      /// Removes the directory, after removing all its contents.
22      pub fn delete_all(&mut self) -> io::Result<()>
23      {
24          remove_dir_all(&self.0)
25      }
26  }
27  
28  impl AsRef<Path> for TempSubDir
29  {
30      fn as_ref(&self) -> &Path
31      {
32          &self.0
33      }
34  }
35  
36  impl AsRef<OsStr> for TempSubDir
37  {
38      fn as_ref(&self) -> &OsStr
39      {
40          self.0.as_ref()
41      }
42  }
43  
44  impl Drop for TempSubDir
45  {
46      fn drop(&mut self)
47      {
48          let _ = self.delete_all();
49      }
50  }