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
//!
//! This tiny crate contains helpers to turn a borrowed future into an owned one.
//!
//! # Motivation
//!
//! Take [`tokio::sync::Notify`] as an example. A common paradigm is to call [`Notify::notified`]
//! before a relevant threading update/check, then perform the update/check, and then wait on the
//! resulting [`Notified`] future. Doing this guarantees the `Notified` is watching for calls to
//! [`Notify::notify_waiters`] prior to the update/check. This paradigm would be useful when dealing
//! with thread spawning (i.e. calling `notified` and moving the resulting future into the thread),
//! but this isn't possible with `notified` as it borrows the `Notify`.
//!
//! ```compile_fail
//! use std::sync::Arc;
//! use tokio::sync::Notify;
//!
//! let notify = Arc::new(Notify::new());
//!
//! // Spawn a thread that waits to be notified
//! {
//! // Copy the Arc
//! let notify = notify.clone();
//!
//! // Start listening before we spawn
//! let notified = notify.notified();
//!
//! // Spawn the thread
//! tokio::spawn(async move {
//! // Wait for our listen to complete
//! notified.await; // <-- fails because we can't move `notified`
//! });
//! }
//!
//! // Notify the waiting threads
//! notify.notify_waiters();
//! ```
//!
//! At present, there's no easy way to do this kind of borrow-then-move. While there are many
//! crates available to help turn this problem into a self-borrowing one, those solutions require
//! `unsafe` code with complicated covariance implications. This crate is instead able to solve this
//! simple case with no `unsafe`, and more complex cases are solved with only 1-2 lines of `unsafe`
//! code with no covariance meddling. Here is the solution to the above problem:
//!
//!
//! ```
//! # #[tokio::main(flavor = "current_thread")]
//! # async fn main() {
//! use std::sync::Arc;
//! use tokio::sync::Notify;
//! use owned_future::make;
//!
//! // Make the constructor for our future
//! let get_notified = owned_future::get!(fn(n: &mut Arc<Notify>) -> () {
//! n.notified()
//! });
//!
//! let notify = Arc::new(Notify::new());
//!
//! // Spawn a thread that waits to be notified
//! {
//! // Copy the Arc
//! let notify = notify.clone();
//!
//! // Start listening before we spawn
//! let notified = make(notify, get_notified);
//!
//! // Spawn the thread
//! tokio::spawn(async move {
//! // wait for our listen to complete
//! notified.await;
//! });
//! }
//!
//! // notify the waiting threads
//! notify.notify_waiters();
//! # }
//! ```
//!
//! # Technical Details
//!
//! So how does this work exactly? Rust doesn't usually let you move a borrowed value, but there's
//! one exception. Pinned `async` blocks. Once a value has been moved into an `async` block, and the
//! the block has been transformed into a `Pin`ned `Future`, during the execution of the future, the
//! borrow can be executed, but the pointer anchoring the future can still be freely moved around.
//! All that may sound a little confusing, but essentially what this crate does is a prettied up
//! version of this:
//!
//! ```skip
//! // Copy the Arc
//! let notify = notify.clone();
//!
//! let mut wrapped_notified = Box::pin(async move {
//! let notified = notify.notified();
//!
//! // This prevents us from driving the future to completion on the first poll
//! force_pause().await;
//!
//! future.await
//! });
//!
//! // Drive the future up to just past our `force_pause`.
//! // This will start listening before we spawn
//! wrapped_notified.poll_once()
//!
//! // Spawn the thread
//! tokio::spawn(async move {
//! // wait for our listen to complete
//! wrapped_notified.await;
//! });
//!
//! // notify the waiting threads
//! notify.notify_waiters();
//! ```
//!
//! The more complex wrappers have a little bit more machinery to handle auxiliary values and
//! errors, and the `Async*` helpers do a little bit of pin-projection and poll handling, but
//! ultimately the core logic boils down to something like the above.
//!
//! [`tokio::sync::Notify`]: https://docs.rs/tokio/latest/tokio/sync/struct.Notify.html
//! [`Notify::notified`]: https://docs.rs/tokio/latest/tokio/sync/struct.Notify.html#method.notified
//! [`Notified`]: https://docs.rs/tokio/latest/tokio/sync/futures/struct.Notified.html
//! [`Notify::notify_waiters`]: https://docs.rs/tokio/latest/tokio/sync/struct.Notify.html#method.notify_waiters
extern crate alloc;
pub use *;
pub use *;