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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! Provides executors for spawning background tasks.
use futures::executor::ThreadPool;
use std::{future::Future, pin::Pin};

/// Implemented on objects that can run a `Future` in the background.
///
/// > **Note**: While it may be tempting to implement this trait on types such as
/// >           [`futures::stream::FuturesUnordered`], please note that passing an `Executor` is
/// >           optional, and that `FuturesUnordered` (or a similar struct) will automatically
/// >           be used as fallback by libp2p. The `Executor` trait should therefore only be
/// >           about running `Future`s on a separate task.
pub trait Executor {
    /// Run the given future in the background until it ends.
    #[track_caller]
    fn exec(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>);
}

impl<F: Fn(Pin<Box<dyn Future<Output = ()> + Send>>)> Executor for F {
    fn exec(&self, f: Pin<Box<dyn Future<Output = ()> + Send>>) {
        self(f)
    }
}

impl Executor for ThreadPool {
    fn exec(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) {
        self.spawn_ok(future)
    }
}

#[cfg(all(
    feature = "tokio",
    not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown"))
))]
#[derive(Default, Debug, Clone, Copy)]
pub(crate) struct TokioExecutor;
#[cfg(all(
    feature = "tokio",
    not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown"))
))]
impl Executor for TokioExecutor {
    fn exec(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) {
        tokio::spawn(future);
    }
}

#[cfg(all(
    feature = "async-std",
    not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown"))
))]
#[derive(Default, Debug, Clone, Copy)]
pub(crate) struct AsyncStdExecutor;
#[cfg(all(
    feature = "async-std",
    not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown"))
))]
impl Executor for AsyncStdExecutor {
    fn exec(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) {
        async_std::task::spawn(future);
    }
}

#[cfg(feature = "wasm-bindgen")]
#[derive(Default, Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct WasmBindgenExecutor;
#[cfg(feature = "wasm-bindgen")]
impl Executor for WasmBindgenExecutor {
    fn exec(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) {
        wasm_bindgen_futures::spawn_local(future)
    }
}