async_tempfile/
lib.rs

1//! # async-tempfile
2//!
3//! Provides the [`TempFile`] struct, an asynchronous wrapper based on `tokio::fs` for temporary
4//! files that will be automatically deleted when the last reference to the struct is dropped.
5//!
6//! ```
7//! use async_tempfile::TempFile;
8//!
9//! #[tokio::main]
10//! async fn main() {
11//!     let parent = TempFile::new().await.unwrap();
12//!
13//!     // The cloned reference will not delete the file when dropped.
14//!     {
15//!         let nested = parent.open_rw().await.unwrap();
16//!         assert_eq!(nested.file_path(), parent.file_path());
17//!         assert!(nested.file_path().is_file());
18//!     }
19//!
20//!     // The file still exists; it will be deleted when `parent` is dropped.
21//!     assert!(parent.file_path().is_file());
22//! }
23//! ```
24//!
25//! ## Features
26//!
27//! * `uuid` - (Default) Enables random file name generation based on the [`uuid`](https://crates.io/crates/uuid) crate.
28//!            Provides the `new` and `new_in`, as well as the `new_with_uuid*` group of methods.
29
30// Document crate features on docs.rs.
31#![cfg_attr(docsrs, feature(doc_cfg))]
32// Required for dropping the file.
33#![allow(unsafe_code)]
34
35mod errors;
36mod random_name;
37mod tempdir;
38mod tempfile;
39
40pub use errors::Error;
41#[cfg(not(feature = "uuid"))]
42pub(crate) use random_name::RandomName;
43use std::fmt::Debug;
44pub use tempdir::TempDir;
45pub use tempfile::TempFile;
46
47/// Determines the ownership of a temporary file or directory.
48#[derive(Debug, Eq, PartialEq, Copy, Clone)]
49pub enum Ownership {
50    /// The file or directory is owned by [`TempFile`] and will be deleted when
51    /// the last reference to it is dropped.
52    Owned,
53    /// The file or directory is borrowed by [`TempFile`] and will be left untouched
54    /// when the last reference to it is dropped.
55    Borrowed,
56}