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
use Future;
use Pin;
use ;
use ;
use Spinlock;
use crateState;
use crate::;
/// Creates a new [`CompletableFuture`] and the associated [`CompleteHandle`].
///
/// ```rust
/// # use future_handles::sync;
/// # async fn deadlocks_before_result() -> u32 {
/// let (future, handle) = sync::create();
///
/// func_with_callback(|res| {
/// handle.complete(res);
/// });
///
/// match future.await {
/// // The callback was invoked and the result set via the handle.
/// Ok(res) => res,
/// // The callback was never invoked, but the handle has been dropped.
/// Err(err) => panic!("Handle was dropped without setting a value")
/// }
/// # }
/// # fn func_with_callback<F>(func: F)
/// # where F: FnOnce(u32) {
/// # func(1);
/// # }
/// ```
///
/// # Danger!
///
/// Be careful to not await the future **before** setting the complete value or dropping the handle
/// while in the same async block, or you will cause a **deadlock**!
///
/// For a safer API, see [`scoped`].
///
/// ## Deadlock Examples
///
/// Setting the result:
/// ```rust
/// # use future_handles::sync;
/// async fn deadlocks_before_result() {
/// let (future, handle) = sync::create();
///
/// // Start awaiting here...
/// future.await.unwrap();
///
/// // The result is set here, but we'll never be able to reach it!
/// handle.complete(1);
/// }
/// ```
///
/// Dropping the [`CompleteHandle`]. Be careful as this is more subtle, and **MAY** cause a deadlock
/// depending on your compiler's implementation, as Rust is under no obligation to drop the
/// [`CompleteHandle`] before you await the future, as it's lifetime extends until the end of the
/// block:
/// ```rust
/// # use future_handles::sync;
/// async fn may_deadlock_before_drop() {
/// let (future, handle) = sync::create::<()>();
/// // The handle could be dropped immediately here and never deadlock.
///
/// // Start awaiting here...
/// future.await.unwrap();
///
/// // Or it could be dropped here, but we'll never reach it, deadlocking!
/// }
/// ```
///
/// Introducing an inverse dependency between the [`CompletableFuture`] and it's [`CompleteHandle`]
/// will always deadlock:
/// ```rust
/// # use future_handles::sync;
/// async fn may_deadlock_before_drop() {
/// let (future, handle) = sync::create::<bool>();
///
/// // Making the completion depend on the result of the computation itself will cause a deadlock.
/// if future.await.unwrap() {
/// handle.complete(false);
/// }
/// }
/// ```
///
/// [`CompletableFuture`]: CompletableFuture
/// [`CompleteHandle`]: CompleteHandle
/// [`scoped`]: super::scoped::scoped
/// A thread-safe handle to complete the associated [`CompletableFuture`].
/// It can be safely dropped without setting a completion value.
///
/// ```rust
/// # use future_handles::sync::CompleteHandle;
/// fn func(complete_handle: CompleteHandle<u32>) {
/// if let Some(res) = func_that_may_fail() {
/// // Set the result.
/// complete_handle.complete(res);
/// }
///
/// // Or just drop the handle.
/// }
/// # fn func_that_may_fail() -> Option<u32> {
/// # Some(1)
/// # }
/// ```
///
/// If cloned, the handles race to complete the future.
///
/// ```rust
/// # use futures;
/// # use future_handles::sync::CompleteHandle;
/// async fn func(complete_handle: CompleteHandle<u32>) {
/// let clone = complete_handle.clone();
///
/// let a = async { complete_handle.complete(1); };
/// let b = async { clone.complete(2); };
///
/// futures::join!(a, b); // The handles race to set the result.
/// }
/// ```
///
/// [`CompletableFuture`]: CompletableFuture
/// A thread-safe future that can only be completed by the associated [`CompleteHandle`].
///
/// Since the [`CompleteHandle`] can be dropped without setting a completion value,
/// `CompletableFuture` always wraps the return value in a [`HandleResult`].
/// ```rust
/// # use futures::Future;
/// # use future_handles::HandleResult;
/// # async fn func(completable_future: impl Future<Output = HandleResult<()>>) { ///
/// match completable_future.await {
/// Ok(res) => res,
/// Err(err) => panic!("Handle was dropped without setting a value")
/// }
/// # }
/// ```
///
/// [`CompleteHandle`]: CompleteHandle
/// [`HandleResult`]: crate::HandleResult