13
13
html_root_url = "https://doc.rust-lang.org/tempdir/" ) ]
14
14
#![ cfg_attr( test, deny( warnings) ) ]
15
15
16
+ //! Temporary directories of files.
17
+ //!
18
+ //! The [`TempDir`] type creates a directory on the file system that
19
+ //! is deleted once it goes out of scope. At construction, the
20
+ //! `TempDir` creates a new directory with a randomly generated name,
21
+ //! and with a prefix of your choosing.
22
+ //!
23
+ //! [`TempDir`]: struct.TempDir.html
24
+ //! [`std::env::temp_dir()`]: https://doc.rust-lang.org/std/env/fn.temp_dir.html
25
+ //!
26
+ //! # Examples
27
+ //!
28
+ //! ```
29
+ //! extern crate tempdir;
30
+ //!
31
+ //! use std::fs::File;
32
+ //! use std::io::Write;
33
+ //! use tempdir::TempDir;
34
+ //!
35
+ //! fn main() {
36
+ //! // Create a directory inside of `std::env::temp_dir()`, named with
37
+ //! // the prefix, "example".
38
+ //! let tmp_dir = TempDir::new("example").expect("create temp dir");
39
+ //! let file_path = tmp_dir.path().join("my-temporary-note.txt");
40
+ //! let mut tmp_file = File::create(file_path).expect("create temp file");
41
+ //! writeln!(tmp_file, "Brian was here. Briefly.").expect("write temp file");
42
+ //!
43
+ //! // By closing the `TempDir` explicitly we can check that it has
44
+ //! // been deleted successfully. If we don't close it explicitly,
45
+ //! // the directory will still be deleted when `tmp_dir` goes out
46
+ //! // of scope, but we won't know whether deleting the directory
47
+ //! // succeeded.
48
+ //! drop(tmp_file);
49
+ //! tmp_dir.close().expect("delete temp dir");
50
+ //! }
51
+ //! ```
52
+
16
53
extern crate rand;
17
54
18
55
use std:: env;
@@ -22,8 +59,50 @@ use std::fs;
22
59
use std:: path:: { self , PathBuf , Path } ;
23
60
use rand:: { thread_rng, Rng } ;
24
61
25
- /// A wrapper for a path to temporary directory implementing automatic
26
- /// scope-based deletion.
62
+ /// A directory in the filesystem that is automatically deleted when
63
+ /// it goes out of scope.
64
+ ///
65
+ /// The [`TempDir`] type creates a directory on the file system that
66
+ /// is deleted once it goes out of scope. At construction, the
67
+ /// `TempDir` creates a new directory with a randomly generated name,
68
+ /// and with a prefix of your choosing.
69
+ ///
70
+ /// The default constructor, [`TempDir::new`], creates directories in
71
+ /// the location returned by [`std::env::temp_dir()`], but they can be
72
+ /// configured to manage the lifetime of a temporary directory in any
73
+ /// location by constructing with [`TempDir::new_in`].
74
+ ///
75
+ /// After creating a `TempDir`, work with the file system by doing
76
+ /// standard [`std::fs`] file system operations on it's [`Path`],
77
+ /// which can be retrieved with [`TempDir::path`]. Once the `TempDir`
78
+ /// value is dropped, the directory at the path will be deleted, along
79
+ /// with any files and directories it contains. It is your responsibility
80
+ /// to ensure that no further file system operations are attempted
81
+ /// inside the temporary directory once it has been deleted.
82
+ ///
83
+ /// Various platform-specific conditions may cause `TempDir` to fail
84
+ /// to delete the underlying directory. It's important to ensure that
85
+ /// handles (like [`File`] and [`ReadDir`]) to files inside the
86
+ /// directory are dropped before the `TempDir` goes out of scope. The
87
+ /// `TempDir` destructor will silently ignore any errors in deleting
88
+ /// the directory; to instead handle errors call [`TempDir::close`].
89
+ ///
90
+ /// Note that if the program exits before the `TempDir` destructor is
91
+ /// run, such as via [`std::process::exit`], by segfaulting, or by
92
+ /// receiving a signal like `SIGINT`, then the temporary directory
93
+ /// will not be deleted.
94
+ ///
95
+ /// [`File`]: http://doc.rust-lang.org/std/fs/struct.File.html
96
+ /// [`Path`]: http://doc.rust-lang.org/std/path/struct.Path.html
97
+ /// [`ReadDir`]: http://doc.rust-lang.org/std/fs/struct.ReadDir.html
98
+ /// [`TempDir::close`]: struct.TempDir.html#method.close
99
+ /// [`TempDir::new`]: struct.TempDir.html#method.new
100
+ /// [`TempDir::new_in`]: struct.TempDir.html#method.new_in
101
+ /// [`TempDir::path`]: struct.TempDir.html#method.path
102
+ /// [`TempDir`]: struct.TempDir.html
103
+ /// [`std::env::temp_dir()`]: https://doc.rust-lang.org/std/env/fn.temp_dir.html
104
+ /// [`std::fs`]: http://doc.rust-lang.org/std/fs/index.html
105
+ /// [`std::process::exit`]: http://doc.rust-lang.org/std/process/fn.exit.html
27
106
pub struct TempDir {
28
107
path : Option < PathBuf > ,
29
108
}
@@ -38,11 +117,52 @@ const NUM_RETRIES: u32 = 1 << 31;
38
117
const NUM_RAND_CHARS : usize = 12 ;
39
118
40
119
impl TempDir {
41
- /// Attempts to make a temporary directory inside of `tmpdir` whose name
42
- /// will have the prefix `prefix`. The directory will be automatically
43
- /// deleted once the returned wrapper is destroyed.
120
+ /// Attempts to make a temporary directory inside of `env::temp_dir()` whose
121
+ /// name will have the prefix, `prefix`. The directory and
122
+ /// everything inside it will be automatically deleted once the
123
+ /// returned `TempDir` is destroyed.
124
+ ///
125
+ /// If the directory can not be created, `Err` is returned.
126
+ ///
127
+ /// # Examples
128
+ ///
129
+ /// ```
130
+ /// use std::fs::File;
131
+ /// use std::io::Write;
132
+ /// use tempdir::TempDir;
133
+ ///
134
+ /// // Create a directory inside of `std::env::temp_dir()`, named with
135
+ /// // the prefix, "example".
136
+ /// let tmp_dir = TempDir::new("example").expect("create temp dir");
137
+ /// let file_path = tmp_dir.path().join("my-temporary-note.txt");
138
+ /// let mut tmp_file = File::create(file_path).expect("create temp file");
139
+ /// writeln!(tmp_file, "Brian was here. Briefly.").expect("write temp file");
140
+ /// ```
141
+ pub fn new ( prefix : & str ) -> io:: Result < TempDir > {
142
+ TempDir :: new_in ( & env:: temp_dir ( ) , prefix)
143
+ }
144
+
145
+ /// Attempts to make a temporary directory inside of `tmpdir`
146
+ /// whose name will have the prefix `prefix`. The directory and
147
+ /// everything inside it will be automatically deleted once the
148
+ /// returned `TempDir` is destroyed.
149
+ ///
150
+ /// If the directory can not be created, `Err` is returned.
151
+ ///
152
+ /// # Examples
44
153
///
45
- /// If no directory can be created, `Err` is returned.
154
+ /// ```
155
+ /// use std::fs::{self, File};
156
+ /// use std::io::Write;
157
+ /// use tempdir::TempDir;
158
+ ///
159
+ /// // Create a directory inside of the current directory, named with
160
+ /// // the prefix, "example".
161
+ /// let tmp_dir = TempDir::new_in(".", "example").expect("create temp dir");
162
+ /// let file_path = tmp_dir.path().join("my-temporary-note.txt");
163
+ /// let mut tmp_file = File::create(file_path).expect("create temp file");
164
+ /// writeln!(tmp_file, "Brian was here. Briefly.").expect("write temp file");
165
+ /// ```
46
166
pub fn new_in < P : AsRef < Path > > ( tmpdir : P , prefix : & str ) -> io:: Result < TempDir > {
47
167
let storage;
48
168
let mut tmpdir = tmpdir. as_ref ( ) ;
@@ -76,32 +196,77 @@ impl TempDir {
76
196
"too many temporary directories already exist" ) )
77
197
}
78
198
79
- /// Attempts to make a temporary directory inside of `env::temp_dir()` whose
80
- /// name will have the prefix `prefix`. The directory will be automatically
81
- /// deleted once the returned wrapper is destroyed.
199
+ /// Accesses the [`Path`] to the temporary directory.
82
200
///
83
- /// If no directory can be created, `Err` is returned.
84
- pub fn new ( prefix : & str ) -> io:: Result < TempDir > {
85
- TempDir :: new_in ( & env:: temp_dir ( ) , prefix)
201
+ /// [`Path`]: http://doc.rust-lang.org/std/path/struct.Path.html
202
+ ///
203
+ /// # Examples
204
+ ///
205
+ /// ```
206
+ /// use tempdir::TempDir;
207
+ ///
208
+ /// let tmp_dir = TempDir::new("example").expect("create temp dir");
209
+ ///
210
+ /// // Check that the temp directory actually exists.
211
+ /// assert!(tmp_dir.path().exists());
212
+ /// ```
213
+ pub fn path ( & self ) -> & path:: Path {
214
+ self . path . as_ref ( ) . unwrap ( )
86
215
}
87
216
88
- /// Unwrap the wrapped `std::path::Path` from the `TempDir` wrapper.
89
- /// This discards the wrapper so that the automatic deletion of the
90
- /// temporary directory is prevented.
217
+ /// Unwraps the [`Path`] contained in the `TempDir` and
218
+ /// returns it. This destroys the `TempDir` without deleting the
219
+ /// directory represented by the returned `Path`.
220
+ ///
221
+ /// [`Path`]: http://doc.rust-lang.org/std/path/struct.Path.html
222
+ ///
223
+ /// # Examples
224
+ ///
225
+ /// ```
226
+ /// use std::fs;
227
+ /// use tempdir::TempDir;
228
+ ///
229
+ /// let tmp_dir = TempDir::new("example").expect("create temp dir");
230
+ ///
231
+ /// // Convert `tmp_dir` into a `Path`, destroying the `TempDir`
232
+ /// // without deleting the directory.
233
+ /// let tmp_path = tmp_dir.into_path();
234
+ ///
235
+ /// // Delete the temporary directory ourselves.
236
+ /// fs::remove_dir_all(tmp_path).expect("remove temp dir");
237
+ /// ```
91
238
pub fn into_path ( mut self ) -> PathBuf {
92
239
self . path . take ( ) . unwrap ( )
93
240
}
94
241
95
- /// Access the wrapped `std::path::Path` to the temporary directory.
96
- pub fn path ( & self ) -> & path:: Path {
97
- self . path . as_ref ( ) . unwrap ( )
98
- }
99
-
100
- /// Close and remove the temporary directory
242
+ /// Closes and removes the temporary directory, returing a `Result`.
101
243
///
102
244
/// Although `TempDir` removes the directory on drop, in the destructor
103
245
/// any errors are ignored. To detect errors cleaning up the temporary
104
246
/// directory, call `close` instead.
247
+ ///
248
+ /// # Examples
249
+ ///
250
+ /// ```
251
+ /// use std::fs::File;
252
+ /// use std::io::Write;
253
+ /// use tempdir::TempDir;
254
+ ///
255
+ /// // Create a directory inside of `std::env::temp_dir()`, named with
256
+ /// // the prefix, "example".
257
+ /// let tmp_dir = TempDir::new("example").expect("create temp dir");
258
+ /// let file_path = tmp_dir.path().join("my-temporary-note.txt");
259
+ /// let mut tmp_file = File::create(file_path).expect("create temp file");
260
+ /// writeln!(tmp_file, "Brian was here. Briefly.").expect("write temp file");
261
+ ///
262
+ /// // By closing the `TempDir` explicitly we can check that it has
263
+ /// // been deleted successfully. If we don't close it explicitly,
264
+ /// // the directory will still be deleted when `tmp_dir` goes out
265
+ /// // of scope, but we won't know whether deleting the directory
266
+ /// // succeeded.
267
+ /// drop(tmp_file);
268
+ /// tmp_dir.close().expect("delete temp dir");
269
+ /// ```
105
270
pub fn close ( mut self ) -> io:: Result < ( ) > {
106
271
self . cleanup_dir ( )
107
272
}
0 commit comments