diff --git a/src/executor/mod.rs b/src/executor/mod.rs index 5d53af80..1dd67e84 100644 --- a/src/executor/mod.rs +++ b/src/executor/mod.rs @@ -1,3 +1,10 @@ +// Copyright 2023 System76 +// SPDX-License-Identifier: MPL-2.0 + +#[cfg(feature = "tokio")] +pub mod multi; + +#[cfg(feature = "tokio")] pub mod single; #[cfg(not(feature = "tokio"))] diff --git a/src/executor/multi.rs b/src/executor/multi.rs new file mode 100644 index 00000000..a6f07318 --- /dev/null +++ b/src/executor/multi.rs @@ -0,0 +1,27 @@ +// Copyright 2023 System76 +// SPDX-License-Identifier: MPL-2.0 + +use std::future::Future; + +#[cfg(feature = "tokio")] +pub struct Executor(tokio::runtime::Runtime); + +#[cfg(feature = "tokio")] +impl iced_native::Executor for Executor { + fn new() -> Result { + Ok(Self( + tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build()?, + )) + } + + fn spawn(&self, future: impl Future + Send + 'static) { + let _res = self.0.spawn(future); + } + + fn enter(&self, f: impl FnOnce() -> R) -> R { + let _guard = self.0.enter(); + f() + } +} diff --git a/src/executor/single.rs b/src/executor/single.rs index ac09d0e7..e293fc1d 100644 --- a/src/executor/single.rs +++ b/src/executor/single.rs @@ -1,3 +1,6 @@ +// Copyright 2023 System76 +// SPDX-License-Identifier: MPL-2.0 + use std::future::Future; #[cfg(feature = "tokio")] @@ -18,7 +21,7 @@ impl iced_native::Executor for Executor { } fn spawn(&self, future: impl Future + Send + 'static) { - let _ = self.0.spawn(future); + let _res = self.0.spawn(future); } fn enter(&self, f: impl FnOnce() -> R) -> R {