Skip to content

Commit 146a593

Browse files
authored
fix(tokio): add safe access join handles (#85)
Fixes: #84
1 parent ab5f1c9 commit 146a593

File tree

2 files changed

+192
-133
lines changed

2 files changed

+192
-133
lines changed

src/async_lib.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ pub fn unwrap_joinhandle_value<T>(value: T) -> T {
100100
pub use tokio::task::JoinHandle;
101101
#[cfg(feature = "tokio")]
102102
#[inline]
103-
pub fn unwrap_joinhandle_value<T>(value: Result<T, tokio::task::JoinError>) -> T {
104-
value.unwrap()
103+
pub fn unwrap_joinhandle_value<T>(value: T) -> T {
104+
value
105105
}
106106

107107
use tempfile::NamedTempFile;
@@ -110,19 +110,28 @@ use crate::errors::IoErrorExt;
110110

111111
#[cfg(feature = "async-std")]
112112
#[inline]
113-
pub async fn create_named_tempfile(tmp_path: std::path::PathBuf) -> crate::Result<NamedTempFile> {
113+
pub async fn create_named_tempfile(
114+
tmp_path: std::path::PathBuf,
115+
) -> Option<crate::Result<NamedTempFile>> {
114116
let cloned = tmp_path.clone();
115-
spawn_blocking(|| NamedTempFile::new_in(tmp_path))
116-
.await
117-
.with_context(|| format!("Failed to create a temp file at {}", cloned.display()))
117+
118+
Some(
119+
spawn_blocking(|| NamedTempFile::new_in(tmp_path))
120+
.await
121+
.with_context(|| format!("Failed to create a temp file at {}", cloned.display())),
122+
)
118123
}
119124

120125
#[cfg(feature = "tokio")]
121126
#[inline]
122-
pub async fn create_named_tempfile(tmp_path: std::path::PathBuf) -> crate::Result<NamedTempFile> {
127+
pub async fn create_named_tempfile(
128+
tmp_path: std::path::PathBuf,
129+
) -> Option<crate::Result<NamedTempFile>> {
123130
let cloned = tmp_path.clone();
124-
spawn_blocking(|| NamedTempFile::new_in(tmp_path))
125-
.await
126-
.unwrap()
127-
.with_context(|| format!("Failed to create a temp file at {}", cloned.display()))
131+
match spawn_blocking(|| NamedTempFile::new_in(tmp_path)).await {
132+
Ok(ctx) => Some(
133+
ctx.with_context(|| format!("Failed to create a temp file at {}", cloned.display())),
134+
),
135+
_ => None,
136+
}
128137
}

0 commit comments

Comments
 (0)