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 = "async-std"))]
15/// Represents the AsyncStd runtime environment.
16pub enum AsyncStd {}
17
18#[cfg(all(not(target_arch = "wasm32"), feature = "tokio"))]
19/// Represents the Tokio runtime environment.
20pub enum Tokio {}
21
22#[cfg(feature = "wasm-bindgen")]
23/// Represents the WasmBindgen environment for WebAssembly.
24pub enum WasmBindgen {}
25
26/// Represents a phase in the SwarmBuilder where a provider has been chosen but not yet specified.
27pub struct ProviderPhase {}
28
29impl SwarmBuilder<NoProviderSpecified, ProviderPhase> {
30    /// Configures the SwarmBuilder to use the AsyncStd runtime.
31    /// This method is only available when compiling for non-Wasm
32    /// targets with the `async-std` feature enabled.
33    #[cfg(all(not(target_arch = "wasm32"), feature = "async-std"))]
34    pub fn with_async_std(self) -> SwarmBuilder<AsyncStd, TcpPhase> {
35        SwarmBuilder {
36            keypair: self.keypair,
37            phantom: PhantomData,
38            phase: TcpPhase {},
39        }
40    }
41
42    /// Configures the SwarmBuilder to use the Tokio runtime.
43    /// This method is only available when compiling for non-Wasm
44    /// targets with the `tokio` feature enabled
45    #[cfg(all(not(target_arch = "wasm32"), feature = "tokio"))]
46    pub fn with_tokio(self) -> SwarmBuilder<Tokio, TcpPhase> {
47        SwarmBuilder {
48            keypair: self.keypair,
49            phantom: PhantomData,
50            phase: TcpPhase {},
51        }
52    }
53
54    /// Configures the SwarmBuilder for WebAssembly using WasmBindgen.
55    /// This method is available when the `wasm-bindgen` feature is enabled.
56    #[cfg(feature = "wasm-bindgen")]
57    pub fn with_wasm_bindgen(self) -> SwarmBuilder<WasmBindgen, TcpPhase> {
58        SwarmBuilder {
59            keypair: self.keypair,
60            phantom: PhantomData,
61            phase: TcpPhase {},
62        }
63    }
64}