From df60b3b91cd3887c0674ff5066a99e14fd5ba1ef Mon Sep 17 00:00:00 2001 From: umgefahren <55623006+umgefahren@users.noreply.github.com> Date: Tue, 22 Nov 2022 11:35:06 +0100 Subject: [PATCH] Make executors pub and add convenience constructors for SwarmBuilder --- swarm/src/executor.rs | 5 +++-- swarm/src/lib.rs | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/swarm/src/executor.rs b/swarm/src/executor.rs index 0d815cc2129..dc17c174c48 100644 --- a/swarm/src/executor.rs +++ b/swarm/src/executor.rs @@ -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", diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index c467b99c25b..003a14120b9 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -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`]. ///