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
379
380
381
382
383
384
385
386
387
//! Object handles — pyrucast's "object pile", cast3m-style.
//!
//! Every object the user manipulates lives behind a [`Handle<T>`]: a counted,
//! cheaply cloned reference carrying its own lock. There is no session to pass
//! around and no global registry — a handle *is* the object's address, and the
//! object dies with the last handle that names it.
//!
//! ```text
//! Handle<T> = Arc<RwLock<T>> + the API we want on it
//! ```
//!
//! # Guarantees
//!
//! - **Counted.** `Clone` shares, `Drop` releases; when the last handle goes,
//! the value is dropped — running its own `Drop`, so side effects such as
//! `SubMesh` releasing its nodes inside the `Coords` happen exactly once.
//! - **Always valid.** A handle cannot outlive its object: holding one keeps
//! the object alive. There is no stale handle, no generation to check, and
//! [`Handle::read`] / [`Handle::write`] cannot fail.
//! - **Identity is the pointer.** [`Handle::same_object`] answers « are these
//! two references the same object? » — the basis of the aggregates' union.
//!
//! # Why not a slab of numbered slots
//!
//! There used to be one: a registry per [`std::any::TypeId`], a slab of
//! generational slots, a hand-written refcount, and a disk-swap path. Every
//! piece of it duplicated something `Arc` already does — the hand refcount
//! tracked what the `Arc`'s tracked, and the generation only protected handles
//! rebuilt from bytes, which the swap alone produced.
//!
//! What the registry offered beyond that was **enumeration** (« list every live
//! mesh ») and a **stable number** per object. Neither had ever reached a user.
//! Should enumeration become wanted — a cast3m-style listing, a whole-session
//! save — it costs a `Vec<Weak<_>>` registered in [`Handle::new`], which is why
//! that constructor is the single funnel through which objects are created.
//!
//! # Concurrency
//!
//! Locking is **per object**: each handle carries its own
//! [`parking_lot::RwLock`]. [`Handle::read`] returns a shared guard (many
//! concurrent readers), [`Handle::write`] an exclusive one. Guards are *owned*
//! (`'static`): they can be returned from functions and stored in structs, so
//! operators read the data **in place** instead of copying it out.
//!
//! One usage rule applies: **do not acquire a second guard on an object while
//! holding a write guard on that same object** (and do not `write` an object
//! while holding any guard on it) — the lock is not reentrant. Distinct
//! objects are fully independent.
//!
//! # Example
//!
//! ```
//! use pyrucast::handle::Handle;
//!
//! #[derive(Debug, PartialEq)]
//! struct Points(Vec<f64>);
//!
//! let h = Handle::new(Points(vec![1.0, 2.0]));
//! h.write().0.push(3.0);
//! assert_eq!(h.read().0.len(), 3);
//!
//! // A clone names the same object.
//! let g = h.clone();
//! assert!(g.same_object(&h));
//! ```
use ;
use ;
use fmt;
use Arc;
/// Shared (read) access to an object, returned by [`Handle::read`].
///
/// Owned (`'static`): it can be returned from a function and stored in a
/// struct. Dereferences to `&T`; the lock is released when the guard goes out
/// of scope. The guard holds the object itself alive, so it may outlive every
/// handle it came from.
///
/// ```
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # let handle = Handle::new(Coords::new(2).unwrap());
/// // Le verrou porte sur **cet objet seul**, et dure le temps du guard :
/// // plusieurs lectures peuvent coexister.
/// let a = handle.read();
/// let b = handle.read();
/// assert_eq!(a.dim(), b.dim());
/// ```
pub type ReadGuard<T> = ;
/// Exclusive (write) access to an object, returned by [`Handle::write`].
///
/// Same ownership properties as [`ReadGuard`]; dereferences to `&mut T`.
///
/// ```
/// # use pyrucast::coords::Coords;
/// # use pyrucast::handle::Handle;
/// # let handle = Handle::new(Coords::new(2).unwrap());
/// // L'écriture est exclusive, le temps de l'appel. Le relâcher avant de
/// // reprendre en lecture est ce qui évite l'interblocage.
/// handle.write().add_node(&[0.0, 0.0])?;
/// assert_eq!(handle.read().node_count(), 1);
/// # Ok::<(), pyrucast::PyrucastError>(())
/// ```
pub type WriteGuard<T> = ;
/// A counted reference to an object, with its own lock.
///
/// Cloning shares the object rather than copying it — the whole point, since a
/// `SubMesh` or a `Coords` is large and shared by construction. Two handles
/// name the same object exactly when [`same_object`](Self::same_object) says
/// so.
// ANCHOR: declaration
///
/// ```
/// # use pyrucast::handle::Handle;
/// # use pyrucast::coords::Coords;
/// let h1 = Handle::new(Coords::new(2).unwrap()); // premier ticket
/// let h2 = h1.clone(); // même objet
/// assert!(h1.same_object(&h2));
/// drop(h1); // h2 tient encore l'objet
/// assert_eq!(h2.read().dim(), 2);
/// ```
// ANCHOR_END: declaration
/// Short, lock-free view: `<SubMesh #7f3a2c>`.
///
/// Deliberately does **not** read the object. Printing a handle must stay free
/// — a `SubMesh` holds a connectivity of millions of entries, and a handle may
/// well be formatted while a write guard is held on it, which reading would
/// deadlock.
/// Same short view as [`fmt::Display`] — `<SubMesh #7f3a2c>`.
///
/// The two agree on purpose. `Handle<pyrucast::containers::mesh::SubMesh> { #7f3a2c }`
/// spelled the module path out at every occurrence, which a `Debug` of a
/// container repeats once per field and once per zone, while the type is
/// already given by the field's own name.