libp2p/builder/phase/
provider.rs

1use std::marker::PhantomData;
2
3#[allow(unused_imports)]
4use super::*;
5use crate::SwarmBuilder;
6/// Represents the phase where a provider is not yet specified.
7/// This is a marker type used in the type-state pattern to ensure compile-time checks of the
8/// builder's state.
9pub enum NoProviderSpecified {}
10
11// Define enums for each of the possible runtime environments. These are used as markers in the
12// type-state pattern, allowing compile-time checks for the appropriate environment configuration.
13
14#[cfg(all(not(target_arch = "wasm32"), feature = "tokio"))]
15/// Represents the Tokio runtime environment.
16pub enum Tokio {}
17
18#[cfg(feature = "wasm-bindgen")]
19/// Represents the WasmBindgen environment for WebAssembly.
20pub enum WasmBindgen {}
21
22/// Represents a phase in the SwarmBuilder where a provider has been chosen but not yet specified.
23pub struct ProviderPhase {}
24
25impl SwarmBuilder<NoProviderSpecified, ProviderPhase> {
26    /// Configures the SwarmBuilder to use the Tokio runtime.
27    /// This method is only available when compiling for non-Wasm
28    /// targets with the `tokio` feature enabled
29    #[cfg(all(not(target_arch = "wasm32"), feature = "tokio"))]
30    pub fn with_tokio(self) -> SwarmBuilder<Tokio, TcpPhase> {
31        SwarmBuilder {
32            keypair: self.keypair,
33            phantom: PhantomData,
34            phase: TcpPhase {},
35        }
36    }
37
38    /// Configures the SwarmBuilder for WebAssembly using WasmBindgen.
39    /// This method is available when the `wasm-bindgen` feature is enabled.
40    #[cfg(feature = "wasm-bindgen")]
41    pub fn with_wasm_bindgen(self) -> SwarmBuilder<WasmBindgen, TcpPhase> {
42        SwarmBuilder {
43            keypair: self.keypair,
44            phantom: PhantomData,
45            phase: TcpPhase {},
46        }
47    }
48}