diff --git a/README.md b/README.md index f1a3824..d596bdd 100644 --- a/README.md +++ b/README.md @@ -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(()) } diff --git a/src/lib.rs b/src/lib.rs index 27e4046..651d7aa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,16 +29,22 @@ //! 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, @@ -46,7 +52,8 @@ //! // 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(()) //! } //! ``` @@ -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::new_in(&env::temp_dir(), prefix) @@ -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>(tmpdir: P, prefix: &str) -> io::Result { let storage; @@ -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. @@ -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() @@ -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() @@ -278,12 +301,14 @@ 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, @@ -291,7 +316,9 @@ impl TempDir { /// // 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());