gecko-dev/third_party/rust/tempdir
Kartikaya Gupta 47c3dd535d Bug 1369156 - Re-vendor third-party rust libraries with latest cargo-vendor. r=froydnj
MozReview-Commit-ID: LQicTh0fmk0

--HG--
extra : rebase_source : 7a5ee9c3242fefa72e8d0372b8e9c03170c7df4b
2017-06-20 16:05:17 -04:00
..
src
tests
.cargo-checksum.json
.cargo-ok
.travis.yml
Cargo.toml
LICENSE-APACHE
LICENSE-MIT
README.md

tempdir

A Rust library for creating a temporary directory and deleting its entire contents when the directory is dropped.

Build Status

Documentation

Usage

Add this to your Cargo.toml:

[dependencies]
tempdir = "0.3"

and this to your crate root:

extern crate tempdir;

Example

This sample method does the following:

  1. Create a temporary directory in the default location with the given prefix.
  2. Determine a file path in the directory and print it out.
  3. Create a file inside the temp folder.
  4. Write to the file and sync it to disk.
  5. Close the directory, deleting the contents in the process.
fn write_temp_folder_with_files() -> Result<(), io::Error> {
    if let Ok(dir) = TempDir::new("my_directory_prefix") {
        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());
    }
    Ok(())
}

Note: Closing the directory is actually optional, as it would be done on drop. The benefit of closing here is that it allows possible errors to be handled.