Skip to main content

async_flow/tokio/scheduler/
parallel.rs

1// This is free and unencumbered software released into the public domain.
2
3use crate::Scheduler;
4use tokio::runtime::{Builder, Runtime};
5
6#[derive(Debug)]
7pub struct ParallelScheduler {
8    runtime: Runtime,
9}
10
11impl Scheduler for ParallelScheduler {}
12
13impl ParallelScheduler {
14    pub fn new() -> std::io::Result<Self> {
15        let runtime = Builder::new_multi_thread().enable_all().build()?;
16        Ok(Self { runtime })
17    }
18
19    #[cfg(feature = "tokio")]
20    pub fn id(&self) -> tokio::runtime::Id {
21        self.runtime.handle().id()
22    }
23}
24
25#[cfg(feature = "tokio")]
26impl AsRef<tokio::runtime::Runtime> for ParallelScheduler {
27    fn as_ref(&self) -> &tokio::runtime::Runtime {
28        &self.runtime
29    }
30}
31
32#[cfg(feature = "tokio")]
33impl AsRef<tokio::runtime::Handle> for ParallelScheduler {
34    fn as_ref(&self) -> &tokio::runtime::Handle {
35        self.runtime.handle()
36    }
37}