discv5/
executor.rs

1//! A simple trait to allow generic executors or wrappers for spawning the discv5 tasks.
2use std::{future::Future, pin::Pin};
3
4pub trait Executor: ExecutorClone {
5    /// Run the given future in the background until it ends.
6    fn spawn(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>);
7}
8
9pub trait ExecutorClone {
10    fn clone_box(&self) -> Box<dyn Executor + Send + Sync>;
11}
12
13impl<T> ExecutorClone for T
14where
15    T: 'static + Executor + Clone + Send + Sync,
16{
17    fn clone_box(&self) -> Box<dyn Executor + Send + Sync> {
18        Box::new(self.clone())
19    }
20}
21
22impl Clone for Box<dyn Executor + Send + Sync> {
23    fn clone(&self) -> Box<dyn Executor + Send + Sync> {
24        self.clone_box()
25    }
26}
27
28#[derive(Clone)]
29pub struct TokioExecutor;
30
31impl Executor for TokioExecutor {
32    fn spawn(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) {
33        tokio::task::spawn(future);
34    }
35}
36
37impl Default for TokioExecutor {
38    fn default() -> Self {
39        TokioExecutor
40    }
41}