Struct git_tempfile::Handle

source ·
pub struct Handle<Marker: Debug> { /* private fields */ }
Expand description

A registered temporary file which will delete itself on drop or if the program is receiving signals that should cause it to terminate.

Note

Signals interrupting the calling thread right after taking ownership of the registered tempfile will cause all but this tempfile to be removed automatically. In the common case it will persist on disk as destructors were not called or didn’t get to remove the file.

In the best case the file is a true temporary with a non-clashing name that ‘only’ fills up the disk, in the worst case the temporary file is used as a lock file which may leave the repository in a locked state forever.

This kind of raciness exists whenever take() is used and can’t be circumvented.

Implementations§

source§

impl Handle<Writable>

source

pub fn persist( self, path: impl AsRef<Path> ) -> Result<Option<File>, Error<Writable>>

Persist this tempfile to replace the file at the given path if necessary, in a way that recovers the original instance on error or returns the open now persisted former tempfile. Note that it might not exist anymore if an interrupt handler managed to steal it and allowed the program to return to its normal flow.

source§

impl Handle<Closed>

source

pub fn persist(self, path: impl AsRef<Path>) -> Result<(), Error<Closed>>

Persist this tempfile to replace the file at the given path if necessary, in a way that recovers the original instance on error.

source§

impl Handle<Closed>

Creation and ownership transfer

source

pub fn at( path: impl AsRef<Path>, directory: ContainingDirectory, cleanup: AutoRemove ) -> Result<Self>

Create a registered tempfile at the given path, where path includes the desired filename and close it immediately.

Depending on the directory configuration, intermediate directories will be created, and depending on cleanup empty intermediate directories will be removed.

source

pub fn take(self) -> Option<TempPath>

Take ownership of the temporary file path, which deletes it when dropped without persisting it beforehand.

It’s a theoretical possibility that the file isn’t present anymore if signals interfere, hence the Option

source§

impl Handle<Writable>

Creation and ownership transfer

source

pub fn at( path: impl AsRef<Path>, directory: ContainingDirectory, cleanup: AutoRemove ) -> Result<Self>

Create a registered tempfile at the given path, where path includes the desired filename.

Depending on the directory configuration, intermediate directories will be created, and depending on cleanup empty intermediate directories will be removed.

source

pub fn new( containing_directory: impl AsRef<Path>, directory: ContainingDirectory, cleanup: AutoRemove ) -> Result<Self>

Create a registered tempfile within containing_directory with a name that won’t clash, and clean it up as specified with cleanup. Control how to deal with intermediate directories with directory. The temporary file is opened and can be written to using the with_mut() method.

source

pub fn take(self) -> Option<NamedTempFile>

Take ownership of the temporary file.

It’s a theoretical possibility that the file isn’t present anymore if signals interfere, hence the Option

source

pub fn close(self) -> Result<Handle<Closed>>

Close the underlying file handle but keep track of the temporary file as before for automatic cleanup.

This saves system resources in situations where one opens a tempfile file at a time, writes a new value, and closes it right after to perform more updates of this kind in other tempfiles. When all succeed, they can be renamed one after another.

source§

impl Handle<Writable>

Mutation

source

pub fn with_mut<T>( &mut self, once: impl FnOnce(&mut NamedTempFile) -> T ) -> Result<T>

Obtain a mutable handler to the underlying named tempfile and call f(&mut named_tempfile) on it.

Note that for the duration of the call, a signal interrupting the operation will cause the tempfile not to be cleaned up as it is not visible anymore to the signal handler.

Assumptions

The caller must assure that the signal handler for cleanup will be followed by an abort call so that this code won’t run again on a removed instance. An error will occur otherwise.

Trait Implementations§

source§

impl<Marker: Debug + Debug> Debug for Handle<Marker>

source§

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

Formats the value using the given formatter. Read more
source§

impl<T: Debug> Drop for Handle<T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Read for Handle<Writable>

source§

fn read(&mut self, buf: &mut [u8]) -> Result<usize>

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
1.36.0 · source§

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>

Like read, except that it reads into a slice of buffers. Read more
source§

fn is_read_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
1.0.0 · source§

fn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>

Read all bytes until EOF in this source, placing them into buf. Read more
1.0.0 · source§

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

Read all bytes until EOF in this source, appending them to buf. Read more
1.6.0 · source§

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

Read the exact number of bytes required to fill buf. Read more
source§

fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
source§

fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Read the exact number of bytes required to fill cursor. Read more
1.0.0 · source§

fn by_ref(&mut self) -> &mut Selfwhere Self: Sized,

Creates a “by reference” adaptor for this instance of Read. Read more
1.0.0 · source§

fn bytes(self) -> Bytes<Self>where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · source§

fn chain<R>(self, next: R) -> Chain<Self, R>where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 · source§

fn take(self, limit: u64) -> Take<Self>where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
source§

impl Seek for Handle<Writable>

source§

fn seek(&mut self, pos: SeekFrom) -> Result<u64>

Seek to an offset, in bytes, in a stream. Read more
1.55.0 · source§

fn rewind(&mut self) -> Result<(), Error>

Rewind to the beginning of a stream. Read more
source§

fn stream_len(&mut self) -> Result<u64, Error>

🔬This is a nightly-only experimental API. (seek_stream_len)
Returns the length of this stream (in bytes). Read more
1.51.0 · source§

fn stream_position(&mut self) -> Result<u64, Error>

Returns the current seek position from the start of the stream. Read more
source§

impl Write for Handle<Writable>

source§

fn write(&mut self, buf: &[u8]) -> Result<usize>

Write a buffer into this writer, returning how many bytes were written. Read more
source§

fn flush(&mut self) -> Result<()>

Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · source§

fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · source§

fn by_ref(&mut self) -> &mut Selfwhere Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations§

§

impl<Marker> RefUnwindSafe for Handle<Marker>where Marker: RefUnwindSafe,

§

impl<Marker> Send for Handle<Marker>where Marker: Send,

§

impl<Marker> Sync for Handle<Marker>where Marker: Sync,

§

impl<Marker> Unpin for Handle<Marker>where Marker: Unpin,

§

impl<Marker> UnwindSafe for Handle<Marker>where Marker: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.