fix(swarm): Make executors pub and add convenience constructors for SwarmBuilder (#3155)

Addresses issues raised here: https://github.com/libp2p/rust-libp2p/pull/3097#discussion_r1026526312
This commit is contained in:
Hannes 2022-11-23 19:19:22 +01:00 committed by GitHub
parent 2a5a5180e9
commit 76dba1773b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 2 deletions

View File

@ -1,3 +1,4 @@
//! Provides executors for spawning background tasks.
use futures::executor::ThreadPool;
use std::{future::Future, pin::Pin};
@ -30,7 +31,7 @@ impl Executor for ThreadPool {
not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown"))
))]
#[derive(Default, Debug, Clone, Copy)]
pub(crate) struct TokioExecutor;
pub struct TokioExecutor;
#[cfg(all(
feature = "tokio",
@ -47,7 +48,7 @@ impl Executor for TokioExecutor {
not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown"))
))]
#[derive(Default, Debug, Clone, Copy)]
pub(crate) struct AsyncStdExecutor;
pub struct AsyncStdExecutor;
#[cfg(all(
feature = "async-std",

View File

@ -1456,6 +1456,44 @@ where
}
}
/// Builds a new [`SwarmBuilder`] from the given transport, behaviour, local peer ID and a
/// `tokio` executor.
#[cfg(all(
feature = "tokio",
not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown"))
))]
pub fn with_tokio_executor(
transport: transport::Boxed<(PeerId, StreamMuxerBox)>,
behaviour: TBehaviour,
local_peer_id: PeerId,
) -> Self {
Self::with_executor(
transport,
behaviour,
local_peer_id,
crate::executor::TokioExecutor,
)
}
/// Builds a new [`SwarmBuilder`] from the given transport, behaviour, local peer ID and a
/// `async-std` executor.
#[cfg(all(
feature = "async-std",
not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown"))
))]
pub fn with_async_std_executor(
transport: transport::Boxed<(PeerId, StreamMuxerBox)>,
behaviour: TBehaviour,
local_peer_id: PeerId,
) -> Self {
Self::with_executor(
transport,
behaviour,
local_peer_id,
crate::executor::AsyncStdExecutor,
)
}
/// Creates a new [`SwarmBuilder`] from the given transport, behaviour and local peer ID. The
/// `Swarm` with its underlying `Network` is obtained via [`SwarmBuilder::build`].
///