1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// SPDX-License-Identifier: MIT OR Apache-2.0
/*!
An executor-agnostic program entry point.
[`ExecutorMain`] is the trait spelling of "construct the chosen executor, install it,
and run this future." It exists so an attribute macro can expand to something that
compiles against a backend the macro has never heard of:
```text
#[some_executor::main(some_executor_tokio::TokioExecutor)]
async fn main() { .. }
```
expands to roughly
```text
fn main() {
<some_executor_tokio::TokioExecutor as some_executor::ExecutorMain>::main(async { .. })
}
```
The macro cannot know an inherent method on an arbitrary backend type, so the call has
to go through a trait this crate defines. That is the whole reason [`ExecutorMain`] is
separate from [`block_on`](fn@crate::block_on): `block_on` is for code that already has an
executor, and this is for code that is choosing one.
# Why it returns `()`
An entry point cannot hand a value back on every platform. On the wasm32 main thread
`fn main` returns while the program keeps running on the event loop, so there is no
moment at which a result could be produced — the honest signature there is "start this
and return." Making the trait return `()` everywhere is what lets a wasm backend
implement it at all, and therefore what makes `#[some_executor::main]` portable.
An `async fn main` that wants to return a `Result` handles it inside the future:
```
# use std::future::Future;
# fn wrapper<E: some_executor::ExecutorMain>() {
E::main(async {
if let Err(e) = fallible().await {
eprintln!("error: {e}");
std::process::exit(1);
}
});
# }
# async fn fallible() -> Result<(), String> { Ok(()) }
```
That is what the attribute macro generates for a fallible `main`.
# Implementing it
A backend that can block does the obvious three steps -- construct, install globally,
drive -- and [`run_main`] packages them, so the whole implementation is one line:
```
use some_executor::{SomeExecutorExt, entry_point::run_main};
use std::future::Future;
// The body a blocking backend writes for `ExecutorMain::main`:
fn main_impl<E, F>(future: F)
where
E: SomeExecutorExt + Default + 'static,
F: Future<Output = ()> + 'static,
{
run_main(E::default(), future)
}
```
A wasm32 main-thread backend cannot block, so it installs itself and hands the future to
the event loop instead — which is exactly why the return type is `()`:
```text
fn main<F: Future<Output = ()> + 'static>(future: F) {
set_global_executor(Box::new(MyWasmExecutor::new()));
wasm_lite_std::spawn_local(future);
}
```
*/
use crateSomeExecutorExt;
use crateset_global_executor;
use Debug;
use Future;
/// Constructs an executor, installs it, and runs `future` as the program's main task.
///
/// Implemented by executor backends and called by `#[some_executor::main]`. See the
/// [module documentation](self) for why this is an associated function returning `()`
/// rather than a method returning the future's output.
///
/// # Contract
///
/// - Called at most once per process, from `fn main`, before any other executor is
/// installed.
/// - The implementation should install itself via
/// [`set_global_executor`] so that code
/// anywhere in the program can spawn.
/// - It should run `future` to completion where it can, and — where it cannot, such as
/// the wasm32 main thread — hand it to the platform's event loop and return.
/// The ordinary [`ExecutorMain::main`] body for a backend that can block: install
/// `executor` globally, then drive `future` on it.
///
/// # Panics
///
/// Panics if a global executor is already set, which
/// [`set_global_executor`] does not allow.
/// That is the right behavior for an entry point — two of them in one process is a bug —
/// but it does mean this function cannot be called twice, including from two tests in
/// the same process.
///
/// Also panics on the wasm32 main thread, which cannot block; see
/// [`block_on`](fn@crate::block_on).
/// The backend `#[some_executor::main]` uses when no argument is given.
///
/// This is the crate's built-in fallback executor, which is always available
/// and needs no dependency. It is meant for quick demos, examples and
/// doctests; it warns on first use precisely because it is not what a real
/// program should be running on.
///
/// ```no_run
/// # // no_run because: the attribute generates this doctest's own `fn main`, so
/// # // running it would install a process-wide executor inside the test harness.
/// #[some_executor::main]
/// async fn main() {
/// println!("no backend named, so this ran on LastResort");
/// }
/// ```
///
/// # Platform behaviour
///
/// It blocks, so on the wasm32 main thread it panics like anything else that
/// blocks there. A wasm32 program names a backend that can hand its future to
/// the event loop instead; that is the case [`ExecutorMain`] returning `()`
/// exists for.
;
/// What `#[some_executor::main]` does with whatever an `async fn main`
/// returned.
///
/// The macro always emits `MainResult::report(..)` and lets the compiler pick
/// the impl, so it never has to inspect the declared return type — and a `main`
/// returning something unsupported gets "`MainResult` is not implemented for
/// `X`" rather than a mystery inside an expansion nobody wrote.
///
/// Putting the policy here rather than in generated code is deliberate: it is
/// documented, it is tested, and changing it does not mean changing what every
/// existing crate's `fn main` expands to.
/// An infallible `main` has nothing to report.
/// A fallible `main` logs its error and brings the process down.
///
/// Panicking rather than `std::process::exit(1)`: an exit code is meaningless
/// on the wasm32 main thread, where `fn main` returns while the program keeps
/// running, and a panic is the one failure signal both targets have. The error
/// is logged first because a panic payload is a `&str` — the `Debug` rendering
/// would otherwise be the only copy, and on wasm32 a panic under `panic_abort`
/// does not unwind to anywhere that could print it.