Skip to content
This repository was archived by the owner on Aug 20, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ fn write_temp_folder_with_files() -> Result<(), io::Error> {
let file_path = dir.path().join("foo.txt");
println!("{:?}", file_path);

let mut f = try!(File::create(file_path));
try!(f.write_all(b"Hello, world!"));
try!(f.sync_all());
try!(dir.close());
let mut f = File::create(file_path)?;
f.write_all(b"Hello, world!")?;
f.sync_all()?;
dir.close()?;
}
Ok(())
}
Expand Down
63 changes: 45 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,31 @@
//! extern crate tempdir;
//!
//! use std::fs::File;
//! use std::io::Write;
//! use std::io::{self, Write};
//! use tempdir::TempDir;
//!
//! fn main() {
//! if let Err(_) = run() {
//! ::std::process::exit(1);
//! }
//! }
//!
//! fn run() -> Result<(), io::Error> {
//! // Create a directory inside of `std::env::temp_dir()`, named with
//! // the prefix "example".
//! let tmp_dir = TempDir::new("example").expect("create temp dir");
//! let tmp_dir = TempDir::new("example")?;
//! let file_path = tmp_dir.path().join("my-temporary-note.txt");
//! let mut tmp_file = File::create(file_path).expect("create temp file");
//! writeln!(tmp_file, "Brian was here. Briefly.").expect("write temp file");
//! let mut tmp_file = File::create(file_path)?;
//! writeln!(tmp_file, "Brian was here. Briefly.")?;
//!
//! // By closing the `TempDir` explicitly, we can check that it has
//! // been deleted successfully. If we don't close it explicitly,
//! // the directory will still be deleted when `tmp_dir` goes out
//! // of scope, but we won't know whether deleting the directory
//! // succeeded.
//! drop(tmp_file);
//! tmp_dir.close().expect("delete temp dir");
//! tmp_dir.close()?;
//! Ok(())
//! }
//! ```

Expand Down Expand Up @@ -133,15 +140,19 @@ impl TempDir {
/// use std::io::Write;
/// use tempdir::TempDir;
///
/// # use std::io;
/// # fn run() -> Result<(), io::Error> {
/// // Create a directory inside of `std::env::temp_dir()`, named with
/// // the prefix, "example".
/// let tmp_dir = TempDir::new("example").expect("create temp dir");
/// let tmp_dir = TempDir::new("example")?;
/// let file_path = tmp_dir.path().join("my-temporary-note.txt");
/// let mut tmp_file = File::create(file_path).expect("create temp file");
/// writeln!(tmp_file, "Brian was here. Briefly.").expect("write temp file");
/// let mut tmp_file = File::create(file_path)?;
/// writeln!(tmp_file, "Brian was here. Briefly.")?;
///
/// // `tmp_dir` goes out of scope, the directory as well as
/// // `tmp_file` will be deleted here.
/// # Ok(())
/// # }
/// ```
pub fn new(prefix: &str) -> io::Result<TempDir> {
TempDir::new_in(&env::temp_dir(), prefix)
Expand All @@ -163,12 +174,16 @@ impl TempDir {
/// use std::io::Write;
/// use tempdir::TempDir;
///
/// # use std::io;
/// # fn run() -> Result<(), io::Error> {
/// // Create a directory inside of the current directory, named with
/// // the prefix, "example".
/// let tmp_dir = TempDir::new_in(".", "example").expect("create temp dir");
/// let tmp_dir = TempDir::new_in(".", "example")?;
/// let file_path = tmp_dir.path().join("my-temporary-note.txt");
/// let mut tmp_file = File::create(file_path).expect("create temp file");
/// writeln!(tmp_file, "Brian was here. Briefly.").expect("write temp file");
/// let mut tmp_file = File::create(file_path)?;
/// writeln!(tmp_file, "Brian was here. Briefly.")?;
/// # Ok(())
/// # }
/// ```
pub fn new_in<P: AsRef<Path>>(tmpdir: P, prefix: &str) -> io::Result<TempDir> {
let storage;
Expand Down Expand Up @@ -212,10 +227,12 @@ impl TempDir {
/// ```
/// use tempdir::TempDir;
///
/// # use std::io;
/// # fn run() -> Result<(), io::Error> {
/// let tmp_path;
///
/// {
/// let tmp_dir = TempDir::new("example").expect("create temp dir");
/// let tmp_dir = TempDir::new("example")?;
/// tmp_path = tmp_dir.path().to_owned();
///
/// // Check that the temp directory actually exists.
Expand All @@ -226,6 +243,8 @@ impl TempDir {
///
/// // Temp directory should be deleted by now
/// assert_eq!(tmp_path.exists(), false);
/// # Ok(())
/// # }
/// ```
pub fn path(&self) -> &path::Path {
self.path.as_ref().unwrap()
Expand All @@ -243,14 +262,18 @@ impl TempDir {
/// use std::fs;
/// use tempdir::TempDir;
///
/// let tmp_dir = TempDir::new("example").expect("create temp dir");
/// # use std::io;
/// # fn run() -> Result<(), io::Error> {
/// let tmp_dir = TempDir::new("example")?;
///
/// // Convert `tmp_dir` into a `Path`, destroying the `TempDir`
/// // without deleting the directory.
/// let tmp_path = tmp_dir.into_path();
///
/// // Delete the temporary directory ourselves.
/// fs::remove_dir_all(tmp_path).expect("remove temp dir");
/// fs::remove_dir_all(tmp_path)?;
/// # Ok(())
/// # }
/// ```
pub fn into_path(mut self) -> PathBuf {
self.path.take().unwrap()
Expand Down Expand Up @@ -278,20 +301,24 @@ impl TempDir {
/// use std::io::Write;
/// use tempdir::TempDir;
///
/// # use std::io;
/// # fn run() -> Result<(), io::Error> {
/// // Create a directory inside of `std::env::temp_dir()`, named with
/// // the prefix, "example".
/// let tmp_dir = TempDir::new("example").expect("create temp dir");
/// let tmp_dir = TempDir::new("example")?;
/// let file_path = tmp_dir.path().join("my-temporary-note.txt");
/// let mut tmp_file = File::create(file_path).expect("create temp file");
/// writeln!(tmp_file, "Brian was here. Briefly.").expect("write temp file");
/// let mut tmp_file = File::create(file_path)?;
/// writeln!(tmp_file, "Brian was here. Briefly.")?;
///
/// // By closing the `TempDir` explicitly we can check that it has
/// // been deleted successfully. If we don't close it explicitly,
/// // the directory will still be deleted when `tmp_dir` goes out
/// // of scope, but we won't know whether deleting the directory
/// // succeeded.
/// drop(tmp_file);
/// tmp_dir.close().expect("delete temp dir");
/// tmp_dir.close()?;
/// # Ok(())
/// # }
/// ```
pub fn close(mut self) -> io::Result<()> {
let result = fs::remove_dir_all(self.path());
Expand Down