2023-01-27 03:58:39 +01:00
|
|
|
// Copyright 2023 System76 <info@system76.com>
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
2023-08-15 10:51:59 +02:00
|
|
|
//! An async executor that schedules tasks across a pol ofbackground thread.
|
|
|
|
|
|
2023-01-27 03:58:39 +01:00
|
|
|
use std::future::Future;
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "tokio")]
|
|
|
|
|
pub struct Executor(tokio::runtime::Runtime);
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "tokio")]
|
2023-05-30 12:03:15 -04:00
|
|
|
impl iced::Executor for Executor {
|
2023-01-27 03:58:39 +01:00
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
}
|