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
//! *enjoin* - async joining at the syntax level
//!
//! The macros provided in this crate take blocks of code and run them
//! concurrently. The macros try to make the blocks work in the same way that
//! regular blocks (that are not being run concurrently) work.
//!
//! ## Syntax
//! ```
//! # async {
//! let (res_1, res_2) = enjoin::join!(
//! {
//! // Code goes here
//! },
//! {
//! // Code goes here
//! }
//! );
//! # };
//! ```
//!
//! The macro takes blocks of code and execute them concurrently.
//! Any number of blocks may be supplied.
//!
//! The blocks are regular blocks, not async blocks.
//! The blocks can contain async code (i.e. you can use `.await` in the blocks).
//!
//! The results are returned as a tuple.
//!
//! ## Features
//!
//! This features are things that you can already do in regular blocks.
//! They are being listed here because they don't work in async blocks
//! (which other join implementations rely on).
//!
//! ### Branching statements support
//!
//! You can use `break`, `continue`, and `return` inside the blocks.
//!
//! ```
//! # async {
//! let x = 'a: loop {
//! enjoin::join!(
//! {
//! // do something
//! break 'a 5;
//! },
//! {
//! // do something else
//! continue;
//! },
//! {
//! // do something
//! return;
//! }
//! );
//! };
//! # };
//! ```
//!
//! The `break`/`continue` may be labeled or unlabeled.
//! `break`-ing with a value is supported.
//!
//! You can, of course, still use `break` and `continue` for
//! loops or blocks contained inside the join macro.
//!
//! As in regular Rust, unlabeled `break`/`return` effects
//! the innermost loop,
//! and labeled `break`/`return` effects the innermost loop with that label.
//!
//! Returning from inside the join will cause the current function to return.
//!
//! If the branching statement causes execution to jump out of the macro,
//! all the code executing in the macro will be stopped and dropped.
//!
//! ### Try operator (`?`) support
//!
//! You can use the `?` operator inside your blocks just as you would outside
//! a join macro.
//!
//! ```
//! async fn f() -> Result<i32, &'static str> {
//! enjoin::join!(
//! {
//! let a = Ok(5);
//! let b = Err("oh no");
//! let c = a?;
//! b?; // causes the `f` function to return `Err("oh no")`
//! },
//! {
//! // ...
//! }
//! );
//! unreachable!("would have returned error at `b?`")
//! }
//! ```
//!
//! ### Shared borrowing support
//!
//! If two or more blocks mutably borrow the same value, the `join_auto_borrow!`
//! macro will automatically put that value in a RefCell.
//! (`join_auto_borrow!` also do everything `join!` does)
//!
//! ```
//! # async {
//! let mut count = 0;
//! enjoin::join_auto_borrow!(
//! {
//! // ...
//! count += 1;
//! },
//! {
//! count -= 1;
//! }
//! );
//! # };
//! ```
//!
//! The macro makes sure the RefCell will never panic by disallowing
//! shared borrows from lasting across await yieldpoints.
//!
//! ```compile_fail
//! # async {
//! let mut count = 0;
//! enjoin::join_auto_borrow!(
//! {
//! let borrow = &mut count;
//! std::future::ready(123).await; // <- borrow crosses this yieldpoint
//! drop(borrow);
//! },
//! {
//! count += 1;
//! }
//! );
//! # };
//! ```
//!
//! ## More information
//!
//! There is [a blog post](https://wishawa.github.io/posts/enjoin) detailing
//! why this macro was made and how it works.
//!
//! The source code is [here on GitHub](https://github.com/wishawa/enjoin).
//!
//! ## Troubleshooting
//!
//! * If branching statements and/or captured variables are hidden
//! in another macro, *enjoin* wouldn't be able to transform them.
//! This will usually cause compilation failure.
//!
//! ```rust
//! # async {
//! enjoin::join!({ vec![
//! 1, 2, 3
//! // enjoin can't see the code in this vec!
//! ] });
//! # };
//! ```
//!
//! ---
//!
//! * If an `await` is hidden inside a macro, `join_auto_borrow!` won't be able
//! to unlock the RefCell for the yieldpoint, leading to a RefCell panic.
//! This limitation means you can't nest `enjoin::join!` or `tokio::join!`
//! within `enjoin::join_auto_borrow!`.
//!
//! ---
//!
//! * With only syntactic information, *enjoin* can only guess whether or not a
//! name is a borrowed variable, and whether or not that borrow is mutable.
//! We have heuristics, but even so the macro may end up RefCell-ing
//! immutable borrows, constants, or function pointers sometimes.
//! You can help the macro by writing `(&mut var).method()` or
//! `(&var).method()` instead of `var.method()`.
//!
pub use ;