summaryrefslogtreecommitdiffstats
path: root/src/executor.rs
blob: cbbd8283b7652c405ddde5e5b3eaa486ec666f42 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! Choose your preferred executor to power your application.
pub use crate::common::{executor::Null, Executor};

pub use platform::Default;

#[cfg(not(target_arch = "wasm32"))]
mod platform {
    use iced_winit::{executor::ThreadPool, futures, Executor};

    /// A default cross-platform executor.
    ///
    /// - On native platforms, it will use a `iced_futures::executor::ThreadPool`.
    /// - On the Web, it will use `iced_futures::executor::WasmBindgen`.
    #[derive(Debug)]
    pub struct Default(ThreadPool);

    impl Executor for Default {
        fn new() -> Result<Self, futures::io::Error> {
            Ok(Default(ThreadPool::new()?))
        }

        fn spawn(
            &self,
            future: impl futures::Future<Output = ()> + Send + 'static,
        ) {
            self.0.spawn(future);
        }
    }
}

#[cfg(target_arch = "wasm32")]
mod platform {
    use iced_web::{executor::WasmBindgen, futures, Executor};

    /// A default cross-platform executor.
    ///
    /// - On native platforms, it will use a `iced_futures::executor::ThreadPool`.
    /// - On the Web, it will use `iced_futures::executor::WasmBindgen`.
    #[derive(Debug)]
    pub struct Default(WasmBindgen);

    impl Executor for Default {
        fn new() -> Result<Self, futures::io::Error> {
            Ok(Default(WasmBindgen::new()?))
        }

        fn spawn(
            &self,
            future: impl futures::Future<Output = ()> + Send + 'static,
        ) {
            self.0.spawn(future);
        }
    }
}