Skip to content

Commit

Permalink
Compatibility with file systems.
Browse files Browse the repository at this point in the history
Signed-off-by: csh <[email protected]>
  • Loading branch information
L-jasmine authored and CaptainVincent committed Dec 1, 2024
1 parent 1f48654 commit bc7e791
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
29 changes: 18 additions & 11 deletions tokio/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,21 +297,28 @@ cfg_windows! {

use std::io;

#[cfg(not(test))]
use crate::blocking::spawn_blocking;
#[cfg(test)]
use mocks::spawn_blocking;

pub(crate) async fn asyncify<F, T>(f: F) -> io::Result<T>
where
F: FnOnce() -> io::Result<T> + Send + 'static,
T: Send + 'static,
{
match spawn_blocking(f).await {
Ok(res) => res,
Err(_) => Err(io::Error::new(
io::ErrorKind::Other,
"background task failed",
)),
#[cfg(not(all(tokio_unstable, target_os = "wasi")))]
{
#[cfg(not(test))]
use crate::blocking::spawn_blocking;
#[cfg(test)]
use mocks::spawn_blocking;

match spawn_blocking(f).await {
Ok(res) => res,
Err(_) => Err(io::Error::new(
io::ErrorKind::Other,
"background task failed",
)),
}
}
#[cfg(all(tokio_unstable, target_os = "wasi"))]
{
f().map_err(|_| io::Error::new(io::ErrorKind::Other, "background task failed"))
}
}
1 change: 1 addition & 0 deletions tokio/src/macros/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ macro_rules! cfg_fs {
($($item:item)*) => {
$(
#[cfg(feature = "fs")]
#[cfg(any(tokio_unstable, not(target_os = "wasi")))]
#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
$item
)*
Expand Down
6 changes: 5 additions & 1 deletion tokio/src/runtime/blocking/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,11 @@ where
R: Send + 'static,
{
let rt = Handle::current();
rt.spawn_blocking(func)
if cfg!(all(tokio_unstable, target_os = "wasi")) {
rt.spawn(async { func() })
} else {
rt.spawn_blocking(func)
}
}

cfg_fs! {
Expand Down

0 comments on commit bc7e791

Please sign in to comment.