Crate temp_file[][src]

crates.io version license: Apache 2.0 unsafe forbidden pipeline status

temp-file

Provides a TempFile struct.

Features

  • Makes a file in a system temporary directory
  • Deletes the file on drop
  • Optional file name prefix
  • Optional file contents
  • No dependencies
  • forbid(unsafe_code)

Limitations

Alternatives

  • test-temp-file
    • Depends on crates which contain unsafe
    • Incomplete documentation
  • temp_file_name
    • Does not delete file
    • Usage is not straightforward. Missing example.
  • mktemp
    • Sets file mode 0600 on unix
    • Contains unsafe
    • No readme or online docs

Example

use temp_file::TempFile;
let t = TempFile::new()
  .unwrap()
  .with_contents(b"abc")
  .unwrap();
// Prints "/tmp/1a9b0".
println!("{:?}", t.path());
assert_eq!(
  "abc",
  std::fs::read_to_string(t.path()).unwrap(),
);
// Prints "/tmp/1a9b1".
println!(
    "{:?}", TempFile::new().unwrap().path());

Cargo Geiger Safety Report

Structs

TempFile

The path of an existing writable file in a system temporary directory.

Functions

empty

Create a new empty file in a system temporary directory.

with_contents

Create a new file in a system temporary directory and writes contents to it.