mqtt_async_client/util/
tokio_runtime.rs

1use std::future::Future;
2use tokio::{
3    self,
4    task::JoinHandle
5};
6
7/// Represents a tokio runtime on which to spawn tasks.
8#[derive(Clone, Debug)]
9pub enum TokioRuntime {
10    /// Represents the default global tokio runtime, i.e. to use [tokio::spawn](https://docs.rs/tokio/0.2.6/tokio/fn.spawn.html)
11    Default,
12
13    /// Encapsulates a [tokio::runtime::Handle](https://docs.rs/tokio/0.2.6/tokio/runtime/struct.Handle.html) to use to spawn tasks.
14    Handle(tokio::runtime::Handle),
15}
16
17impl TokioRuntime {
18    /// Spawn a task onto the selected tokio runtime.
19    pub fn spawn<F>(&self, f: F) -> JoinHandle<F::Output>
20        where
21            F: Future + Send + 'static,
22            F::Output: Send + 'static, {
23        match self {
24            TokioRuntime::Default => tokio::spawn(f),
25            TokioRuntime::Handle(h) => h.spawn(f),
26        }
27    }
28}
29
30impl Default for TokioRuntime {
31    fn default() -> TokioRuntime {
32        TokioRuntime::Default
33    }
34}