1use std::future::Future;
4
5use thiserror::Error;
6
7#[derive(Error, Debug)]
8pub enum ScheduleError {
9 #[error("scheduling work failed")]
10 Scheduling(Box<dyn std::error::Error>),
11 #[error("scheduler is not implemented on this platform")]
12 NotImplemented,
13}
14
15pub trait Scheduler: 'static {
18 #[cfg(feature = "thread-safe-futures")]
19 fn schedule<T>(
20 &self,
21 future_factory: impl (FnOnce() -> T) + Send + 'static,
22 ) -> Result<(), ScheduleError>
23 where
24 T: Future<Output = ()> + Send + 'static;
25
26 #[cfg(not(feature = "thread-safe-futures"))]
27 fn schedule<T>(
28 &self,
29 future_factory: impl (FnOnce() -> T) + Send + 'static,
30 ) -> Result<(), ScheduleError>
31 where
32 T: Future<Output = ()> + 'static;
33}
34
35pub struct NopScheduler;
36
37impl Scheduler for NopScheduler {
38 fn schedule<T>(
39 &self,
40 _future_factory: impl FnOnce() -> T + Send + 'static,
41 ) -> Result<(), ScheduleError>
42 where
43 T: Future<Output = ()> + 'static,
44 {
45 Err(ScheduleError::NotImplemented)
46 }
47}