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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//! This module is somewhat similar to scoped threads, but allows passing references to other
//! threads over channels, or similar mechanisms.
//!
//! It captures zero or more references inside of "capsules" with a specific lifetime, then runs a function in a context
//! where control won't be returned to the caller until all capsules for that lifetime have been
//! dropped.
//!
//! Since it blocks waiting for the "capsules" to be dropped, the capsules can be safely passed to
//! other threads (for example with a channel), wher they can be derferenced as references again.
//!
//! # Examples
//!
//! ```
//! use std::thread;
//! use std::time::Duration;
//! use std::sync::mpsc::channel;
//! use refcapsule::{Capsule, with_encapsulated};
//!
//! let (sender, receiver) = channel::<Capsule<u32>>();
//!
//! // receiver of references
//!
//! thread::spawn(move || {
//! {
//! let r = receiver.recv().unwrap();
//! thread::sleep(Duration::from_millis(100));
//! assert_eq!(*r, 4);
//! }
//! {
//! let r = receiver.recv().unwrap();
//! thread::sleep(Duration::from_millis(100));
//! assert_eq!(*r, 12);
//! }
//! });
//!
//! let x: u32 = 4;
//! let s1 = sender.clone();
//! with_encapsulated(&x, move |x| s1.send(x).unwrap());
//!
//! with_encapsulated(&12, move |cap| sender.send(cap).unwrap());
//! ```
//!
//!
//! ## Things that shouldn't compile
//!
//! Mutating the original variable, while it is encapsulated:
//!
//! ```compile_fail,E0499
//! use refcapsule::with_encapsulated;
//!
//! let mut x = 43;
//! with_encapsulated(&mut x, |y| {
//! x = 4;
//! });
//! ```
//!
//! Encapsulating a reference with a lifetime shorter than the encapsulation scope:
//!
//! ```compile_fail,E0597
//! use refcapsule::{with_encapsulated, encapsulate::gen};
//!
//! with_encapsulated(gen(|s| {
//! let x = 43;
//! s.encapsulate(&x);
//! }), |x| {});
//! ```
//!
//! Mutating the original variable when it is encapsulated using a generator function:
//!
//! ```compile_fail,E0499
//! use refcapsule::{with_encapsulated, encapsulate::gen};
//!
//! let mut x = 43;
//! with_encapsulated(gen(|s| {
//! s.encapsulate_mut(&mut x);
//! }), |y| {
//! x = 4;
//! });
//! ```
//!
//! Save a scope for a longer duration:
//!
//! ```compile_fail
//! use refcapsule::{with_encapsulated, encapsulate::gen};
//! with_encapsulated(gen(|s| s), |s| ());
//! ```
use ;
use PhantomData;
use ;
use ;
use NonNull;
use ;
use Arc;
use ;
use *;
// NOTE:
// We need to store the state in an arc to prevent a race condition
// if the original thread returns before all threads have finished decrementing.
// For example, in the case of a spurious wakeup between the last decrement of the counter
// and the call to unpark.
;
/// An encapsulated `&T` that can be passed between threads.
///
/// Since the thread that created it will block until this is dropped, it doesn't have
/// a lifetime. However, note that if it isn't dropped (for example if it is passed to
/// [`std::mem::forget`]) then it can create a deadlock, as the original thread is waiting
/// for something that will never happen.
// Safety:
// This is safe because the thread that created the capsule will wait for
// it to be dropped before ending the original lifetime
unsafe
/// If this is dropped while panicking, then the original thread will also panic.
/// Type representing a scope for encapsulated references
///
/// Most user code shouldn't have to directly use this. It is primarily for implementing
/// [`Encapsulate`]. It can convert a `&T` or `&mut T` into a `Capsule<T>` or `CapsuleMut<T>`
/// respectively. A typical implementation of [`Encapsulate`] will project embedded reference
/// types from the source type to capsule types in the destination types, using the methods on
/// `Scope`.
///
/// Create a scope for sending references to another thread.
///
/// Encapsulate one or more references from `data` inside of `Capsule` or `CapsuleMut` which can
/// be safely passed to another thread (for example, through a channel). The `f` function is then
/// called with the capsule(s), and then this function will wait until all created capsules have
/// been dropped, even if they are eventually dropped by another thread.
///
/// Note that this function will block unless the capsules are dropped by `f` on the same thread.
///
/// `data` is any type that implements the [`Encapsulate`] trait. The most significant
/// implementations of this trait includes:
///
/// - `&T` which is convertend into a `Capsule<T>`
/// - `&mut T` which is converted into a `CapsuleMut<T>`
/// - A tuple of up to 8 elements, each of which implements `Encapsulate`, which is converted into
/// a tuple where each item is converted into the respective `Encapsulated` type.
/// - `[T; N]` where T implements `Encapsulate`.
/// - [`encapsulate::Gen`] which will allow you to directly use the [`Scope`] to convert
/// references to capsules inside a function or (more likely) closure.