feat(executor): add cosmic::executor::Default
This commit is contained in:
parent
a1b3d6ec38
commit
cc21b9baa1
4 changed files with 13 additions and 7 deletions
7
src/executor/mod.rs
Normal file
7
src/executor/mod.rs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
pub mod single;
|
||||
|
||||
#[cfg(not(feature = "tokio"))]
|
||||
pub type Default = iced::executor::Default;
|
||||
|
||||
#[cfg(feature = "tokio")]
|
||||
pub type Default = single::Executor;
|
||||
28
src/executor/single.rs
Normal file
28
src/executor/single.rs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
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> {
|
||||
// Current thread executor requires calling `block_on` to actually run
|
||||
// futures. Main thread is busy with things other than running futures,
|
||||
// so spawn a single worker thread.
|
||||
Ok(Self(
|
||||
tokio::runtime::Builder::new_multi_thread()
|
||||
.worker_threads(1)
|
||||
.enable_all()
|
||||
.build()?,
|
||||
))
|
||||
}
|
||||
|
||||
fn spawn(&self, future: impl Future<Output = ()> + Send + 'static) {
|
||||
let _ = self.0.spawn(future);
|
||||
}
|
||||
|
||||
fn enter<R>(&self, f: impl FnOnce() -> R) -> R {
|
||||
let _guard = self.0.enter();
|
||||
f()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue