libp2p/builder/phase/
swarm.rs

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
#[allow(unused_imports)]
use super::*;

#[allow(dead_code)]
pub struct SwarmPhase<T, B> {
    pub(crate) behaviour: B,
    pub(crate) transport: T,
}

macro_rules! impl_with_swarm_config {
    ($providerKebabCase:literal, $providerPascalCase:ty, $config:expr) => {
        #[cfg(feature = $providerKebabCase)]
        impl<T, B> SwarmBuilder<$providerPascalCase, SwarmPhase<T, B>> {
            pub fn with_swarm_config(
                self,
                constructor: impl FnOnce(libp2p_swarm::Config) -> libp2p_swarm::Config,
            ) -> SwarmBuilder<$providerPascalCase, BuildPhase<T, B>> {
                SwarmBuilder {
                    phase: BuildPhase {
                        behaviour: self.phase.behaviour,
                        transport: self.phase.transport,
                        swarm_config: constructor($config),
                    },
                    keypair: self.keypair,
                    phantom: std::marker::PhantomData,
                }
            }

            // Shortcuts
            pub fn build(self) -> libp2p_swarm::Swarm<B>
            where
                B: libp2p_swarm::NetworkBehaviour,
                T: AuthenticatedMultiplexedTransport,
            {
                self.with_swarm_config(std::convert::identity).build()
            }
        }
    };
}

#[cfg(not(target_arch = "wasm32"))]
impl_with_swarm_config!(
    "async-std",
    super::provider::AsyncStd,
    libp2p_swarm::Config::with_async_std_executor()
);

#[cfg(not(target_arch = "wasm32"))]
impl_with_swarm_config!(
    "tokio",
    super::provider::Tokio,
    libp2p_swarm::Config::with_tokio_executor()
);

#[cfg(target_arch = "wasm32")]
impl_with_swarm_config!(
    "wasm-bindgen",
    super::provider::WasmBindgen,
    libp2p_swarm::Config::with_wasm_executor()
);