From eacb2e913fdbfe7af735ec0f4041599a87ad318a Mon Sep 17 00:00:00 2001 From: valbendan Date: Thu, 5 Nov 2020 11:36:23 +0800 Subject: [PATCH 1/3] upgrade tokio to latest version(v0.3) --- examples/game_of_life/Cargo.toml | 2 +- futures/Cargo.toml | 4 ++-- futures/src/executor/tokio.rs | 3 ++- src/executor.rs | 3 ++- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/examples/game_of_life/Cargo.toml b/examples/game_of_life/Cargo.toml index b9bb7f2aa4..9c4172c42c 100644 --- a/examples/game_of_life/Cargo.toml +++ b/examples/game_of_life/Cargo.toml @@ -7,6 +7,6 @@ publish = false [dependencies] iced = { path = "../..", features = ["canvas", "tokio", "debug"] } -tokio = { version = "0.2", features = ["blocking"] } +tokio = { version = "0.3", features = ["sync"] } itertools = "0.9" rustc-hash = "1.1" diff --git a/futures/Cargo.toml b/futures/Cargo.toml index 275d039140..078c96aad7 100644 --- a/futures/Cargo.toml +++ b/futures/Cargo.toml @@ -20,9 +20,9 @@ log = "0.4" version = "0.3" [target.'cfg(not(target_arch = "wasm32"))'.dependencies.tokio] -version = "0.2" +version = "0.3" optional = true -features = ["rt-core", "rt-threaded", "time", "stream"] +features = ["rt-multi-thread", "time", "stream"] [target.'cfg(not(target_arch = "wasm32"))'.dependencies.async-std] version = "1.0" diff --git a/futures/src/executor/tokio.rs b/futures/src/executor/tokio.rs index a730bce817..c6a21cec66 100644 --- a/futures/src/executor/tokio.rs +++ b/futures/src/executor/tokio.rs @@ -16,6 +16,7 @@ impl Executor for Tokio { } fn enter(&self, f: impl FnOnce() -> R) -> R { - tokio::runtime::Runtime::enter(self, f) + let _guard = tokio::runtime::Runtime::enter(self); + f() } } diff --git a/src/executor.rs b/src/executor.rs index ea6ab14afe..34538fb679 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -40,7 +40,8 @@ mod platform { } fn enter(&self, f: impl FnOnce() -> R) -> R { - self.0.enter(f) + let _guard = self.0.enter(); + f() } } } From a4ad1b297e97d6c3efc196681822f127efc954cc Mon Sep 17 00:00:00 2001 From: dabaichi Date: Fri, 6 Nov 2020 04:20:19 +0000 Subject: [PATCH 2/3] Update src/executor.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix executor other than tokio Co-authored-by: Héctor Ramón --- src/executor.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/executor.rs b/src/executor.rs index 34538fb679..13a3990b6c 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -40,8 +40,7 @@ mod platform { } fn enter(&self, f: impl FnOnce() -> R) -> R { - let _guard = self.0.enter(); - f() + super::Executor::enter(&self.0, f) } } } From 782dd2f5222bfef5e12aa576a821da21126505b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Wed, 25 Nov 2020 03:06:24 +0100 Subject: [PATCH 3/3] 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. --- Cargo.toml | 2 ++ examples/download_progress/Cargo.toml | 2 +- examples/pokedex/Cargo.toml | 2 +- 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 +++++++- src/executor.rs | 16 +++++++++++++--- src/lib.rs | 11 +++++++++-- 10 files changed, 67 insertions(+), 9 deletions(-) create mode 100644 futures/src/executor/tokio_old.rs diff --git a/Cargo.toml b/Cargo.toml index e201a4b65d..90ac75bd8b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,6 +33,8 @@ glow_default_system_font = ["iced_glow/default_system_font"] debug = ["iced_winit/debug"] # Enables `tokio` as the `executor::Default` on native platforms tokio = ["iced_futures/tokio"] +# Enables old `tokio` (0.2) as the `executor::Default` on native platforms +tokio_old = ["iced_futures/tokio_old"] # Enables `async-std` as the `executor::Default` on native platforms async-std = ["iced_futures/async-std"] # Enables advanced color conversion via `palette` diff --git a/examples/download_progress/Cargo.toml b/examples/download_progress/Cargo.toml index 34e6a132a3..4b05e7dc10 100644 --- a/examples/download_progress/Cargo.toml +++ b/examples/download_progress/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" publish = false [dependencies] -iced = { path = "../..", features = ["tokio"] } +iced = { path = "../..", features = ["tokio_old"] } iced_native = { path = "../../native" } iced_futures = { path = "../../futures" } reqwest = "0.10" diff --git a/examples/pokedex/Cargo.toml b/examples/pokedex/Cargo.toml index 943200860e..05e739929d 100644 --- a/examples/pokedex/Cargo.toml +++ b/examples/pokedex/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" publish = false [dependencies] -iced = { path = "../..", features = ["image", "debug", "tokio"] } +iced = { path = "../..", features = ["image", "debug", "tokio_old"] } serde_json = "1.0" [dependencies.serde] diff --git a/futures/Cargo.toml b/futures/Cargo.toml index 078c96aad7..7854dbb09a 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 cbd34ee8bb..13abe4308d 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 0000000000..d64729fa53 --- /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 46fc59fcf4..f46db44915 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 e87b4a83e4..ec007d887c 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) diff --git a/src/executor.rs b/src/executor.rs index 13a3990b6c..0333bc1df3 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -7,13 +7,23 @@ pub use platform::Default; mod platform { use iced_futures::{executor, futures}; - #[cfg(feature = "tokio")] + #[cfg(feature = "tokio_old")] + type Executor = executor::TokioOld; + + #[cfg(all(not(feature = "tokio_old"), feature = "tokio"))] type Executor = executor::Tokio; - #[cfg(all(not(feature = "tokio"), feature = "async-std"))] + #[cfg(all( + not(any(feature = "tokio_old", feature = "tokio")), + feature = "async-std" + ))] type Executor = executor::AsyncStd; - #[cfg(not(any(feature = "tokio", feature = "async-std")))] + #[cfg(not(any( + feature = "tokio_old", + feature = "tokio", + feature = "async-std" + )))] type Executor = executor::ThreadPool; /// A default cross-platform executor. diff --git a/src/lib.rs b/src/lib.rs index 610683b368..6b61d29751 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -193,10 +193,17 @@ pub mod widget; pub mod window; #[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"))))] +#[cfg_attr( + docsrs, + doc(cfg(any( + feature = "tokio", + feature = "tokio_old", + feature = "async-std" + ))) +)] pub mod time; #[cfg(all(