cosmic/executor/
multi.rs

1// Copyright 2023 System76 <info@system76.com>
2// SPDX-License-Identifier: MPL-2.0
3
4//! An async executor that schedules tasks across a pol ofbackground thread.
5
6use std::future::Future;
7
8#[cfg(feature = "tokio")]
9pub struct Executor(tokio::runtime::Runtime);
10
11#[cfg(feature = "tokio")]
12impl iced::Executor for Executor {
13    fn new() -> Result<Self, iced::futures::io::Error> {
14        Ok(Self(
15            tokio::runtime::Builder::new_multi_thread()
16                .enable_all()
17                .build()?,
18        ))
19    }
20
21    fn spawn(&self, future: impl Future<Output = ()> + Send + 'static) {
22        let _res = self.0.spawn(future);
23    }
24
25    fn enter<R>(&self, f: impl FnOnce() -> R) -> R {
26        let _guard = self.0.enter();
27        f()
28    }
29}