#temporary-files #file-writer #persist #write #persisted #data #temp-file

tmpfile

A temporary file writer with an option to persist

3 releases

0.0.3 Sep 21, 2024
0.0.2 Jan 29, 2024
0.0.1 Jan 25, 2024

#517 in Filesystem

Download history 520/week @ 2024-07-14 639/week @ 2024-07-21 594/week @ 2024-07-28 564/week @ 2024-08-04 556/week @ 2024-08-11 606/week @ 2024-08-18 745/week @ 2024-08-25 627/week @ 2024-09-01 579/week @ 2024-09-08 573/week @ 2024-09-15 579/week @ 2024-09-22 497/week @ 2024-09-29 405/week @ 2024-10-06 421/week @ 2024-10-13 346/week @ 2024-10-20 337/week @ 2024-10-27

1,544 downloads per month
Used in fsblobstore

0BSD license

18KB
262 lines

tmpfile

TmpFile is an object representing a temporary file that can optionally be persisted.


lib.rs:

Temporary file object that can be persisted.

Usage overview

The main type of this crate is TmpFile, which represents a (temporary) file being written to, that may optionally be persisted (or it will be removed).

To use TmpFile an application must first implement the TmpProc trait on a type. [TmpProc::update()] will be called each time a block of data is written to the TmpFile. [TmpProc::finalize()] will be called if the application chooses to persist the TmpFile.

When a TmpFile is created, the application must pass along the file's temporary location and an object whose type implements TmpProc. The instantiated TmpFile object implements std::io::Write, which is used to write data to it.

If the entire file can not be completed, the TmpFile object is dropped, which will automatically remove the temporary file.

If the entire file has been written, and the application wants to persist it, it calls [TmpFile::persist()]. This will call the TmpProc::finalize() trait method, whose responsibility it is to return the file's persistent location (and application-defined data). Information about the persisted file is finally returned to the application via an instantion of Persisted.

"Small file" special case

An application may not want to store small files in its filesystem. For this purpose, the TmpFile can be set up to have a minimum file size. If a TmpFile does not reach this size before being persisted, a memory buffer of the file's contents will be returned instead of a file name of the persisted file.

The [TmpFile::with_minsize()] factory method can be used to use this feature.

Deferred persist

There may be cases where it's impractical to call [TmpFile::persist()] on a TmpFile, but where the originator of the TmpFile wants to manage the results from the TmpFile when it is pesisted. This crate has means to handle such situations, but it drastically changes the semantics of TmpFile: The defer_persist() method returns a wait context that can be used to wait for the TmpFile to finalize and send its results. In this scenario, the finalization occurrs implicitly when the TmpFile is dropped.

This means that deferred persist shifts the default assumption of "drop-before-persist implies failure" to "drop means persist", with no means to trigger "abort without finalization" (unless the TmpProc's finalization is able to determine that the file is incomplete).

Features

Feature Function
defer-persist Allow Drop to finalize TmpFile.

Dependencies

~0–4.5MB