Struct TempPath
pub struct TempPath { /* private fields */ }
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) -> 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,
Erris 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, 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
selfin the resultingPathPersistError.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
persistreturns.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,
Erris 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, 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
selfin the resultingPathPersistError.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,
Erris 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) -> 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(&mut self, disable_cleanup: bool)Disable cleanup of the temporary file. If
disable_cleanupistrue, the temporary file will not be deleted when thisTempPathis dropped. This method is equivalent to callingBuilder::disable_cleanupwhen creating the originalNamedTempFile, 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(path: impl Into<PathBuf>) -> SelfCreate 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.
When passed a relative path, this function first tries to make it absolute (relative to the current directory). If this fails, this function uses the relative path as-is.
DEPRECATED: Use
TempPath::try_from_pathinstead to handle the case where looking up the current directory fails.fn try_from_path(path: impl Into<PathBuf>) -> Result<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.
Relative paths are resolved relative to the current working directory. If the passed path is empty or if the current working directory cannot be determined, this function returns an error.
NOTE: this function does not check if the target path exists.
Trait Implementations
impl AsRef<OsStr> for TempPath
fn as_ref(&self) -> &OsStr
impl AsRef<Path> for TempPath
fn as_ref(&self) -> &Path
impl Debug for TempPath
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Deref for TempPath
type Target = Path;fn deref(&self) -> &Path
impl Drop for TempPath
fn drop(&mut self)
impl From<PathPersistError> for TempPath
fn from(error: PathPersistError) -> TempPath
Auto Trait Implementations
impl Freeze for 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
Blanket Implementations
impl<P, T> Receiver for TempPath
where
P: Deref<Target = T> + ?Sized,
T: ?Sized,
type Target = T;
impl<T> Any for TempPath
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for TempPath
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for TempPath
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> From<T> for TempPath
fn from(t: T) -> TReturns the argument unchanged.
impl<T, U> Into<U> for TempPath
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for TempPath
where
U: Into<T>,
type Error = never;fn try_from(value: U) -> Result<T, never>
impl<T, U> TryInto<U> for TempPath
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>