qcell/
doctest_lcell.rs

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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Run ./update-compiletest-from-doctest.pl in crate base directory
// after making any modification to compile_fail tests here.

//! This tests the `LCell` implementation.
//!
//! It should be impossible to copy a `&mut LCellOwner`:
//!
//! ```compile_fail
//!# use qcell::{LCell, LCellOwner};
//!# use std::rc::Rc;
//! LCellOwner::scope(|mut owner1| {
//!     let owner2 = owner1;
//!     let rc = Rc::new(owner1.cell(100u32)); // Compile fail
//! });
//! ```
//!
//! It should be impossible to clone an LCellOwner:
//!
//! ```compile_fail
//!# use qcell::{LCell, LCellOwner};
//!# use std::rc::Rc;
//! LCellOwner::scope(|mut owner1| {
//!     let owner2 = owner1.clone(); // Compile fail
//! });
//! ```
//!
//! Two different owners can't borrow each other's cells immutably:
//!
//! ```compile_fail
//!# use qcell::{LCell, LCellOwner};
//!# use std::rc::Rc;
//! LCellOwner::scope(|mut owner1| {
//!     LCellOwner::scope(|mut owner2| {
//!         let c1 = Rc::new(LCell::new(100u32));
//!         let c1ref1 = owner1.ro(&c1);
//!         let c1ref2 = owner2.ro(&c1);   // Compile error
//!         println!("{}", *c1ref2);
//!     });
//! });
//! ```
//!
//! Or mutably:
//!
//! ```compile_fail
//!# use qcell::{LCell, LCellOwner};
//!# use std::rc::Rc;
//! LCellOwner::scope(|mut owner1| {
//!     LCellOwner::scope(|mut owner2| {
//!         let c1 = Rc::new(owner1.cell(100u32));
//!         let c1mutref2 = owner2.rw(&c1);    // Compile error
//!         println!("{}", *c1mutref2);
//!     });
//! });
//! ```
//!
//! You can't have two separate mutable borrows active on the same
//! owner at the same time:
//!
//! ```compile_fail
//!# use qcell::{LCell, LCellOwner};
//!# use std::rc::Rc;
//! LCellOwner::scope(|mut owner| {
//!     let c1 = Rc::new(LCell::new(100u32));
//!     let c2 = Rc::new(LCell::new(200u32));
//!
//!     let c1mutref = owner.rw(&c1);
//!     let c2mutref = owner.rw(&c2);  // Compile error
//!     *c1mutref += 1;
//!     *c2mutref += 2;
//! });
//! ```
//!
//! However with `rw2()` you can do two mutable borrows at the
//! same time, since this call checks at runtime that the two
//! references don't refer to the same memory:
//!
//! ```
//!# use qcell::{LCell, LCellOwner};
//!# use std::rc::Rc;
//! LCellOwner::scope(|mut owner| {
//!     let c1 = Rc::new(LCell::new(100u32));
//!     let c2 = Rc::new(LCell::new(200u32));
//!     let (c1mutref, c2mutref) = owner.rw2(&c1, &c2);
//!     *c1mutref += 1;
//!     *c2mutref += 2;
//!     assert_eq!(303, owner.ro(&c1) + owner.ro(&c2));   // Success!
//! });
//! ```
//!
//! You can't have a mutable borrow at the same time as an immutable
//! borrow:
//!
//! ```compile_fail
//!# use qcell::{LCell, LCellOwner};
//!# use std::rc::Rc;
//! LCellOwner::scope(|mut owner| {
//!     let c1 = Rc::new(LCell::new(100u32));
//!     let c2 = Rc::new(LCell::new(200u32));
//!     let c1ref = owner.ro(&c1);
//!     let c1mutref = owner.rw(&c1);    // Compile error
//!     println!("{}", *c1ref);
//! });
//! ```
//!
//! Not even if it's borrowing a different object:
//!
//! ```compile_fail
//!# use qcell::{LCell, LCellOwner};
//!# use std::rc::Rc;
//! LCellOwner::scope(|mut owner| {
//!     let c1 = Rc::new(LCell::new(100u32));
//!     let c2 = Rc::new(LCell::new(200u32));
//!     let c1mutref = owner.rw(&c1);
//!     let c2ref = owner.ro(&c2);    // Compile error
//!     *c1mutref += 1;
//! });
//! ```
//!
//! Many immutable borrows at the same time is fine:
//!
//! ```
//!# use qcell::{LCell, LCellOwner};
//!# use std::rc::Rc;
//! LCellOwner::scope(|mut owner| {
//!     let c1 = Rc::new(LCell::new(100u32));
//!     let c2 = Rc::new(LCell::new(200u32));
//!     let c1ref = owner.ro(&c1);
//!     let c2ref = owner.ro(&c2);
//!     let c1ref2 = owner.ro(&c1);
//!     let c2ref2 = owner.ro(&c2);
//!     assert_eq!(600, *c1ref + *c2ref + *c1ref2 + *c2ref2);   // Success!
//! });
//! ```
//!
//! Whilst a reference is active, it's impossible to drop the `Rc`:
//!
//! ```compile_fail
//!# use qcell::{LCell, LCellOwner};
//!# use std::rc::Rc;
//! LCellOwner::scope(|mut owner| {
//!     let c1 = Rc::new(LCell::new(100u32));
//!     let c1ref = owner.ro(&c1);
//!     drop(c1);    // Compile error
//!     println!("{}", *c1ref);
//! });
//! ```
//!
//! Also, whilst a reference is active, it's impossible to call
//! anything else that uses the `owner` in an incompatible way,
//! e.g. `&mut` when there's a `&` reference:
//!
//! ```compile_fail
//!# use qcell::{LCell, LCellOwner};
//!# use std::rc::Rc;
//! LCellOwner::scope(|mut owner| {
//!     let c1 = Rc::new(LCell::new(100u32));
//!     fn test(o: &mut LCellOwner) {}
//!
//!     let c1ref = owner.ro(&c1);
//!     test(&mut owner);    // Compile error
//!     println!("{}", *c1ref);
//! });
//! ```
//!
//! Or `&` when there's a `&mut` reference:
//!
//! ```compile_fail
//!# use qcell::{LCell, LCellOwner};
//!# use std::rc::Rc;
//! LCellOwner::scope(|mut owner| {
//!     let c1 = Rc::new(LCell::new(100u32));
//!     fn test(o: &LCellOwner) {}
//!
//!     let c1mutref = owner.rw(&c1);
//!     test(&owner);    // Compile error
//!     *c1mutref += 1;
//! });
//! ```
//!
//! Two examples of passing owners and cells in function arguments.
//! This needs lifetime annotations.
//!
//! ```
//! use qcell::{LCell, LCellOwner};
//! use std::rc::Rc;
//! LCellOwner::scope(|mut owner| {
//!     let c1 = Rc::new(LCell::new(100u32));
//!     fn test<'id>(o: &mut LCellOwner<'id>, rc: &Rc<LCell<'id, u32>>) {
//!        *o.rw(rc) += 1;
//!     }
//!
//!     test(&mut owner, &c1);
//!     let c1mutref = owner.rw(&c1);
//!     *c1mutref += 1;
//! });
//! ```
//!
//! ```
//! use qcell::{LCell, LCellOwner};
//! use std::rc::Rc;
//! LCellOwner::scope(|mut owner| {
//!     struct Context<'id> { owner: LCellOwner<'id>, }
//!     let c1 = Rc::new(LCell::new(100u32));
//!     let mut ct = Context { owner };
//!     fn test<'id>(ct: &mut Context<'id>, rc: &Rc<LCell<'id, u32>>) {
//!        *ct.owner.rw(rc) += 1;
//!     }
//!
//!     test(&mut ct, &c1);
//!     let c1mutref = ct.owner.rw(&c1);
//!     *c1mutref += 2;
//! });
//! ```
//!
//! `LCellOwner` and `LCell` should be both `Send` and `Sync` by default:
//!
//! ```
//!# use qcell::{LCellOwner, LCell};
//! fn is_send_sync<T: Send + Sync>() {}
//! is_send_sync::<LCellOwner<'_>>();
//! is_send_sync::<LCell<'_, ()>>();
//! ```
//!
//! So for example we can share a cell ref between threads (Sync), and
//! pass an owner back and forth (Send):
//!
//! ```
//!# use qcell::{LCellOwner, LCell};
//!
//! LCellOwner::scope(|mut owner| {
//!     let cell = LCell::new(100);
//!
//!     *owner.rw(&cell) += 1;
//!     let cell_ref = &cell;
//!     let mut owner = crossbeam::scope(move |s| {
//!         s.spawn(move |_| {
//!             *owner.rw(cell_ref) += 2;
//!             owner
//!         }).join().unwrap()
//!     }).unwrap();
//!     *owner.rw(&cell) += 4;
//!     assert_eq!(*owner.ro(&cell), 107);
//! });
//! ```
//!
//! However you can't send a cell that's still borrowed:
//!
//! ```compile_fail
//!# use qcell::{LCellOwner, LCell};
//! LCellOwner::scope(|mut owner| {
//!     let cell = LCell::new(100);
//!     let val_ref = owner.ro(&cell);
//!     crossbeam::scope(move |s| {
//!         s.spawn(move |_| assert_eq!(*owner.ro(&cell), 100)).join().unwrap(); // Compile fail
//!     }).unwrap();
//!     assert_eq!(*val_ref, 100);
//! });
//! ```
//!
//! If the contained type isn't `Sync`, though, then `LCell` shouldn't
//! be `Sync` either:
//!
//! ```compile_fail
//!# use qcell::LCell;
//!# use std::cell::Cell;
//! fn is_sync<T: Sync>() {}
//! is_sync::<LCell<'_, Cell<i32>>>();  // Compile fail
//! ```
//!
//! ```compile_fail
//!# use qcell::{LCell, LCellOwner};
//!# use std::cell::Cell;
//! LCellOwner::scope(|owner| {
//!     let cell = LCell::new(Cell::new(100));
//!
//!     // This would likely be a data race if it compiled
//!     crossbeam::scope(|s| {   // Compile fail
//!         let handle = s.spawn(|_| owner.ro(&cell).set(200));
//!         owner.ro(&cell).set(300);
//!         handle.join().unwrap();
//!     }).unwrap();
//! });
//! ```
//!
//! If the contained type isn't `Send`, the `LCell` should be neither
//! `Sync` nor `Send`:
//!
//! ```compile_fail
//!# use qcell::LCell;
//!# use std::rc::Rc;
//!# struct Marker;
//! fn is_sync<T: Sync>() {}
//! is_sync::<LCell<'_, Rc<()>>>();  // Compile fail
//! ```
//!
//! ```compile_fail
//!# use qcell::LCell;
//!# use std::rc::Rc;
//!# struct Marker;
//! fn is_send<T: Send>() {}
//! is_send::<LCell<'_, Rc<()>>>();  // Compile fail
//! ```
//!
//! ```compile_fail
//!# use qcell::{LCell, LCellOwner};
//!# use std::rc::Rc;
//! LCellOwner::scope(|owner| {
//!     let cell = LCell::new(Rc::new(100));
//!
//!     // We aren't permitted to move the Rc to another thread
//!     crossbeam::scope(move |s| {
//!         s.spawn(move |_| assert_eq!(100, **owner.ro(&cell))).join().unwrap(); // Compile fail
//!     }).unwrap();
//! });
//! ```
//!
//! A reference obtained using `get_mut` should exclude any other kind
//! of borrowing.
//!
//! ```compile_fail
//!# use qcell::{LCell, LCellOwner};
//! LCellOwner::scope(|owner| {
//!     let mut cell = LCell::new(100);
//!     let cell_ref = cell.get_mut();
//!     assert_eq!(100, *owner.ro(&cell)); // Compile fail
//!     *cell_ref = 50;
//! });
//! ```
//!
//! ```compile_fail
//!# use qcell::{LCell, LCellOwner};
//! LCellOwner::scope(|mut owner| {
//!     let mut cell = LCell::new(100);
//!     let cell_ref = cell.get_mut();
//!     assert_eq!(100, *owner.rw(&cell)); // Compile fail
//!     *cell_ref = 50;
//! });
//! ```
//!
//! ```compile_fail
//!# use qcell::{LCell, LCellOwner};
//! LCellOwner::scope(|owner| {
//!     let mut cell = LCell::new(100);
//!     let cell_ref = owner.ro(&cell);
//!     *cell.get_mut() = 50; // Compile fail
//!     assert_eq!(100, *cell_ref);
//! });
//! ```
//!
//! ```compile_fail
//!# use qcell::{LCell, LCellOwner};
//! LCellOwner::scope(|mut owner| {
//!     let mut cell = LCell::new(100);
//!     let cell_ref = owner.rw(&cell);
//!     *cell.get_mut() = 50; // Compile fail
//!     assert_eq!(100, *cell_ref);
//! });
//! ```
//!
//! `Default` is implemented, but only if the enclosed type has a
//! default:
//!
//! ```
//!# use qcell::{LCell, LCellOwner};
//! LCellOwner::scope(|mut owner| {
//!     let mut cell: LCell<i32> = LCell::default();
//!     assert_eq!(0, *owner.ro(&cell));
//! });
//! ```
//!
//! ```compile_fail
//!# use qcell::{LCell, LCellOwner};
//! LCellOwner::scope(|mut owner| {
//!     struct NoDefault(i32);
//!     let mut cell: LCell<NoDefault> = LCell::default(); // Compile fail
//!     assert_eq!(0, owner.ro(&cell).0);
//! });
//! ```