Struct temp_file::TempFile[][src]

pub struct TempFile { /* fields omitted */ }

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

Deletes the file on drop. Ignores errors deleting the file.

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());

Implementations

impl TempFile[src]

pub fn new() -> Result<Self, String>[src]

Create a new empty file in a system temporary directory.

Drop the returned struct to delete the file.

Errors

Returns Err when it fails to create the file.

Example

// Prints "/tmp/1a9b0".
println!("{:?}", temp_file::TempFile::new().unwrap().path());

pub fn with_prefix(prefix: impl AsRef<str>) -> Result<Self, String>[src]

Create a new empty file in a system temporary directory. Use prefix as the first part of the file’s name.

Drop the returned struct to delete the file.

Errors

Returns Err when it fails to create the file.

Example

// Prints "/tmp/ok1a9b0".
println!("{:?}", temp_file::TempFile::with_prefix("ok").unwrap().path());

pub fn with_contents(self, contents: &[u8]) -> Result<Self, String>[src]

Write contents to the file.

Errors

Returns Err when it fails to write all of contents to the file.

#[must_use]pub fn panic_on_cleanup_error(self) -> Self[src]

Make the struct panic on Drop if it hits an error while removing the file.

pub fn leak(self)[src]

Do not delete the file.

This is useful when debugging a test.

#[must_use]pub fn path(&self) -> &Path[src]

The path to the file.

Trait Implementations

impl Clone for TempFile[src]

impl Debug for TempFile[src]

impl Drop for TempFile[src]

impl Eq for TempFile[src]

impl Hash for TempFile[src]

impl Ord for TempFile[src]

impl PartialEq<TempFile> for TempFile[src]

impl PartialOrd<TempFile> for TempFile[src]

impl StructuralEq for TempFile[src]

impl StructuralPartialEq for TempFile[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.