Struct TempPath

struct TempPath { ... }

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 unless disable_cleanup(true) was called on the builder that constructed this temporary file and/or was called on either this TempPath or the NamedTempFile from which this TempPath was constructed.

Implementations

impl TempPath

fn close(self: Self) -> Result<()>

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()?;
# Ok::<(), std::io::Error>(())
fn persist<P: AsRef<Path>>(self: Self, new_path: P) -> Result<(), PathPersistError>

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. Also neither the file contents nor the containing directory are synchronized, so the update may not yet have reached the disk when persist returns.

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 std::io::Write;
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")?;
# Ok::<(), std::io::Error>(())
fn persist_noclobber<P: AsRef<Path>>(self: Self, new_path: P) -> Result<(), PathPersistError>

Persist the temporary file at the target path if and only if 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;
use std::io::Write;

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

let path = file.into_temp_path();
path.persist_noclobber("./saved_file.txt")?;
# Ok::<(), std::io::Error>(())
fn keep(self: Self) -> Result<PathBuf, PathPersistError>

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 std::io::Write;
use tempfile::NamedTempFile;

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

let path = file.into_temp_path();
let path = path.keep()?;
# Ok::<(), std::io::Error>(())
fn disable_cleanup(self: &mut Self, disable_cleanup: bool)

Disable cleanup of the temporary file. If disable_cleanup is true, the temporary file will not be deleted when this TempPath is dropped. This method is equivalent to calling Builder::disable_cleanup when creating the original NamedTempFile, which see for relevant warnings.

NOTE: this method is primarily useful for testing/debugging. If you want to simply turn a temporary file-path into a non-temporary file-path, prefer TempPath::keep.

fn from_path<impl Into<PathBuf>: Into<PathBuf>>(path: impl Into<PathBuf>) -> Self

Create a new TempPath from an existing path. This can be done even if no file exists at the given path.

This is mostly useful for interacting with libraries and external components that provide files to be consumed or expect a path with no existing file to be given.

impl AsRef for TempPath

fn as_ref(self: &Self) -> &Path

impl AsRef for TempPath

fn as_ref(self: &Self) -> &OsStr

impl Debug for TempPath

fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result

impl Deref for TempPath

fn deref(self: &Self) -> &Path

impl Drop for TempPath

fn drop(self: &mut Self)

impl Freeze for TempPath

impl From for TempPath

fn from(error: PathPersistError) -> TempPath

impl RefUnwindSafe for TempPath

impl Send for TempPath

impl Sync for TempPath

impl Unpin for TempPath

impl UnsafeUnpin for TempPath

impl UnwindSafe for TempPath

impl<P, T> Receiver for TempPath

impl<T> Any for TempPath

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for TempPath

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for TempPath

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> From for TempPath

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into for TempPath

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for TempPath

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for TempPath

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>