feat(executor): add multi::Executor

This commit is contained in:
Michael Aaron Murphy 2023-01-27 03:58:39 +01:00 committed by Michael Murphy
parent cc21b9baa1
commit e4280dd381
3 changed files with 38 additions and 1 deletions

View file

@ -1,3 +1,10 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
#[cfg(feature = "tokio")]
pub mod multi;
#[cfg(feature = "tokio")]
pub mod single;
#[cfg(not(feature = "tokio"))]

27
src/executor/multi.rs Normal file
View file

@ -0,0 +1,27 @@
// Copyright 2023 System76 <info@system76.com>
// 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<Self, iced::futures::io::Error> {
Ok(Self(
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()?,
))
}
fn spawn(&self, future: impl Future<Output = ()> + Send + 'static) {
let _res = self.0.spawn(future);
}
fn enter<R>(&self, f: impl FnOnce() -> R) -> R {
let _guard = self.0.enter();
f()
}
}

View file

@ -1,3 +1,6 @@
// Copyright 2023 System76 <info@system76.com>
// 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<Output = ()> + Send + 'static) {
let _ = self.0.spawn(future);
let _res = self.0.spawn(future);
}
fn enter<R>(&self, f: impl FnOnce() -> R) -> R {