borrowscope-runtime 0.1.2

Runtime tracking system for BorrowScope
Documentation
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
//! Smart pointer tracking: Rc, Arc, Box, Weak, Pin, Cow

use super::TRACKER;
use std::borrow::ToOwned;

pub fn track_rc_new_with_id<T: ?Sized>(
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] id: usize,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] name: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] type_name: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] location: &str,
    value: std::rc::Rc<T>,
) -> std::rc::Rc<T> {
    #[cfg(feature = "track")]
    {
        let strong_count = std::rc::Rc::strong_count(&value);
        let weak_count = std::rc::Rc::weak_count(&value);
        let mut tracker = TRACKER.lock();
        tracker.record_rc_new_with_id(id, name, type_name, location, strong_count, weak_count);
    }
    value
}

/// Track Rc::clone with explicit IDs and location (advanced API)
#[inline(always)]
pub fn track_rc_clone_with_id<T: ?Sized>(
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] new_id: usize,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] source_id: usize,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] name: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] location: &str,
    value: std::rc::Rc<T>,
) -> std::rc::Rc<T> {
    #[cfg(feature = "track")]
    {
        let strong_count = std::rc::Rc::strong_count(&value);
        let weak_count = std::rc::Rc::weak_count(&value);
        let mut tracker = TRACKER.lock();
        tracker.record_rc_clone_with_id(
            new_id,
            source_id,
            name,
            location,
            strong_count,
            weak_count,
        );
    }
    value
}

/// Track Arc::new with explicit ID and location (advanced API)
#[inline(always)]
pub fn track_arc_new_with_id<T: ?Sized>(
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] id: usize,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] name: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] type_name: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] location: &str,
    value: std::sync::Arc<T>,
) -> std::sync::Arc<T> {
    #[cfg(feature = "track")]
    {
        let strong_count = std::sync::Arc::strong_count(&value);
        let weak_count = std::sync::Arc::weak_count(&value);
        let mut tracker = TRACKER.lock();
        tracker.record_arc_new_with_id(id, name, type_name, location, strong_count, weak_count);
    }
    value
}

/// Track Arc::clone with explicit IDs and location (advanced API)
#[inline(always)]
pub fn track_arc_clone_with_id<T: ?Sized>(
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] new_id: usize,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] source_id: usize,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] name: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] location: &str,
    value: std::sync::Arc<T>,
) -> std::sync::Arc<T> {
    #[cfg(feature = "track")]
    {
        let strong_count = std::sync::Arc::strong_count(&value);
        let weak_count = std::sync::Arc::weak_count(&value);
        let mut tracker = TRACKER.lock();
        tracker.record_arc_clone_with_id(
            new_id,
            source_id,
            name,
            location,
            strong_count,
            weak_count,
        );
    }
    value
}

/// Track `Rc::new` allocation.
///
/// Records an `RcNew` event with the current strong and weak reference counts.
/// Use this when creating a new reference-counted pointer.
///
/// # Arguments
///
/// * `name` - A descriptive name for the Rc
/// * `value` - The Rc being tracked (returned unchanged)
///
/// # Returns
///
/// The input `Rc`, unchanged.
///
/// # Examples
///
/// ```rust
/// # use borrowscope_runtime::*;
/// use std::rc::Rc;
/// # reset();
///
/// let shared = track_rc_new("shared", Rc::new(vec![1, 2, 3]));
/// assert_eq!(Rc::strong_count(&shared), 1);
///
/// let events = get_events();
/// assert!(events[0].is_rc());
/// assert_eq!(events[0].strong_count(), Some(1));
/// ```
#[inline(always)]
pub fn track_rc_new<T: ?Sized>(
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] name: &str,
    value: std::rc::Rc<T>,
) -> std::rc::Rc<T> {
    #[cfg(feature = "track")]
    {
        let strong_count = std::rc::Rc::strong_count(&value);
        let weak_count = std::rc::Rc::weak_count(&value);
        let mut tracker = TRACKER.lock();
        tracker.record_rc_new(name, strong_count, weak_count);
    }
    value
}

/// Track `Rc::clone` operation.
///
/// Records an `RcClone` event with the updated reference counts.
/// Use this when cloning an Rc to share ownership.
///
/// # Arguments
///
/// * `name` - A descriptive name for the new clone
/// * `source_name` - Name of the Rc being cloned from
/// * `value` - The cloned Rc (returned unchanged)
///
/// # Returns
///
/// The input `Rc`, unchanged.
///
/// # Examples
///
/// ```rust
/// # use borrowscope_runtime::*;
/// use std::rc::Rc;
/// # reset();
///
/// let original = track_rc_new("original", Rc::new(42));
/// let clone1 = track_rc_clone("clone1", "original", Rc::clone(&original));
/// let clone2 = track_rc_clone("clone2", "original", Rc::clone(&original));
///
/// assert_eq!(Rc::strong_count(&original), 3);
///
/// let events = get_events();
/// assert_eq!(events[1].strong_count(), Some(2)); // After first clone
/// assert_eq!(events[2].strong_count(), Some(3)); // After second clone
/// ```
#[inline(always)]
pub fn track_rc_clone<T: ?Sized>(
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] name: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] source_name: &str,
    value: std::rc::Rc<T>,
) -> std::rc::Rc<T> {
    #[cfg(feature = "track")]
    {
        let strong_count = std::rc::Rc::strong_count(&value);
        let weak_count = std::rc::Rc::weak_count(&value);
        let mut tracker = TRACKER.lock();
        tracker.record_rc_clone(name, source_name, strong_count, weak_count);
    }
    value
}

/// Track `Arc::new` allocation.
///
/// Records an `ArcNew` event with the current strong and weak reference counts.
/// Use this when creating a new thread-safe reference-counted pointer.
///
/// # Arguments
///
/// * `name` - A descriptive name for the Arc
/// * `value` - The Arc being tracked (returned unchanged)
///
/// # Returns
///
/// The input `Arc`, unchanged.
///
/// # Examples
///
/// ```rust
/// # use borrowscope_runtime::*;
/// use std::sync::Arc;
/// # reset();
///
/// let shared = track_arc_new("shared", Arc::new(vec![1, 2, 3]));
/// assert_eq!(Arc::strong_count(&shared), 1);
///
/// let events = get_events();
/// assert!(events[0].is_arc());
/// ```
#[inline(always)]
pub fn track_arc_new<T: ?Sized>(
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] name: &str,
    value: std::sync::Arc<T>,
) -> std::sync::Arc<T> {
    #[cfg(feature = "track")]
    {
        let strong_count = std::sync::Arc::strong_count(&value);
        let weak_count = std::sync::Arc::weak_count(&value);
        let mut tracker = TRACKER.lock();
        tracker.record_arc_new(name, strong_count, weak_count);
    }
    value
}

/// Track `Arc::clone` operation.
///
/// Records an `ArcClone` event with the updated reference counts.
/// Use this when cloning an Arc for thread-safe shared ownership.
///
/// # Arguments
///
/// * `name` - A descriptive name for the new clone
/// * `source_name` - Name of the Arc being cloned from
/// * `value` - The cloned Arc (returned unchanged)
///
/// # Returns
///
/// The input `Arc`, unchanged.
///
/// # Examples
///
/// ```rust
/// # use borrowscope_runtime::*;
/// use std::sync::Arc;
/// use std::thread;
/// # reset();
///
/// let data = track_arc_new("data", Arc::new(42));
/// let data_clone = track_arc_clone("thread_copy", "data", Arc::clone(&data));
///
/// let handle = thread::spawn(move || {
///     println!("Value: {}", *data_clone);
/// });
/// handle.join().unwrap();
/// ```
#[inline(always)]
pub fn track_arc_clone<T: ?Sized>(
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] name: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] source_name: &str,
    value: std::sync::Arc<T>,
) -> std::sync::Arc<T> {
    #[cfg(feature = "track")]
    {
        let strong_count = std::sync::Arc::strong_count(&value);
        let weak_count = std::sync::Arc::weak_count(&value);
        let mut tracker = TRACKER.lock();
        tracker.record_arc_clone(name, source_name, strong_count, weak_count);
    }
    value
}

/// Track `RefCell::new` allocation.
///
/// Records a `RefCellNew` event. Use this when creating a new RefCell
/// for interior mutability.
///
/// # Arguments
///
/// * `name` - A descriptive name for the RefCell
/// * `value` - The RefCell being tracked (returned unchanged)
///
/// # Returns
///
/// The input `RefCell`, unchanged.
///
/// # Examples
///
/// ```rust
/// # use borrowscope_runtime::*;
/// use std::cell::RefCell;
/// # reset();
///
/// let cell = track_refcell_new("cell", RefCell::new(42));
///
/// let events = get_events();
/// assert!(events[0].is_refcell());
/// ```
#[inline(always)]
pub fn track_weak_new<T: ?Sized>(
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] name: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] source_name: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] location: &str,
    value: std::rc::Weak<T>,
) -> std::rc::Weak<T> {
    #[cfg(feature = "track")]
    {
        let weak_count = std::rc::Weak::weak_count(&value);
        let mut tracker = TRACKER.lock();
        tracker.record_weak_new(name, source_name, weak_count, location);
    }
    value
}

/// Track sync Weak::new or Arc::downgrade
#[inline(always)]
pub fn track_weak_new_sync<T: ?Sized>(
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] name: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] source_name: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] location: &str,
    value: std::sync::Weak<T>,
) -> std::sync::Weak<T> {
    #[cfg(feature = "track")]
    {
        let weak_count = std::sync::Weak::weak_count(&value);
        let mut tracker = TRACKER.lock();
        tracker.record_weak_new(name, source_name, weak_count, location);
    }
    value
}

/// Track Weak::clone
#[inline(always)]
pub fn track_weak_clone<T: ?Sized>(
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] name: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] source_name: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] location: &str,
    value: std::rc::Weak<T>,
) -> std::rc::Weak<T> {
    #[cfg(feature = "track")]
    {
        let weak_count = std::rc::Weak::weak_count(&value);
        let mut tracker = TRACKER.lock();
        tracker.record_weak_clone(name, source_name, weak_count, location);
    }
    value
}

/// Track sync Weak::clone
#[inline(always)]
pub fn track_weak_clone_sync<T: ?Sized>(
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] name: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] source_name: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] location: &str,
    value: std::sync::Weak<T>,
) -> std::sync::Weak<T> {
    #[cfg(feature = "track")]
    {
        let weak_count = std::sync::Weak::weak_count(&value);
        let mut tracker = TRACKER.lock();
        tracker.record_weak_clone(name, source_name, weak_count, location);
    }
    value
}

/// Track Weak::upgrade
#[inline(always)]
pub fn track_weak_upgrade<T>(
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] weak_id: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] location: &str,
    value: Option<std::rc::Rc<T>>,
) -> Option<std::rc::Rc<T>> {
    #[cfg(feature = "track")]
    {
        let mut tracker = TRACKER.lock();
        tracker.record_weak_upgrade(weak_id, value.is_some(), location);
    }
    value
}

/// Track sync Weak::upgrade
#[inline(always)]
pub fn track_weak_upgrade_sync<T>(
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] weak_id: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] location: &str,
    value: Option<std::sync::Arc<T>>,
) -> Option<std::sync::Arc<T>> {
    #[cfg(feature = "track")]
    {
        let mut tracker = TRACKER.lock();
        tracker.record_weak_upgrade(weak_id, value.is_some(), location);
    }
    value
}

/// Track Box::new
#[inline(always)]
pub fn track_box_new<T: ?Sized>(
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] name: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] location: &str,
    value: Box<T>,
) -> Box<T> {
    #[cfg(feature = "track")]
    {
        let type_name = std::any::type_name::<T>();
        let mut tracker = TRACKER.lock();
        tracker.record_box_new(name, type_name, location);
    }
    value
}

/// Track Box::into_raw
#[inline(always)]
pub fn track_box_into_raw<T>(
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] box_id: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] location: &str,
    ptr: *mut T,
) -> *mut T {
    #[cfg(feature = "track")]
    {
        let mut tracker = TRACKER.lock();
        tracker.record_box_into_raw(box_id, location);
    }
    ptr
}

/// Track Box::from_raw
#[inline(always)]
pub fn track_box_from_raw<T>(
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] name: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] location: &str,
    value: Box<T>,
) -> Box<T> {
    #[cfg(feature = "track")]
    {
        let mut tracker = TRACKER.lock();
        tracker.record_box_from_raw(name, location);
    }
    value
}

/// Track lock guard acquisition
#[inline(always)]
pub fn track_lock_guard_acquire(
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] guard_id: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] lock_id: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] lock_type: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] location: &str,
) {
    #[cfg(feature = "track")]
    {
        let mut tracker = TRACKER.lock();
        tracker.record_lock_guard_acquire(guard_id, lock_id, lock_type, location);
    }
}

/// Track lock guard drop
#[inline(always)]
pub fn track_lock_guard_drop(
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] guard_id: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] location: &str,
) {
    #[cfg(feature = "track")]
    {
        let mut tracker = TRACKER.lock();
        tracker.record_lock_guard_drop(guard_id, location);
    }
}

/// Track Pin::new
#[inline(always)]
pub fn track_pin_new<P>(
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] name: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] location: &str,
    value: std::pin::Pin<P>,
) -> std::pin::Pin<P> {
    #[cfg(feature = "track")]
    {
        let mut tracker = TRACKER.lock();
        tracker.record_pin_new(name, location);
    }
    value
}

/// Track Pin::into_inner
#[inline(always)]
pub fn track_pin_into_inner<T>(
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] pin_id: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] location: &str,
    value: T,
) -> T {
    #[cfg(feature = "track")]
    {
        let mut tracker = TRACKER.lock();
        tracker.record_pin_into_inner(pin_id, location);
    }
    value
}

/// Track Cow::Borrowed
#[inline(always)]
pub fn track_cow_borrowed<'a, B: ?Sized + ToOwned>(
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] name: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] location: &str,
    value: std::borrow::Cow<'a, B>,
) -> std::borrow::Cow<'a, B> {
    #[cfg(feature = "track")]
    {
        let mut tracker = TRACKER.lock();
        tracker.record_cow_borrowed(name, location);
    }
    value
}

/// Track Cow::Owned
#[inline(always)]
pub fn track_cow_owned<'a, B: ?Sized + ToOwned>(
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] name: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] location: &str,
    value: std::borrow::Cow<'a, B>,
) -> std::borrow::Cow<'a, B> {
    #[cfg(feature = "track")]
    {
        let mut tracker = TRACKER.lock();
        tracker.record_cow_owned(name, location);
    }
    value
}

/// Track Cow::to_mut (clone-on-write)
#[inline(always)]
pub fn track_cow_to_mut(
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] cow_id: &str,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] cloned: bool,
    #[cfg_attr(not(feature = "track"), allow(unused_variables))] location: &str,
) {
    #[cfg(feature = "track")]
    {
        let mut tracker = TRACKER.lock();
        tracker.record_cow_to_mut(cow_id, cloned, location);
    }
}