Download Evaluate/projects/libp2p/rust/swarm/src/executor.rs from SYSUSELab/RustRepoTrans: direct link, hf CLI and curl.
- Browser
- Download file 2.39 kB
-
https://huggingface.co/datasets/SYSUSELab/RustRepoTrans/resolve/main/Evaluate/projects/libp2p/rust/swarm/src/executor.rs
- Command line
-
hf download hf://datasets/SYSUSELab/RustRepoTrans/Evaluate/projects/libp2p/rust/swarm/src/executor.rs
-
curl -L -o executor.rs https://huggingface.co/datasets/SYSUSELab/RustRepoTrans/resolve/main/Evaluate/projects/libp2p/rust/swarm/src/executor.rs
2.39 kB
| //! Provides executors for spawning background tasks. | |
| use futures::executor::ThreadPool; | |
| use std::{future::Future, pin::Pin}; | |
| /// Implemented on objects that can run a `Future` in the background. | |
| /// | |
| /// > **Note**: While it may be tempting to implement this trait on types such as | |
| /// > [`futures::stream::FuturesUnordered`], please note that passing an `Executor` is | |
| /// > optional, and that `FuturesUnordered` (or a similar struct) will automatically | |
| /// > be used as fallback by libp2p. The `Executor` trait should therefore only be | |
| /// > about running `Future`s on a separate task. | |
| pub trait Executor { | |
| /// Run the given future in the background until it ends. | |
| fn exec(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>); | |
| } | |
| impl<F: Fn(Pin<Box<dyn Future<Output = ()> + Send>>)> Executor for F { | |
| fn exec(&self, f: Pin<Box<dyn Future<Output = ()> + Send>>) { | |
| self(f) | |
| } | |
| } | |
| impl Executor for ThreadPool { | |
| fn exec(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) { | |
| self.spawn_ok(future) | |
| } | |
| } | |
| pub(crate) struct TokioExecutor; | |
| impl Executor for TokioExecutor { | |
| fn exec(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) { | |
| tokio::spawn(future); | |
| } | |
| } | |
| pub(crate) struct AsyncStdExecutor; | |
| impl Executor for AsyncStdExecutor { | |
| fn exec(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) { | |
| async_std::task::spawn(future); | |
| } | |
| } | |
| pub(crate) struct WasmBindgenExecutor; | |
| impl Executor for WasmBindgenExecutor { | |
| fn exec(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) { | |
| wasm_bindgen_futures::spawn_local(future) | |
| } | |
| } | |