[][src]Struct tempfile::TempPath

pub struct TempPath { /* fields omitted */ }

A path to a named temporary file without an open file handle.

This is useful when the temporary file needs to be used by a child process, for example.

When dropped, the temporary file is deleted.

Methods

impl TempPath[src]

pub fn close(self) -> Result<()>[src]

Close and remove the temporary file.

Use this if you want to detect errors in deleting the file.

Errors

If the file cannot be deleted, Err is returned.

Examples

use tempfile::NamedTempFile;

let file = NamedTempFile::new()?;

// Close the file, but keep the path to it around.
let path = file.into_temp_path();

// By closing the `TempPath` explicitly, we can check that it has
// been deleted successfully. If we don't close it explicitly, the
// file will still be deleted when `file` goes out of scope, but we
// won't know whether deleting the file succeeded.
path.close()?;

pub fn persist<P: AsRef<Path>>(
    self,
    new_path: P
) -> Result<(), PathPersistError>
[src]

Persist the temporary file at the target path.

If a file exists at the target path, persist will atomically replace it. If this method fails, it will return self in the resulting PathPersistError.

Note: Temporary files cannot be persisted across filesystems.

Security

Only use this method if you're positive that a temporary file cleaner won't have deleted your file. Otherwise, you might end up persisting an attacker controlled file.

Errors

If the file cannot be moved to the new location, Err is returned.

Examples

use tempfile::NamedTempFile;

let mut file = NamedTempFile::new()?;
writeln!(file, "Brian was here. Briefly.")?;

let path = file.into_temp_path();
path.persist("./saved_file.txt")?;

pub fn persist_noclobber<P: AsRef<Path>>(
    self,
    new_path: P
) -> Result<(), PathPersistError>
[src]

Persist the temporary file at the target path iff no file exists there.

If a file exists at the target path, fail. If this method fails, it will return self in the resulting PathPersistError.

Note: Temporary files cannot be persisted across filesystems. Also Note: This method is not atomic. It can leave the original link to the temporary file behind.

Security

Only use this method if you're positive that a temporary file cleaner won't have deleted your file. Otherwise, you might end up persisting an attacker controlled file.

Errors

If the file cannot be moved to the new location or a file already exists there, Err is returned.

Examples

use tempfile::NamedTempFile;

let mut file = NamedTempFile::new()?;
writeln!(file, "Brian was here. Briefly.")?;

let path = file.into_temp_path();
path.persist_noclobber("./saved_file.txt")?;

pub fn keep(self) -> Result<PathBuf, PathPersistError>[src]

Keep the temporary file from being deleted. This function will turn the temporary file into a non-temporary file without moving it.

Errors

On some platforms (e.g., Windows), we need to mark the file as non-temporary. This operation could fail.

Examples

use tempfile::NamedTempFile;

let mut file = NamedTempFile::new()?;
writeln!(file, "Brian was here. Briefly.")?;

let path = file.into_temp_path();
let path = path.keep()?;

Trait Implementations

impl Drop for TempPath[src]

impl AsRef<Path> for TempPath[src]

impl AsRef<OsStr> for TempPath[src]

impl From<PathPersistError> for TempPath[src]

impl Deref for TempPath[src]

type Target = Path

The resulting type after dereferencing.

impl Debug for TempPath[src]

Auto Trait Implementations

Blanket Implementations

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

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

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.

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

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

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

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,