Skip to main content

rx_rust/scheduler/
tokio_scheduler.rs

1use super::Scheduler;
2use crate::{
3    disposable::{Disposable, bound_drop_disposal::BoundDropDisposal},
4    utils::types::MaybeSend,
5};
6use std::time::Duration;
7
8/// Leverages a Tokio runtime handle to drive scheduled tasks.
9impl Scheduler for tokio::runtime::Handle {
10    type D = tokio::task::JoinHandle<()>;
11
12    fn spawn_future(
13        &self,
14        future: impl Future<Output = ()> + MaybeSend + 'static,
15    ) -> BoundDropDisposal<Self::D> {
16        let handle = self.spawn(future);
17        BoundDropDisposal::new(handle)
18    }
19
20    fn sleep(&self, duration: Duration) -> impl Future + MaybeSend + 'static + use<> {
21        // Enter the runtime so the timer can be created outside a runtime context.
22        let _guard = self.enter();
23        tokio::time::sleep(duration)
24    }
25}
26
27impl Disposable for tokio::task::JoinHandle<()> {
28    fn dispose(self) {
29        self.abort();
30    }
31}