cljrs-value 0.2.33

Runtime Value type and persistent collections for clojurust
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
#![allow(clippy::arc_with_non_send_sync)]
pub mod clone;
pub mod collections;
pub mod error;
pub mod hash;
pub mod intern;
pub mod jit_hooks;
pub mod keyword;
pub mod native_object;
pub mod parking;
pub mod publish;
pub mod realization;
pub mod regex;
pub mod resource;
pub mod shared;
pub mod symbol;
pub mod type_hint;
pub mod types;
pub mod value;

pub use collections::{
    PersistentArrayMap, PersistentHashMap, PersistentHashSet, PersistentList, PersistentQueue,
    PersistentVector, SortedMap, SortedSet,
};
pub use error::{ExceptionInfo, ValueError, ValueResult};
pub use hash::ClojureHash;
pub use intern::{intern_keyword, intern_symbol};
pub use jit_hooks::set_var_rebind_hook;
pub use keyword::Keyword;
pub use native_object::{NativeObject, NativeObjectBox, gc_native_object};
pub use resource::{Resource, ResourceHandle};
pub use shared::{ByteBlob, PromoteError, SharedAtom, SharedSlot, SharedValue, demote, promote};
pub use symbol::Symbol;
pub use type_hint::TypeHint;
pub use types::{
    Agent, Arity, Atom, BoundFn, CljxCons, CljxFn, CljxFnArity, CljxFuture, CljxPromise, Delay,
    DelayState, FUTURE_CANCELLED_MSG, FutureState, LazySeq, MultiFn, Namespace, NativeFn,
    NativeFnFunc, NativeFnPtr, Protocol, ProtocolFn, ProtocolMethod, ReferClojureFilter, Thunk,
    Var, Volatile, bump_protocol_generation, protocol_generation,
};
pub use value::{MapValue, ObjectArray, SetValue, TypeInstance, Value};

#[cfg(all(test, feature = "no-gc"))]
mod publication_review_tests {
    use super::*;
    use cljrs_gc::GcPtr;
    struct Probe(std::rc::Rc<std::cell::Cell<bool>>);
    impl cljrs_gc::Trace for Probe {
        fn trace(&self, _: &mut cljrs_gc::MarkVisitor) {}
    }
    impl Drop for Probe {
        fn drop(&mut self) {
            self.0.set(true);
        }
    }
    #[derive(Debug)]
    struct Counted {
        traces: std::rc::Rc<std::cell::Cell<usize>>,
        dropped: std::rc::Rc<std::cell::Cell<bool>>,
    }
    impl cljrs_gc::Trace for Counted {
        fn trace(&self, _: &mut cljrs_gc::MarkVisitor) {
            self.traces.set(self.traces.get() + 1);
        }
    }
    impl NativeObject for Counted {
        fn type_tag(&self) -> &str {
            "Counted"
        }
        fn as_any(&self) -> &dyn std::any::Any {
            self
        }
    }
    impl Drop for Counted {
        fn drop(&mut self) {
            self.dropped.set(true);
        }
    }
    #[test]
    fn cached_cell_snapshots_do_not_retrace_or_permanently_pin_payloads() {
        let atom = GcPtr::new(Atom::new(Value::Nil));
        let traces = std::rc::Rc::new(std::cell::Cell::new(0));
        let dropped = std::rc::Rc::new(std::cell::Cell::new(false));
        {
            let _source = cljrs_gc::alloc_ctx::ScratchGuard::new();
            let counted = Value::NativeObject(gc_native_object(Counted {
                traces: traces.clone(),
                dropped: dropped.clone(),
            }));
            let map = Value::Map(MapValue::from_pairs(
                (0..1000)
                    .map(|i| (Value::Long(i), counted.clone()))
                    .collect(),
            ));
            atom.get().reset(map);
        }
        let initial = traces.get();
        assert_eq!(initial, 1);
        for _ in 0..1000 {
            let scope = cljrs_gc::region::ReadScope::enter().unwrap();
            let snapshot = atom.get().snapshot();
            assert!(matches!(snapshot.value, Value::Map(_)));
            atom.get().deref();
            drop(scope);
        }
        assert_eq!(traces.get(), initial, "reads walked the full map graph");
        atom.get().reset(Value::Nil);
        assert!(
            dropped.get(),
            "top-level reads permanently pinned the old payload"
        );
    }

    #[test]
    fn stored_scalar_boxes_preserve_identity_and_release_with_the_last_alias() {
        let atom = GcPtr::new(Atom::new(Value::Nil));
        let dropped = std::rc::Rc::new(std::cell::Cell::new(false));
        {
            let _source = cljrs_gc::alloc_ctx::ScratchGuard::new();
            let _temporary = GcPtr::new(Probe(dropped.clone()));
            let uuid = GcPtr::new(crate::value::UuidValue(42));
            let returned = atom.get().reset(Value::Uuid(uuid.clone()));
            let Value::Uuid(returned) = returned else {
                unreachable!()
            };
            assert!(!GcPtr::ptr_eq(&uuid, &returned));
            assert!(GcPtr::identity_eq(&uuid, &returned));
        }
        assert!(
            dropped.get(),
            "a scalar kept unrelated source temporaries alive"
        );
        let snapshot = atom.get().snapshot();
        let Value::Uuid(uuid) = &snapshot.value else {
            unreachable!()
        };
        assert_eq!(uuid.get().0, 42);
        atom.get().reset(Value::Nil);
        assert_eq!(uuid.get().0, 42);
        drop(snapshot);
        assert!(dropped.get());
    }

    #[test]
    fn exception_payloads_own_data_from_another_scope() {
        let var = GcPtr::new(Var::new("user", "error-payload"));
        let dropped = std::rc::Rc::new(std::cell::Cell::new(false));
        {
            let _data = cljrs_gc::alloc_ctx::ScratchGuard::new();
            let object = Value::NativeObject(gc_native_object(Counted {
                traces: Default::default(),
                dropped: dropped.clone(),
            }));
            let data = MapValue::from_pairs(vec![(Value::Long(1), object)]);
            let _error = cljrs_gc::alloc_ctx::ScratchGuard::new();
            var.get().bind(Value::Error(GcPtr::new(ExceptionInfo::new(
                ValueError::Other("owned".into()),
                "owned".into(),
                Some(data),
                None,
            ))));
        }
        assert!(!dropped.get(), "the error lost its data region");
        {
            let _reads = cljrs_gc::region::ReadScope::nested();
            let Value::Error(error) = var.get().deref().unwrap() else {
                panic!("expected error")
            };
            var.get().bind(Value::Nil);
            assert!(matches!(
                error.get().data().unwrap().get(&Value::Long(1)),
                Some(Value::NativeObject(_))
            ));
        }
        assert!(dropped.get());
    }

    #[test]
    fn definition_roots_release_initializer_temporaries() {
        for atom in [false, true] {
            let var = GcPtr::new(Var::new("user", "packed"));
            let dropped = std::rc::Rc::new(std::cell::Cell::new(false));
            {
                let _source = cljrs_gc::alloc_ctx::ScratchGuard::new();
                let _temporary = GcPtr::new(Probe(dropped.clone()));
                let payload =
                    Value::Vector(GcPtr::new(PersistentVector::from_iter([Value::Long(42)])));
                let value = if atom {
                    Value::Atom(GcPtr::new_isolated(Atom::new(payload.clone())))
                } else {
                    payload.clone()
                };
                var.get().bind(value);
                if !atom {
                    assert!(payload.identical(&var.get().deref().unwrap()));
                }
            }
            assert!(
                dropped.get(),
                "a definition retained an unrelated initializer temporary"
            );
            let _reads = cljrs_gc::region::ReadScope::nested();
            let value = var.get().deref().unwrap();
            let value = match value {
                Value::Atom(atom) => atom.get().deref(),
                value => value,
            };
            assert_eq!(value.to_string(), "[42]");
        }
    }

    #[test]
    fn scalar_storage_uses_small_chunks_and_transient_returns_reuse_initial_ownership() {
        let _mutator = cljrs_gc::register_mutator();
        let array = GcPtr::new(ObjectArray::new(vec![Value::Nil; 1000]));
        let retired =
            || cljrs_gc::governor::with_current_account(|a| a.retired_region_bytes()).unwrap();
        let before = retired();
        for index in 0..1000 {
            let _source = cljrs_gc::alloc_ctx::ScratchGuard::new();
            let value = publish::publish_for_slot(
                Value::string("x"),
                array.get() as *const _ as usize,
                index,
            );
            array.get().0.lock().unwrap()[index] = value;
        }
        assert!(
            retired() - before <= 128 * 1000,
            "scalar copies reserved full-sized scratch chunks"
        );
        for index in 0..1000 {
            let value =
                publish::publish_for_slot(Value::Nil, array.get() as *const _ as usize, index);
            array.get().0.lock().unwrap()[index] = value;
        }
        let _scope = cljrs_gc::alloc_ctx::ScratchGuard::new();
        let traces = std::rc::Rc::new(std::cell::Cell::new(0));
        let value = Value::NativeObject(gc_native_object(Counted {
            traces: traces.clone(),
            dropped: Default::default(),
        }));
        let original = PersistentVector::from_iter(std::iter::repeat_n(value, 10000));
        let transient = GcPtr::new(collections::TransientVector::from_persistent(&original));
        let initial = traces.get();
        assert_eq!(initial, 1, "initial ownership must be captured once");
        for _ in 0..1000 {
            publish::publish_return(Value::TransientVector(transient.clone()));
        }
        let _persistent = transient.get().persistent().unwrap();
        assert_eq!(
            traces.get(),
            initial,
            "transient returns retraced the initial graph"
        );
    }

    #[test]
    fn returned_updates_protect_shared_children_after_the_cell_is_replaced() {
        let atom = GcPtr::new(Atom::new(Value::Nil));
        let dropped = std::rc::Rc::new(std::cell::Cell::new(false));
        {
            let _source = cljrs_gc::alloc_ctx::ScratchGuard::new();
            let object = Value::NativeObject(gc_native_object(Counted {
                traces: Default::default(),
                dropped: dropped.clone(),
            }));
            atom.get().reset(Value::Map(MapValue::from_pairs(vec![(
                Value::Long(0),
                object,
            )])));
        }
        {
            let _consumer = cljrs_gc::alloc_ctx::ScratchGuard::new();
            let previous = atom.get().snapshot();
            let Value::Map(old) = &previous.value else {
                unreachable!()
            };
            let returned = atom
                .get()
                .reset(Value::Map(old.assoc(Value::Long(1), Value::Long(2))));
            drop(previous);
            atom.get().reset(Value::Nil);
            assert!(
                !dropped.get(),
                "replacing the cell freed a child of its returned value"
            );
            let Value::Map(returned) = returned else {
                unreachable!()
            };
            let Value::NativeObject(child) = returned.get(&Value::Long(0)).unwrap() else {
                unreachable!()
            };
            assert_eq!(child.get().type_tag(), "Counted");
        }
        assert!(dropped.get());
    }

    #[test]
    fn metadata_validators_and_watches_release_replaced_callbacks() {
        let _container = cljrs_gc::alloc_ctx::ScratchGuard::new();
        let atom = GcPtr::new(Atom::new(Value::Nil));
        for _ in 0..50 {
            let dropped = std::rc::Rc::new(std::cell::Cell::new(false));
            {
                let _source = cljrs_gc::alloc_ctx::ScratchGuard::new();
                let callback = Value::NativeFunction(GcPtr::new(NativeFn::with_captures(
                    "owned-callback",
                    Arity::Fixed(1),
                    vec![Value::NativeObject(gc_native_object(Counted {
                        traces: Default::default(),
                        dropped: dropped.clone(),
                    }))],
                    |captures, _| Ok(captures[0].clone()),
                )));
                atom.get().set_validator(Some(callback.clone()));
                atom.get().set_meta(Some(callback.clone()));
                atom.get().add_watch(Value::Long(1), callback);
            }
            assert!(!dropped.get());
            {
                let _reader = cljrs_gc::alloc_ctx::ScratchGuard::new();
                let validator = atom.get().get_validator().unwrap();
                assert!(matches!(validator, Value::NativeFunction(_)));
                assert!(atom.get().get_meta().is_some());
                atom.get().set_validator(None);
                atom.get().set_meta(None);
                atom.get().remove_watch(&Value::Long(1));
                assert!(
                    !dropped.get(),
                    "a borrowed callback was freed during replacement"
                );
                let Value::NativeFunction(f) = validator else {
                    unreachable!()
                };
                assert!(matches!(
                    (f.get().func)(&[Value::Nil]).unwrap(),
                    Value::NativeObject(_)
                ));
            }
            assert!(
                dropped.get(),
                "removed auxiliary slots retained their source"
            );
        }
    }

    #[test]
    fn durable_stores_outlive_an_active_read_scope() {
        std::thread::spawn(|| {
            let destination = cljrs_gc::region::AllocationSnapshot::capture();
            let read = cljrs_gc::region::ReadScope::enter().unwrap();
            let dropped = std::rc::Rc::new(std::cell::Cell::new(false));
            let value = {
                let _source = cljrs_gc::alloc_ctx::ScratchGuard::new();
                let value = Value::NativeObject(gc_native_object(Counted {
                    traces: Default::default(),
                    dropped: dropped.clone(),
                }));
                destination.protect(&value);
                value
            };
            drop(read);
            assert!(
                !dropped.get(),
                "a store was shortened to the reader lifetime"
            );
            let Value::NativeObject(value) = value else {
                unreachable!()
            };
            assert_eq!(value.get().type_tag(), "Counted");
        })
        .join()
        .unwrap();
    }

    #[test]
    fn var_rebinding_releases_opaque_roots_after_the_last_reader() {
        let var = GcPtr::new(Var::new("user", "root"));
        for _ in 0..100 {
            let dropped = std::rc::Rc::new(std::cell::Cell::new(false));
            {
                let _source = cljrs_gc::alloc_ctx::ScratchGuard::new();
                let _temporary = GcPtr::new(Probe(dropped.clone()));
                var.get()
                    .bind(Value::NativeFunction(GcPtr::new(NativeFn::with_captures(
                        "root",
                        Arity::Fixed(0),
                        vec![Value::string("alive")],
                        |captures, _| Ok(captures[0].clone()),
                    ))));
            }
            let scope = cljrs_gc::region::ReadScope::enter().unwrap();
            let alias = var.get().deref().unwrap();
            var.get().bind(Value::Nil);
            assert!(!dropped.get());
            let Value::NativeFunction(f) = alias else {
                unreachable!()
            };
            assert_eq!((f.get().func)(&[]).unwrap().to_string(), "\"alive\"");
            drop(scope);
            assert!(dropped.get(), "an obsolete Var root remained permanent");
        }
    }

    #[test]
    fn unscoped_host_reads_retain_raw_results_and_scoped_snapshots_release() {
        std::thread::spawn(|| {
            let atom = GcPtr::new(Atom::new(Value::Nil));
            let var = GcPtr::new(Var::new("user", "host-read"));
            {
                let _source = cljrs_gc::alloc_ctx::ScratchGuard::new();
                let value = Value::string("host payload");
                atom.get().reset(value.clone());
                var.get().bind(value);
            }
            let raw = atom.get().deref();
            let root = var.get().deref().unwrap();
            atom.get().reset(Value::Nil);
            var.get().bind(Value::Nil);
            assert_eq!(raw.to_string(), "\"host payload\"");
            assert_eq!(root.to_string(), "\"host payload\"");
        })
        .join()
        .unwrap();

        let var = GcPtr::new(Var::new("user", "snapshot"));
        let dropped = std::rc::Rc::new(std::cell::Cell::new(false));
        {
            let _source = cljrs_gc::alloc_ctx::ScratchGuard::new();
            var.get()
                .bind(Value::NativeObject(gc_native_object(Counted {
                    traces: Default::default(),
                    dropped: dropped.clone(),
                })));
        }
        let snapshot = var.get().snapshot().unwrap();
        var.get().bind(Value::Nil);
        assert!(!dropped.get());
        drop(snapshot);
        assert!(dropped.get());
    }

    #[test]
    fn large_and_deep_definition_payloads_keep_their_storage() {
        std::thread::Builder::new()
            .stack_size(128 * 1024)
            .spawn(|| {
                let _source = cljrs_gc::alloc_ctx::ScratchGuard::new();
                let large = Value::string("x".repeat(1024 * 1024));
                let atom = Atom::new(large.clone());
                let Value::Str(before) = large else {
                    unreachable!()
                };
                let Value::Str(after) = atom.snapshot().value else {
                    unreachable!()
                };
                assert!(GcPtr::ptr_eq(&before, &after), "large string was copied");
                let large = Value::Vector(GcPtr::new(PersistentVector::from_iter(
                    (0..10000).map(Value::Long),
                )));
                let atom = Atom::new(large.clone());
                let Value::Vector(before) = large else {
                    unreachable!()
                };
                let Value::Vector(after) = atom.snapshot().value else {
                    unreachable!()
                };
                assert!(
                    GcPtr::ptr_eq(&before, &after),
                    "large shared vector was rebuilt"
                );
                let mut chain = Value::Nil;
                for i in 0..30000 {
                    chain = Value::Cons(GcPtr::new(CljxCons {
                        head: Value::Long(i),
                        tail: chain,
                    }));
                }
                let atom = Atom::new(chain);
                let mut current = atom.snapshot().value;
                let mut count = 0;
                while let Value::Cons(cons) = current {
                    count += 1;
                    current = cons.get().tail.clone();
                }
                assert_eq!(count, 30000);
            })
            .unwrap()
            .join()
            .unwrap();
    }

    #[test]
    fn shared_captured_graphs_are_walked_only_once() {
        let _source = cljrs_gc::alloc_ctx::ScratchGuard::new();
        let traces = std::rc::Rc::new(std::cell::Cell::new(0));
        let dropped = std::rc::Rc::new(std::cell::Cell::new(false));
        let object = Value::NativeObject(gc_native_object(Counted {
            traces: traces.clone(),
            dropped: dropped.clone(),
        }));
        let captured = Value::Vector(GcPtr::new(PersistentVector::from_iter(
            std::iter::repeat_n(object, 10000),
        )));
        for _ in 0..1000 {
            let _call = cljrs_gc::alloc_ctx::ScratchGuard::new();
            let closure = GcPtr::new_isolated(NativeFn::with_captures(
                "capture",
                Arity::Fixed(0),
                vec![captured.clone()],
                |values, _| Ok(values[0].clone()),
            ));
            publish::publish_return(Value::NativeFunction(closure));
        }
        assert_eq!(
            traces.get(),
            1,
            "closure creation retraced the shared graph"
        );
        assert!(!dropped.get());
    }

    #[test]
    fn indexed_storage_releases_overwritten_sources_and_failed_deliveries() {
        let _container = cljrs_gc::alloc_ctx::ScratchGuard::new();
        let array = GcPtr::new(ObjectArray::new(vec![Value::Nil; 2]));
        let mut previous: Option<std::rc::Rc<std::cell::Cell<bool>>> = None;
        for _ in 0..100 {
            let dropped = std::rc::Rc::new(std::cell::Cell::new(false));
            {
                let _source = cljrs_gc::alloc_ctx::ScratchGuard::new();
                let value = Value::NativeObject(gc_native_object(Counted {
                    traces: Default::default(),
                    dropped: dropped.clone(),
                }));
                let value = publish::publish_for_slot(value, array.get() as *const _ as usize, 0);
                array.get().0.lock().unwrap()[0] = value;
            }
            if let Some(previous) = previous {
                assert!(previous.get());
            }
            previous = Some(dropped);
        }
        let value = publish::publish_for_slot(Value::Nil, array.get() as *const _ as usize, 0);
        array.get().0.lock().unwrap()[0] = value;
        assert!(previous.unwrap().get());
        let promise = CljxPromise::new();
        promise.deliver(Value::Long(1));
        let discarded = std::rc::Rc::new(std::cell::Cell::new(false));
        {
            let _source = cljrs_gc::alloc_ctx::ScratchGuard::new();
            promise.deliver(Value::NativeObject(gc_native_object(Counted {
                traces: Default::default(),
                dropped: discarded.clone(),
            })));
        }
        assert!(
            discarded.get(),
            "an unsuccessful promise delivery retained its candidate"
        );
        assert_eq!(promise.deref_blocking(), Value::Long(1));
    }

    #[test]
    fn regional_cells_release_each_replaced_payload_without_changing_identity() {
        let _container = cljrs_gc::alloc_ctx::ScratchGuard::new();
        let atom = GcPtr::new(Atom::new(Value::Nil));
        let other = GcPtr::new(Atom::new(Value::Nil));
        for _ in 0..100 {
            let dropped = std::rc::Rc::new(std::cell::Cell::new(false));
            {
                let _source = cljrs_gc::alloc_ctx::ScratchGuard::new();
                let _temporary = GcPtr::new(Probe(dropped.clone()));
                let vector = GcPtr::new(PersistentVector::from_iter([Value::Long(1)]));
                let returned = atom.get().reset(Value::Vector(vector.clone()));
                let Value::Vector(returned) = returned else {
                    unreachable!()
                };
                assert!(GcPtr::ptr_eq(&vector, &returned));
                other.get().reset(Value::Vector(vector));
            }
            assert!(!dropped.get());
            atom.get().reset(Value::Nil);
            assert!(!dropped.get(), "another slot still owns the same source");
            other.get().reset(Value::Nil);
            assert!(
                dropped.get(),
                "obsolete regional payload kept its source alive"
            );
        }
    }

    #[test]
    fn replacement_does_not_inherit_the_previous_payloads_read_lease() {
        let _container = cljrs_gc::alloc_ctx::ScratchGuard::new();
        let atom = GcPtr::new(Atom::new(Value::Nil));
        let mut previous: Option<std::rc::Rc<std::cell::Cell<bool>>> = None;
        for _ in 0..100 {
            let dropped = std::rc::Rc::new(std::cell::Cell::new(false));
            {
                let _iteration = cljrs_gc::alloc_ctx::ScratchGuard::new();
                let _old = atom.get().deref();
                atom.get()
                    .reset(Value::NativeObject(gc_native_object(Counted {
                        traces: Default::default(),
                        dropped: dropped.clone(),
                    })));
            }
            if let Some(old) = previous {
                assert!(
                    old.get(),
                    "a read became a durable dependency of the replacement"
                );
            }
            previous = Some(dropped);
        }
        atom.get().reset(Value::Nil);
        assert!(previous.unwrap().get());
    }

    #[test]
    fn static_cells_release_replaced_opaque_payloads_and_preserve_borrowed_aliases() {
        let atom = GcPtr::new(Atom::new(Value::Nil));
        let volatile = GcPtr::new(Volatile::new(Value::Nil));
        for _ in 0..100 {
            let dropped = std::rc::Rc::new(std::cell::Cell::new(false));
            {
                let _scope = cljrs_gc::alloc_ctx::ScratchGuard::new();
                let _temporary = GcPtr::new(Probe(dropped.clone()));
                let payload = Value::NativeFunction(GcPtr::new(NativeFn::with_captures(
                    "payload",
                    Arity::Fixed(0),
                    vec![Value::string("alive")],
                    |captures, _| Ok(captures[0].clone()),
                )));
                atom.get().reset(payload.clone());
                volatile.get().reset(payload);
            }
            assert!(!dropped.get());
            {
                let _consumer = cljrs_gc::alloc_ctx::ScratchGuard::new();
                let alias = atom.get().deref();
                atom.get().reset(Value::Nil);
                volatile.get().reset(Value::Nil);
                assert!(!dropped.get(), "a live alias lost its backing scope");
                let Value::NativeFunction(f) = alias else {
                    unreachable!()
                };
                assert_eq!((f.get().func)(&[]).unwrap().to_string(), "\"alive\"");
            }
            assert!(
                dropped.get(),
                "an obsolete opaque payload retained its scope"
            );
        }
        let dropped = std::rc::Rc::new(std::cell::Cell::new(false));
        {
            let _scope = cljrs_gc::alloc_ctx::ScratchGuard::new();
            let _temporary = GcPtr::new(Probe(dropped.clone()));
            let rejected =
                Value::ObjectArray(GcPtr::new(ObjectArray(std::sync::Mutex::new(vec![]))));
            assert!(!atom.get().compare_and_set(&Value::Long(1), rejected));
        }
        assert!(dropped.get(), "a failed CAS retained its candidate");
    }

    #[test]
    fn opaque_publication_preserves_identity_and_releases_unrelated_scopes() {
        let dropped = std::rc::Rc::new(std::cell::Cell::new(false));
        let outer = cljrs_gc::alloc_ctx::ScratchGuard::new();
        let probe = cljrs_gc::GcPtr::new(Probe(dropped.clone()));
        let inner = cljrs_gc::alloc_ctx::ScratchGuard::new();
        let array = cljrs_gc::GcPtr::new(ObjectArray(std::sync::Mutex::new(vec![Value::Long(1)])));
        let value = Value::Vector(cljrs_gc::GcPtr::new(PersistentVector::from_iter(vec![
            Value::ObjectArray(array.clone()),
        ])));
        let published = publish::publish_value(value);
        drop(inner);
        drop(probe);
        drop(outer);
        assert!(
            dropped.get(),
            "an unrelated region was permanently retained"
        );
        let Value::Vector(values) = published else {
            panic!("not a vector")
        };
        let Value::ObjectArray(received) = values.get().iter().next().unwrap() else {
            panic!("not an array")
        };
        assert!(cljrs_gc::GcPtr::ptr_eq(&array, received));
        array.get().0.lock().unwrap()[0] = Value::Long(42);
        assert_eq!(received.get().0.lock().unwrap()[0], Value::Long(42));
    }
}