1use crate::{
3 task::*,
4 time::{RuntimeTime, TimedTaskNotifier},
5};
6use pi_async_task::join::LocalOutput;
7use pi_result::RawResult;
8use std::{future::Future, time::Duration};
9
10pub trait TaskDispatch {
11 type Notifier: TaskNotifier;
12 fn create_with_config<F, M>(
14 &self,
15 future: F,
16 config: TaskConfig<M>,
17 ) -> RawResult<(UnsubmittedTask, TaskHandle<Self::Notifier, F::Output, M>), CreateError<F, M>>
18 where
19 F: Future + Send + 'static,
20 F::Output: Send + 'static,
21 M: Send + Sync + 'static;
22 fn submit(&self, task: UnsubmittedTask) -> RawResult<(), SubmitError>;
24 fn create<F>(
26 &self,
27 future: F,
28 ) -> RawResult<(UnsubmittedTask, TaskHandle<Self::Notifier, F::Output>), CreateError<F, ()>>
29 where
30 F: Future + Send + 'static,
31 F::Output: Send + 'static,
32 {
33 self.create_with_config(future, TaskConfig::default())
34 }
35 fn spawn_with_config<F, M>(
37 &self,
38 future: F,
39 config: TaskConfig<M>,
40 ) -> RawResult<TaskHandle<Self::Notifier, F::Output, M>, SpawnError<F, Self::Notifier, M>>
41 where
42 F: Future + Send + 'static,
43 F::Output: Send + 'static,
44 M: Send + Sync + 'static,
45 {
46 let (task, handle) = self
47 .create_with_config(future, config)
48 .map_err(SpawnError::Create)?;
49 match self.submit(task) {
50 Ok(()) => Ok(handle),
51 Err(error) => Err(SpawnError::Submit { error, handle }),
52 }
53 }
54 fn spawn<F>(
56 &self,
57 future: F,
58 ) -> RawResult<TaskHandle<Self::Notifier, F::Output>, SpawnError<F, Self::Notifier>>
59 where
60 F: Future + Send + 'static,
61 F::Output: Send + 'static,
62 {
63 self.spawn_with_config(future, TaskConfig::default())
64 }
65}
66
67pub trait LocalTaskDispatch: TaskDispatch {
69 fn create_local_with_config<F, M>(
71 future: F,
72 config: TaskConfig<M>,
73 ) -> RawResult<
74 (
75 UnsubmittedTask,
76 TaskHandle<Self::Notifier, F::Output, M, LocalOutput>,
77 ),
78 CreateError<F, M>,
79 >
80 where
81 F: Future + 'static,
82 F::Output: 'static,
83 M: Send + Sync + 'static;
84 fn submit_local(task: UnsubmittedTask) -> RawResult<(), SubmitError>;
86 fn create_local<F>(
88 future: F,
89 ) -> RawResult<
90 (
91 UnsubmittedTask,
92 TaskHandle<Self::Notifier, F::Output, (), LocalOutput>,
93 ),
94 CreateError<F, ()>,
95 >
96 where
97 F: Future + 'static,
98 F::Output: 'static,
99 {
100 Self::create_local_with_config(future, TaskConfig::default())
101 }
102 fn spawn_local_with_config<F, M>(
104 future: F,
105 config: TaskConfig<M>,
106 ) -> RawResult<
107 TaskHandle<Self::Notifier, F::Output, M, LocalOutput>,
108 SpawnError<F, Self::Notifier, M, LocalOutput>,
109 >
110 where
111 F: Future + 'static,
112 F::Output: 'static,
113 M: Send + Sync + 'static,
114 {
115 let (task, handle) =
116 Self::create_local_with_config(future, config).map_err(SpawnError::Create)?;
117 match Self::submit_local(task) {
118 Ok(()) => Ok(handle),
119 Err(error) => Err(SpawnError::Submit { error, handle }),
120 }
121 }
122 fn spawn_local<F>(
124 future: F,
125 ) -> RawResult<
126 TaskHandle<Self::Notifier, F::Output, (), LocalOutput>,
127 SpawnError<F, Self::Notifier, (), LocalOutput>,
128 >
129 where
130 F: Future + 'static,
131 F::Output: 'static,
132 {
133 Self::spawn_local_with_config(future, TaskConfig::default())
134 }
135}
136
137pub trait IdleDispatch: TaskDispatch {
139 fn submit_idle(&self, task: UnsubmittedTask) -> RawResult<(), SubmitError>;
141 fn idle_with_config<F, M>(
143 &self,
144 future: F,
145 config: TaskConfig<M>,
146 ) -> RawResult<TaskHandle<Self::Notifier, F::Output, M>, SpawnError<F, Self::Notifier, M>>
147 where
148 F: Future + Send + 'static,
149 F::Output: Send + 'static,
150 M: Send + Sync + 'static,
151 {
152 let (task, handle) = self
153 .create_with_config(future, config)
154 .map_err(SpawnError::Create)?;
155 match self.submit_idle(task) {
156 Ok(()) => Ok(handle),
157 Err(error) => Err(SpawnError::Submit { error, handle }),
158 }
159 }
160 fn idle<F>(
162 &self,
163 future: F,
164 ) -> RawResult<TaskHandle<Self::Notifier, F::Output>, SpawnError<F, Self::Notifier>>
165 where
166 F: Future + Send + 'static,
167 F::Output: Send + 'static,
168 {
169 self.idle_with_config(future, TaskConfig::default())
170 }
171 fn idle_local_with_config<F, M>(
173 future: F,
174 config: TaskConfig<M>,
175 ) -> RawResult<TaskHandle<Self::Notifier, F::Output, M>, SpawnError<F, Self::Notifier, M>>
176 where
177 F: Future + Send + 'static,
178 F::Output: Send + 'static,
179 M: Send + Sync + 'static;
180 fn idle_local<F>(
182 future: F,
183 ) -> RawResult<TaskHandle<Self::Notifier, F::Output>, SpawnError<F, Self::Notifier>>
184 where
185 F: Future + Send + 'static,
186 F::Output: Send + 'static,
187 {
188 Self::idle_local_with_config(future, TaskConfig::default())
189 }
190}
191
192pub trait TimedTaskDispatch: TaskDispatch + RuntimeTime {
194 fn spawn_timing_with_config<F, M>(
196 &self,
197 future: F,
198 delay: Duration,
199 config: TaskConfig<M>,
200 ) -> RawResult<
201 TaskHandle<TimedTaskNotifier<Self::Notifier, Self::Timer>, F::Output, M>,
202 SpawnError<F, TimedTaskNotifier<Self::Notifier, Self::Timer>, M>,
203 >
204 where
205 F: Future + Send + 'static,
206 F::Output: Send + 'static,
207 M: Send + Sync + 'static;
208 fn spawn_timing<F>(
210 &self,
211 future: F,
212 delay: Duration,
213 ) -> RawResult<
214 TaskHandle<TimedTaskNotifier<Self::Notifier, Self::Timer>, F::Output>,
215 SpawnError<F, TimedTaskNotifier<Self::Notifier, Self::Timer>>,
216 >
217 where
218 F: Future + Send + 'static,
219 F::Output: Send + 'static,
220 {
221 self.spawn_timing_with_config(future, delay, TaskConfig::default())
222 }
223 fn spawn_timing_local_with_config<F, M>(
225 future: F,
226 delay: Duration,
227 config: TaskConfig<M>,
228 ) -> RawResult<
229 TaskHandle<TimedTaskNotifier<Self::Notifier, Self::Timer>, F::Output, M, LocalOutput>,
230 SpawnError<F, TimedTaskNotifier<Self::Notifier, Self::Timer>, M, LocalOutput>,
231 >
232 where
233 F: Future + 'static,
234 F::Output: 'static,
235 M: Send + Sync + 'static;
236 fn spawn_timing_local<F>(
238 future: F,
239 delay: Duration,
240 ) -> RawResult<
241 TaskHandle<TimedTaskNotifier<Self::Notifier, Self::Timer>, F::Output, (), LocalOutput>,
242 SpawnError<F, TimedTaskNotifier<Self::Notifier, Self::Timer>, (), LocalOutput>,
243 >
244 where
245 F: Future + 'static,
246 F::Output: 'static,
247 {
248 Self::spawn_timing_local_with_config(future, delay, TaskConfig::default())
249 }
250}