Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying a custom executor #2580

Merged
merged 4 commits into from
Sep 18, 2024

Conversation

mtkennerly
Copy link
Contributor

As discussed on Discord, with the move away from the Application trait, we lost the ability to set a custom Executor. This PR restores that functionality.

Test case:

Diff
diff --git a/examples/clock/Cargo.toml b/examples/clock/Cargo.toml
index bc6c202b..fb8053d4 100644
--- a/examples/clock/Cargo.toml
+++ b/examples/clock/Cargo.toml
@@ -10,3 +10,4 @@ iced.workspace = true
 iced.features = ["canvas", "tokio", "debug"]
 chrono = "0.4"
 tracing-subscriber = "0.3"
+tokio.workspace = true
diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs
index ef3064c7..0a97803b 100644
--- a/examples/clock/src/main.rs
+++ b/examples/clock/src/main.rs
@@ -15,6 +15,7 @@ pub fn main() -> iced::Result {
         .subscription(Clock::subscription)
         .theme(Clock::theme)
         .antialiasing(true)
+        .executor::<Executor>()
         .run()
 }

@@ -166,3 +167,28 @@ fn hand_rotation(n: u32, total: u32) -> Degrees {

     Degrees(360.0 * turns)
 }
+
+pub struct Executor(tokio::runtime::Runtime);
+
+impl iced::Executor for Executor {
+    fn new() -> Result<Self, iced::futures::io::Error> {
+        println!("Custom executor is active");
+        tokio::runtime::Builder::new_multi_thread()
+            .enable_all()
+            .build()
+            .map(Self)
+    }
+
+    #[allow(clippy::let_underscore_future)]
+    fn spawn(
+        &self,
+        future: impl std::future::Future<Output = ()> + Send + 'static,
+    ) {
+        let _ = tokio::runtime::Runtime::spawn(&self.0, future);
+    }
+
+    fn enter<R>(&self, f: impl FnOnce() -> R) -> R {
+        let _guard = tokio::runtime::Runtime::enter(&self.0);
+        f()
+    }
+}
Output
$ cargo run -p clock
   Compiling clock v0.1.0 (C:\git\_forks\iced\examples\clock)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 4.28s
     Running `target\debug\clock.exe`
Custom executor is active

@hecrj hecrj added this to the 0.13.1 milestone Sep 18, 2024
@hecrj hecrj added feature New feature or request addition labels Sep 18, 2024
Copy link
Member

@hecrj hecrj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Looks great.

@hecrj hecrj merged commit db7ba95 into iced-rs:master Sep 18, 2024
12 checks passed
@mtkennerly mtkennerly deleted the feature/custom-executor branch September 21, 2024 19:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants