Skip to content

Commit 06b2c9e

Browse files
committed
Add drop_async function
1 parent 72cc3e5 commit 06b2c9e

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
All notable changes to this project will be documented in this file.
44
This project uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
55

6+
## [0.7.0] - 2025-02-22
7+
8+
[0.7.0]: https://github.com/sunsided/async-tempfile-rs/releases/tag/v0.7.0
9+
10+
### Added
11+
12+
- Added the `drop_async` functions to provide controlled and `async` dropping.
13+
614
## [0.6.0] - 2024-06-30
715

816
[0.6.0]: https://github.com/sunsided/async-tempfile-rs/releases/tag/v0.6.0

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "async-tempfile"
3-
version = "0.6.0"
3+
version = "0.7.0"
44
description = "Automatically deleted async I/O temporary files."
55
documentation = "https://docs.rs/async-tempfile"
66
license = "MIT"
@@ -24,7 +24,7 @@ tokio = { version = "1.38.0", features = ["fs"] }
2424
uuid = { version = "1.9.1", features = ["v4"], optional = true }
2525

2626
[dev-dependencies]
27-
tokio = { version = "1.38.0", features = ["rt-multi-thread", "macros"] }
27+
tokio = { version = "1.38.0", features = ["rt-multi-thread", "macros", "rt"] }
2828
tokio-test = "0.4.4"
2929

3030
[package.metadata.docs.rs]

src/tempdir.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,31 @@ impl TempDir {
301301
self.core.ownership
302302
}
303303

304+
/// Asynchronously drops the [`TempDir`] instance by moving the drop operation
305+
/// to a blocking thread, avoiding potential blocking of the async runtime.
306+
///
307+
/// This method is useful in cases where manually handling the blocking drop
308+
/// within an async context is required.
309+
///
310+
/// ## Example
311+
/// ```
312+
/// # use async_tempfile::TempDir;
313+
/// # let _ = tokio_test::block_on(async {
314+
/// let dir = TempDir::new().await?;
315+
///
316+
/// // Drop the directory asynchronously.
317+
/// dir.drop_async().await;
318+
///
319+
/// // The directory is now removed.
320+
/// # Ok::<(), Box<dyn std::error::Error>>(())
321+
/// # });
322+
/// ```
323+
///
324+
/// Note: This function spawns a blocking task for the drop operation.
325+
pub async fn drop_async(self) {
326+
tokio::task::spawn_blocking(move || drop(self)).await.ok();
327+
}
328+
304329
async fn new_internal<P: Borrow<Path>>(path: P, ownership: Ownership) -> Result<Self, Error> {
305330
// Create the directory and all its parents.
306331
tokio::fs::create_dir_all(path.borrow()).await?;

src/tempfile.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,29 @@ impl TempFile {
372372
self.core.ownership
373373
}
374374

375+
/// Asynchronously drops the TempFile, ensuring any resources are properly released.
376+
/// This is useful for explicitly managing the lifecycle of the TempFile
377+
/// in an asynchronous context.
378+
///
379+
/// ## Example
380+
///
381+
/// ```rust
382+
/// # use async_tempfile::{TempFile, Error};
383+
/// # let _ = tokio_test::block_on(async {
384+
/// let file = TempFile::new().await?;
385+
/// let path = file.file_path().to_path_buf();
386+
/// assert!(path.is_file());
387+
///
388+
/// file.drop_async().await; // Explicitly drop the TempFile
389+
///
390+
/// assert!(!path.exists());
391+
/// # Ok::<(), Error>(())
392+
/// # });
393+
/// ```
394+
pub async fn drop_async(self) {
395+
tokio::task::spawn_blocking(move || drop(self)).await.ok();
396+
}
397+
375398
async fn new_internal<P: Borrow<Path>>(path: P, ownership: Ownership) -> Result<Self, Error> {
376399
let path = path.borrow();
377400

0 commit comments

Comments
 (0)