From 3e94498741e61cd460ef12685d1229eeca6250e8 Mon Sep 17 00:00:00 2001 From: Marc-Antoine Perennou Date: Tue, 22 Sep 2020 17:07:45 +0200 Subject: [PATCH] fix tokio compatibility Move it into async-global-executor Fixes #881 Signed-off-by: Marc-Antoine Perennou --- Cargo.toml | 4 ++-- src/task/builder.rs | 2 +- src/task/executor.rs | 41 ----------------------------------------- src/task/mod.rs | 2 -- 4 files changed, 3 insertions(+), 46 deletions(-) delete mode 100644 src/task/executor.rs diff --git a/Cargo.toml b/Cargo.toml index 6ea6275d..2503750b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,7 +57,7 @@ alloc = [ "futures-core/alloc", "pin-project-lite", ] -tokio02 = ["tokio"] +tokio02 = ["async-global-executor/tokio02"] [dependencies] async-attributes = { version = "1.1.1", optional = true } @@ -78,7 +78,7 @@ slab = { version = "0.4.2", optional = true } surf = { version = "1.0.3", optional = true } [target.'cfg(not(target_os = "unknown"))'.dependencies] -async-global-executor = { version = "1.0.2", optional = true, features = ["async-io"] } +async-global-executor = { version = "1.2.1", optional = true, features = ["async-io"] } async-io = { version = "1.0.1", optional = true } blocking = { version = "1.0.0", optional = true } futures-lite = { version = "1.0.0", optional = true } diff --git a/src/task/builder.rs b/src/task/builder.rs index 8e69a10c..aba0d611 100644 --- a/src/task/builder.rs +++ b/src/task/builder.rs @@ -168,7 +168,7 @@ impl Builder { TaskLocalsWrapper::set_current(&wrapped.tag, || { let res = if should_run { // The first call should run the executor - crate::task::executor::run(wrapped) + async_global_executor::block_on(wrapped) } else { futures_lite::future::block_on(wrapped) }; diff --git a/src/task/executor.rs b/src/task/executor.rs deleted file mode 100644 index 0cd05032..00000000 --- a/src/task/executor.rs +++ /dev/null @@ -1,41 +0,0 @@ -use std::future::Future; - -pub(crate) fn run(future: F) -> T -where - F: Future, -{ - enter(|| async_global_executor::block_on(future)) -} - -/// Enters the tokio context if the `tokio` feature is enabled. -fn enter(f: impl FnOnce() -> T) -> T { - #[cfg(not(feature = "tokio02"))] - return f(); - - #[cfg(feature = "tokio02")] - { - use std::cell::Cell; - use tokio::runtime::Runtime; - - thread_local! { - /// The level of nested `enter` calls we are in, to ensure that the outermost always - /// has a runtime spawned. - static NESTING: Cell = Cell::new(0); - } - - /// The global tokio runtime. - static RT: once_cell::sync::Lazy = once_cell::sync::Lazy::new(|| Runtime::new().expect("cannot initialize tokio")); - - NESTING.with(|nesting| { - let res = if nesting.get() == 0 { - nesting.replace(1); - RT.enter(f) - } else { - nesting.replace(nesting.get() + 1); - f() - }; - nesting.replace(nesting.get() - 1); - res - }) - } -} diff --git a/src/task/mod.rs b/src/task/mod.rs index b528a788..ca0b92a0 100644 --- a/src/task/mod.rs +++ b/src/task/mod.rs @@ -148,8 +148,6 @@ cfg_default! { mod block_on; mod builder; mod current; - #[cfg(not(target_os = "unknown"))] - pub(crate) mod executor; mod join_handle; mod sleep; #[cfg(not(target_os = "unknown"))]