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
// SPDX-License-Identifier: Apache-2.0
//! Procedural macros for go-lib.
//!
//! Exported through `go_lib` — use as `#[go_lib::main]`.
use TokenStream;
use quote;
use ;
/// Attribute macro that promotes a function's body into the program's first
/// goroutine on the process-wide go-lib scheduler.
///
/// This is the single blessed entry point: apply it to `fn main` (or any
/// entry function) instead of manually wrapping the body. The scheduler is a
/// process singleton, initialised on first use; the attribute runs the body
/// as the "main goroutine" and blocks the calling OS thread until it returns,
/// mirroring Go's `main`.
///
/// The macro rewrites
///
/// ```rust,ignore
/// #[go_lib::main]
/// fn main() {
/// /* body */
/// }
/// ```
///
/// into
///
/// ```rust,ignore
/// fn main() {
/// go_lib::__main_entry(move || {
/// /* body */
/// })
/// }
/// ```
///
/// When the function has a return type the closure is annotated with the same
/// type so that `?` and explicit `return` expressions work as expected:
///
/// ```rust,ignore
/// #[go_lib::main]
/// fn main() -> Result<(), MyError> {
/// do_work()?;
/// Ok(())
/// }
/// // expands to:
/// fn main() -> Result<(), MyError> {
/// go_lib::__main_entry(move || -> Result<(), MyError> {
/// do_work()?;
/// Ok(())
/// })
/// }
/// ```
///
/// Function parameters (if any) are captured by the `move` closure, so the
/// macro also works on non-`main` entry points or helper functions.
///
/// # Errors
///
/// Emits a compile error if the function is `async` (go-lib provides its own
/// concurrency model and does not interact with an async executor).