From 782dd2f5222bfef5e12aa576a821da21126505b7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 25 Nov 2020 03:06:24 +0100 Subject: Introduce `tokio_old` feature This feature allows users to rely on the `0.2` version of `tokio` while the async ecosystem upgrades to the latest version. --- futures/Cargo.toml | 6 ++++++ futures/src/executor.rs | 6 ++++++ futures/src/executor/tokio_old.rs | 21 +++++++++++++++++++++ futures/src/lib.rs | 2 +- futures/src/time.rs | 8 +++++++- 5 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 futures/src/executor/tokio_old.rs (limited to 'futures') diff --git a/futures/Cargo.toml b/futures/Cargo.toml index 078c96aa..7854dbb0 100644 --- a/futures/Cargo.toml +++ b/futures/Cargo.toml @@ -19,6 +19,12 @@ log = "0.4" [dependencies.futures] version = "0.3" +[target.'cfg(not(target_arch = "wasm32"))'.dependencies.tokio_old] +package = "tokio" +version = "0.2" +optional = true +features = ["rt-core", "rt-threaded", "time", "stream"] + [target.'cfg(not(target_arch = "wasm32"))'.dependencies.tokio] version = "0.3" optional = true diff --git a/futures/src/executor.rs b/futures/src/executor.rs index cbd34ee8..13abe430 100644 --- a/futures/src/executor.rs +++ b/futures/src/executor.rs @@ -7,6 +7,9 @@ mod thread_pool; #[cfg(all(not(target_arch = "wasm32"), feature = "tokio"))] mod tokio; +#[cfg(all(not(target_arch = "wasm32"), feature = "tokio_old"))] +mod tokio_old; + #[cfg(all(not(target_arch = "wasm32"), feature = "async-std"))] mod async_std; @@ -21,6 +24,9 @@ pub use thread_pool::ThreadPool; #[cfg(all(not(target_arch = "wasm32"), feature = "tokio"))] pub use self::tokio::Tokio; +#[cfg(all(not(target_arch = "wasm32"), feature = "tokio_old"))] +pub use self::tokio_old::TokioOld; + #[cfg(all(not(target_arch = "wasm32"), feature = "async-std"))] pub use self::async_std::AsyncStd; diff --git a/futures/src/executor/tokio_old.rs b/futures/src/executor/tokio_old.rs new file mode 100644 index 00000000..d64729fa --- /dev/null +++ b/futures/src/executor/tokio_old.rs @@ -0,0 +1,21 @@ +use crate::Executor; + +use futures::Future; + +/// An old `tokio` runtime. +#[cfg_attr(docsrs, doc(cfg(feature = "tokio_old")))] +pub type TokioOld = tokio_old::runtime::Runtime; + +impl Executor for TokioOld { + fn new() -> Result { + tokio_old::runtime::Runtime::new() + } + + fn spawn(&self, future: impl Future + Send + 'static) { + let _ = tokio_old::runtime::Runtime::spawn(self, future); + } + + fn enter(&self, f: impl FnOnce() -> R) -> R { + tokio_old::runtime::Runtime::enter(self, f) + } +} diff --git a/futures/src/lib.rs b/futures/src/lib.rs index 46fc59fc..f46db449 100644 --- a/futures/src/lib.rs +++ b/futures/src/lib.rs @@ -15,7 +15,7 @@ pub mod executor; pub mod subscription; #[cfg(all( - any(feature = "tokio", feature = "async-std"), + any(feature = "tokio", feature = "tokio_old", feature = "async-std"), not(target_arch = "wasm32") ))] #[cfg_attr(docsrs, doc(cfg(any(feature = "tokio", feature = "async-std"))))] diff --git a/futures/src/time.rs b/futures/src/time.rs index e87b4a83..ec007d88 100644 --- a/futures/src/time.rs +++ b/futures/src/time.rs @@ -41,7 +41,10 @@ where } } -#[cfg(all(feature = "tokio", not(feature = "async-std")))] +#[cfg(all( + any(feature = "tokio", feature = "tokio_old"), + not(feature = "async-std") +))] impl subscription::Recipe for Every where H: std::hash::Hasher, @@ -61,6 +64,9 @@ where ) -> futures::stream::BoxStream<'static, Self::Output> { use futures::stream::StreamExt; + #[cfg(feature = "tokio_old")] + use tokio_old as tokio; + let start = tokio::time::Instant::now() + self.0; tokio::time::interval_at(start, self.0) -- cgit