Skip to content

Commit

Permalink
docs: enhance code documentation
Browse files Browse the repository at this point in the history
Pull-Request: #5198.
  • Loading branch information
DoTheBestToGetTheBest committed Apr 25, 2024
1 parent cc432ca commit 66e56fa
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions libp2p/src/builder/phase/provider.rs
@@ -1,46 +1,60 @@
#[allow(unused_imports)]
use super::*;

use crate::SwarmBuilder;
use std::marker::PhantomData;
/// Represents the phase where a provider is not yet specified.
/// This is a marker type used in the type-state pattern to ensure compile-time checks of the builder's state.
pub enum NoProviderSpecified {}

// Define enums for each of the possible runtime environments. These are used as markers in the type-state pattern,
// allowing compile-time checks for the appropriate environment configuration.

#[cfg(all(not(target_arch = "wasm32"), feature = "async-std"))]
/// Represents the AsyncStd runtime environment.
pub enum AsyncStd {}

#[cfg(all(not(target_arch = "wasm32"), feature = "tokio"))]
/// Represents the Tokio runtime environment.
pub enum Tokio {}

#[cfg(feature = "wasm-bindgen")]
/// Represents the WasmBindgen environment for WebAssembly.
pub enum WasmBindgen {}

/// Represents a phase in the SwarmBuilder where a provider has been chosen but not yet specified.
pub struct ProviderPhase {}

impl SwarmBuilder<NoProviderSpecified, ProviderPhase> {
/// Configures the SwarmBuilder to use the AsyncStd runtime.
/// This method is only available when compiling for non-Wasm targets with the `async-std` feature enabled.
#[cfg(all(not(target_arch = "wasm32"), feature = "async-std"))]
pub fn with_async_std(self) -> SwarmBuilder<AsyncStd, TcpPhase> {
SwarmBuilder {
keypair: self.keypair,
phantom: std::marker::PhantomData,
phantom: PhantomData,
phase: TcpPhase {},
}
}

/// Configures the SwarmBuilder to use the Tokio runtime.
/// This method is only available when compiling for non-Wasm targets with the `tokio` feature enabled
#[cfg(all(not(target_arch = "wasm32"), feature = "tokio"))]
pub fn with_tokio(self) -> SwarmBuilder<Tokio, TcpPhase> {
SwarmBuilder {
keypair: self.keypair,
phantom: std::marker::PhantomData,
phantom: PhantomData,
phase: TcpPhase {},
}
}

/// Configures the SwarmBuilder for WebAssembly using WasmBindgen.
/// This method is available when the `wasm-bindgen` feature is enabled.
#[cfg(feature = "wasm-bindgen")]
pub fn with_wasm_bindgen(self) -> SwarmBuilder<WasmBindgen, TcpPhase> {
SwarmBuilder {
keypair: self.keypair,
phantom: std::marker::PhantomData,
phantom: PhantomData,
phase: TcpPhase {},
}
}
}

pub enum NoProviderSpecified {}

#[cfg(all(not(target_arch = "wasm32"), feature = "async-std"))]
pub enum AsyncStd {}

#[cfg(all(not(target_arch = "wasm32"), feature = "tokio"))]
pub enum Tokio {}

#[cfg(feature = "wasm-bindgen")]
pub enum WasmBindgen {}

0 comments on commit 66e56fa

Please sign in to comment.