libp2p/builder/phase/
swarm.rs

1#[allow(unused_imports)]
2use super::*;
3
4#[allow(unused)] // used below but due to feature flag combinations, clippy gives an unnecessary warning.
5const DEFAULT_CONNECTION_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);
6
7#[allow(dead_code)]
8pub struct SwarmPhase<T, B> {
9    pub(crate) behaviour: B,
10    pub(crate) transport: T,
11}
12
13macro_rules! impl_with_swarm_config {
14    ($providerKebabCase:literal, $providerPascalCase:ty, $config:expr) => {
15        #[cfg(feature = $providerKebabCase)]
16        impl<T, B> SwarmBuilder<$providerPascalCase, SwarmPhase<T, B>> {
17            pub fn with_swarm_config(
18                self,
19                constructor: impl FnOnce(libp2p_swarm::Config) -> libp2p_swarm::Config,
20            ) -> SwarmBuilder<$providerPascalCase, BuildPhase<T, B>> {
21                SwarmBuilder {
22                    phase: BuildPhase {
23                        behaviour: self.phase.behaviour,
24                        transport: self.phase.transport,
25                        swarm_config: constructor($config),
26                        connection_timeout: DEFAULT_CONNECTION_TIMEOUT,
27                    },
28                    keypair: self.keypair,
29                    phantom: std::marker::PhantomData,
30                }
31            }
32
33            // Shortcuts
34            pub fn build(self) -> libp2p_swarm::Swarm<B>
35            where
36                B: libp2p_swarm::NetworkBehaviour,
37                T: AuthenticatedMultiplexedTransport,
38            {
39                self.with_swarm_config(std::convert::identity).build()
40            }
41        }
42    };
43}
44
45#[cfg(not(target_arch = "wasm32"))]
46impl_with_swarm_config!(
47    "async-std",
48    super::provider::AsyncStd,
49    libp2p_swarm::Config::with_async_std_executor()
50);
51
52#[cfg(not(target_arch = "wasm32"))]
53impl_with_swarm_config!(
54    "tokio",
55    super::provider::Tokio,
56    libp2p_swarm::Config::with_tokio_executor()
57);
58
59#[cfg(target_arch = "wasm32")]
60impl_with_swarm_config!(
61    "wasm-bindgen",
62    super::provider::WasmBindgen,
63    libp2p_swarm::Config::with_wasm_executor()
64);