1use crate::global::GlobalExecutor;
2use crate::{
3 AbortableJoinHandle, CommunicationTask, Executor, ExecutorBlocking, JoinHandle,
4 UnboundedCommunicationTask,
5};
6use futures::channel::mpsc::{Receiver, UnboundedReceiver};
7use std::future::Future;
8use std::pin::Pin;
9use std::task::{Context, Poll};
10
11const EXECUTOR: GlobalExecutor = GlobalExecutor;
12
13pub fn spawn<F>(future: F) -> JoinHandle<F::Output>
15where
16 F: Future + Send + 'static,
17 F::Output: Send + 'static,
18{
19 EXECUTOR.spawn(future)
20}
21
22pub fn spawn_blocking<F, T>(future: F) -> JoinHandle<T>
23where
24 F: FnOnce() -> T + Send + 'static,
25 T: Send + 'static,
26{
27 EXECUTOR.spawn_blocking(future)
28}
29
30pub fn spawn_abortable<F>(future: F) -> AbortableJoinHandle<F::Output>
36where
37 F: Future + Send + 'static,
38 F::Output: Send + 'static,
39{
40 EXECUTOR.spawn_abortable(future)
41}
42
43pub fn dispatch<F>(future: F)
46where
47 F: Future + Send + 'static,
48 F::Output: Send + 'static,
49{
50 EXECUTOR.dispatch(future);
51}
52
53pub fn spawn_coroutine<T, F, Fut>(f: F) -> CommunicationTask<T>
57where
58 F: FnMut(Receiver<T>) -> Fut,
59 Fut: Future<Output = ()> + Send + 'static,
60{
61 EXECUTOR.spawn_coroutine(f)
62}
63
64pub fn spawn_coroutine_with_buffer<T, F, Fut>(buffer: usize, f: F) -> CommunicationTask<T>
68where
69 F: FnMut(Receiver<T>) -> Fut,
70 Fut: Future<Output = ()> + Send + 'static,
71{
72 EXECUTOR.spawn_coroutine_with_buffer(buffer, f)
73}
74
75pub fn spawn_coroutine_with_context<T, F, C, Fut>(context: C, f: F) -> CommunicationTask<T>
79where
80 F: FnMut(C, Receiver<T>) -> Fut,
81 Fut: Future<Output = ()> + Send + 'static,
82{
83 EXECUTOR.spawn_coroutine_with_context(context, f)
84}
85
86pub fn spawn_coroutine_with_buffer_and_context<T, F, C, Fut>(
90 context: C,
91 buffer: usize,
92 f: F,
93) -> CommunicationTask<T>
94where
95 F: FnMut(C, Receiver<T>) -> Fut,
96 Fut: Future<Output = ()> + Send + 'static,
97{
98 EXECUTOR.spawn_coroutine_with_buffer_and_context(context, buffer, f)
99}
100
101pub fn spawn_unbounded_coroutine<T, F, Fut>(f: F) -> UnboundedCommunicationTask<T>
105where
106 F: FnMut(UnboundedReceiver<T>) -> Fut,
107 Fut: Future<Output = ()> + Send + 'static,
108{
109 EXECUTOR.spawn_unbounded_coroutine(f)
110}
111
112pub fn spawn_unbounded_coroutine_with_context<T, F, C, Fut>(
116 context: C,
117 f: F,
118) -> UnboundedCommunicationTask<T>
119where
120 F: FnMut(C, UnboundedReceiver<T>) -> Fut,
121 Fut: Future<Output = ()> + Send + 'static,
122{
123 EXECUTOR.spawn_unbounded_coroutine_with_context(context, f)
124}
125
126#[derive(Default)]
127struct Yield {
128 yielded: bool,
129}
130
131impl Future for Yield {
132 type Output = ();
133
134 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
135 if self.yielded {
136 return Poll::Ready(());
137 }
138 self.yielded = true;
139 cx.waker().wake_by_ref();
140 Poll::Pending
141 }
142}
143
144pub fn yield_now() -> impl Future<Output = ()> {
146 Yield::default()
147}