Skip to content

Commit

Permalink
fix(tokio): add safe access join handles (#85)
Browse files Browse the repository at this point in the history
Fixes: #84
  • Loading branch information
j-mendez authored Jun 25, 2024
1 parent ab5f1c9 commit 146a593
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 133 deletions.
31 changes: 20 additions & 11 deletions src/async_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ pub fn unwrap_joinhandle_value<T>(value: T) -> T {
pub use tokio::task::JoinHandle;
#[cfg(feature = "tokio")]
#[inline]
pub fn unwrap_joinhandle_value<T>(value: Result<T, tokio::task::JoinError>) -> T {
value.unwrap()
pub fn unwrap_joinhandle_value<T>(value: T) -> T {
value
}

use tempfile::NamedTempFile;
Expand All @@ -110,19 +110,28 @@ use crate::errors::IoErrorExt;

#[cfg(feature = "async-std")]
#[inline]
pub async fn create_named_tempfile(tmp_path: std::path::PathBuf) -> crate::Result<NamedTempFile> {
pub async fn create_named_tempfile(
tmp_path: std::path::PathBuf,
) -> Option<crate::Result<NamedTempFile>> {
let cloned = tmp_path.clone();
spawn_blocking(|| NamedTempFile::new_in(tmp_path))
.await
.with_context(|| format!("Failed to create a temp file at {}", cloned.display()))

Some(
spawn_blocking(|| NamedTempFile::new_in(tmp_path))
.await
.with_context(|| format!("Failed to create a temp file at {}", cloned.display())),
)
}

#[cfg(feature = "tokio")]
#[inline]
pub async fn create_named_tempfile(tmp_path: std::path::PathBuf) -> crate::Result<NamedTempFile> {
pub async fn create_named_tempfile(
tmp_path: std::path::PathBuf,
) -> Option<crate::Result<NamedTempFile>> {
let cloned = tmp_path.clone();
spawn_blocking(|| NamedTempFile::new_in(tmp_path))
.await
.unwrap()
.with_context(|| format!("Failed to create a temp file at {}", cloned.display()))
match spawn_blocking(|| NamedTempFile::new_in(tmp_path)).await {
Ok(ctx) => Some(
ctx.with_context(|| format!("Failed to create a temp file at {}", cloned.display())),
),
_ => None,
}
}
Loading

0 comments on commit 146a593

Please sign in to comment.