|
8 | 8 | // option. This file may not be copied, modified, or distributed
|
9 | 9 | // except according to those terms.
|
10 | 10 |
|
| 11 | +use std::ffi::OsStr; |
11 | 12 | use std::fs::remove_dir_all;
|
12 | 13 | use std::mem;
|
13 | 14 | use std::path::{self, Path, PathBuf};
|
@@ -264,6 +265,65 @@ impl TempDir {
|
264 | 265 | Builder::new().tempdir_in(dir)
|
265 | 266 | }
|
266 | 267 |
|
| 268 | + /// Attempts to make a temporary directory with the specified prefix inside of |
| 269 | + /// `env::temp_dir()`. The directory and everything inside it will be automatically |
| 270 | + /// deleted once the returned `TempDir` is destroyed. |
| 271 | + /// |
| 272 | + /// # Errors |
| 273 | + /// |
| 274 | + /// If the directory can not be created, `Err` is returned. |
| 275 | + /// |
| 276 | + /// # Examples |
| 277 | + /// |
| 278 | + /// ``` |
| 279 | + /// use std::fs::{self, File}; |
| 280 | + /// use std::io::Write; |
| 281 | + /// use tempfile::TempDir; |
| 282 | + /// |
| 283 | + /// # use std::io; |
| 284 | + /// # fn run() -> Result<(), io::Error> { |
| 285 | + /// // Create a directory inside of the current directory |
| 286 | + /// let tmp_dir = TempDir::with_prefix("foo-")?; |
| 287 | + /// let tmp_name = tmp_dir.path().file_name().unwrap().to_str().unwrap(); |
| 288 | + /// assert!(tmp_name.starts_with("foo-")); |
| 289 | + /// # Ok(()) |
| 290 | + /// # } |
| 291 | + /// ``` |
| 292 | + pub fn with_prefix<S: AsRef<OsStr>>(prefix: S) -> io::Result<TempDir> { |
| 293 | + Builder::new().prefix(&prefix).tempdir() |
| 294 | + } |
| 295 | + |
| 296 | + /// Attempts to make a temporary directory with the specified prefix inside |
| 297 | + /// the specified directory. The directory and everything inside it will be |
| 298 | + /// automatically deleted once the returned `TempDir` is destroyed. |
| 299 | + /// |
| 300 | + /// # Errors |
| 301 | + /// |
| 302 | + /// If the directory can not be created, `Err` is returned. |
| 303 | + /// |
| 304 | + /// # Examples |
| 305 | + /// |
| 306 | + /// ``` |
| 307 | + /// use std::fs::{self, File}; |
| 308 | + /// use std::io::Write; |
| 309 | + /// use tempfile::TempDir; |
| 310 | + /// |
| 311 | + /// # use std::io; |
| 312 | + /// # fn run() -> Result<(), io::Error> { |
| 313 | + /// // Create a directory inside of the current directory |
| 314 | + /// let tmp_dir = TempDir::with_prefix_in("foo-", ".")?; |
| 315 | + /// let tmp_name = tmp_dir.path().file_name().unwrap().to_str().unwrap(); |
| 316 | + /// assert!(tmp_name.starts_with("foo-")); |
| 317 | + /// # Ok(()) |
| 318 | + /// # } |
| 319 | + /// ``` |
| 320 | + pub fn with_prefix_in<S: AsRef<OsStr>, P: AsRef<Path>>( |
| 321 | + prefix: S, |
| 322 | + dir: P, |
| 323 | + ) -> io::Result<TempDir> { |
| 324 | + Builder::new().prefix(&prefix).tempdir_in(dir) |
| 325 | + } |
| 326 | + |
267 | 327 | /// Accesses the [`Path`] to the temporary directory.
|
268 | 328 | ///
|
269 | 329 | /// [`Path`]: http://doc.rust-lang.org/std/path/struct.Path.html
|
|
0 commit comments