Skip to content
This repository was archived by the owner on Aug 20, 2021. It is now read-only.

Commit eed6168

Browse files
committed
Update docs for review feedback
1 parent 8e90b65 commit eed6168

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

src/lib.rs

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
//!
1818
//! The [`TempDir`] type creates a directory on the file system that
1919
//! 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.
20+
//! `TempDir` creates a new directory with a randomly generated name
21+
//! and a prefix of your choosing.
2222
//!
2323
//! [`TempDir`]: struct.TempDir.html
2424
//! [`std::env::temp_dir()`]: https://doc.rust-lang.org/std/env/fn.temp_dir.html
@@ -34,13 +34,13 @@
3434
//!
3535
//! fn main() {
3636
//! // Create a directory inside of `std::env::temp_dir()`, named with
37-
//! // the prefix, "example".
37+
//! // the prefix "example".
3838
//! let tmp_dir = TempDir::new("example").expect("create temp dir");
3939
//! let file_path = tmp_dir.path().join("my-temporary-note.txt");
4040
//! let mut tmp_file = File::create(file_path).expect("create temp file");
4141
//! writeln!(tmp_file, "Brian was here. Briefly.").expect("write temp file");
4242
//!
43-
//! // By closing the `TempDir` explicitly we can check that it has
43+
//! // By closing the `TempDir` explicitly, we can check that it has
4444
//! // been deleted successfully. If we don't close it explicitly,
4545
//! // the directory will still be deleted when `tmp_dir` goes out
4646
//! // of scope, but we won't know whether deleting the directory
@@ -68,12 +68,12 @@ use rand::{thread_rng, Rng};
6868
/// and with a prefix of your choosing.
6969
///
7070
/// 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`].
71+
/// the location returned by [`std::env::temp_dir()`], but `TempDir`
72+
/// can be configured to manage a temporary directory in any location
73+
/// by constructing with [`TempDir::new_in`].
7474
///
7575
/// After creating a `TempDir`, work with the file system by doing
76-
/// standard [`std::fs`] file system operations on it's [`Path`],
76+
/// standard [`std::fs`] file system operations on its [`Path`],
7777
/// which can be retrieved with [`TempDir::path`]. Once the `TempDir`
7878
/// value is dropped, the directory at the path will be deleted, along
7979
/// with any files and directories it contains. It is your responsibility
@@ -122,6 +122,8 @@ impl TempDir {
122122
/// everything inside it will be automatically deleted once the
123123
/// returned `TempDir` is destroyed.
124124
///
125+
/// # Errors
126+
///
125127
/// If the directory can not be created, `Err` is returned.
126128
///
127129
/// # Examples
@@ -137,6 +139,9 @@ impl TempDir {
137139
/// let file_path = tmp_dir.path().join("my-temporary-note.txt");
138140
/// let mut tmp_file = File::create(file_path).expect("create temp file");
139141
/// writeln!(tmp_file, "Brian was here. Briefly.").expect("write temp file");
142+
///
143+
/// // `tmp_dir` goes out of scope, the directory as well as
144+
/// // `tmp_file` will be deleted here.
140145
/// ```
141146
pub fn new(prefix: &str) -> io::Result<TempDir> {
142147
TempDir::new_in(&env::temp_dir(), prefix)
@@ -147,6 +152,8 @@ impl TempDir {
147152
/// everything inside it will be automatically deleted once the
148153
/// returned `TempDir` is destroyed.
149154
///
155+
/// # Errors
156+
///
150157
/// If the directory can not be created, `Err` is returned.
151158
///
152159
/// # Examples
@@ -205,10 +212,20 @@ impl TempDir {
205212
/// ```
206213
/// use tempdir::TempDir;
207214
///
208-
/// let tmp_dir = TempDir::new("example").expect("create temp dir");
215+
/// let tmp_path;
216+
///
217+
/// {
218+
/// let tmp_dir = TempDir::new("example").expect("create temp dir");
219+
/// tmp_path = tmp_dir.path().to_owned();
209220
///
210-
/// // Check that the temp directory actually exists.
211-
/// assert!(tmp_dir.path().exists());
221+
/// // Check that the temp directory actually exists.
222+
/// assert!(tmp_path.exists());
223+
///
224+
/// // End of `tmp_dir` scope, directory will be deleted
225+
/// }
226+
///
227+
/// // Temp directory should be deleted by now
228+
/// assert_eq!(tmp_path.exists(), false);
212229
/// ```
213230
pub fn path(&self) -> &path::Path {
214231
self.path.as_ref().unwrap()
@@ -245,6 +262,15 @@ impl TempDir {
245262
/// any errors are ignored. To detect errors cleaning up the temporary
246263
/// directory, call `close` instead.
247264
///
265+
/// # Errors
266+
///
267+
/// This function may return a variety of [`std::io::Error`]s that result from deleting
268+
/// the files and directories contained with the temporary directory,
269+
/// as well as from deleting the temporary directory itself. These errors
270+
/// may be platform specific.
271+
///
272+
/// [`std::io::Error`]: http://doc.rust-lang.org/std/io/struct.Error.html
273+
///
248274
/// # Examples
249275
///
250276
/// ```

0 commit comments

Comments
 (0)