tur/weak
weak<T> non-owning references, and the cycle break they exist for.
Since: stdlib-weak-ref-audit WR1
rc/downgrade
(rc/downgrade [A])
create a non-owning weak<A> observer of an rc<A>.
| r | the rc<A> to observe (borrowed, not consumed) |
A weak<A> observing the same allocation without owning it. Release it with `weak/drop` when done: a weak is NOT dropped automatically at scope exit, and the control block outlives its value until the last weak goes away.
(let [r (rc/of 42)]
(let [w (rc/downgrade r)]
(println (rc/strong-count r)) ; => 1 -- a weak adds no strong count
(weak/drop w)))
Since: stdlib-weak-ref-audit WR1
weak/upgrade
(weak/upgrade [A])
try to reclaim a strong rc<A> from a weak<A>.
| w | the weak<A> to upgrade (borrowed, not consumed) |
(Option rc<A>) -- some(strong handle) if alive, none if the value is gone.
(let [o (weak/upgrade w)]
(if (some? o)
(let [s (weak/unwrap o)]
(rc/drop s)) ; the upgraded handle is ours to release
(println "gone")))
Since: stdlib-weak-ref-audit WR1
weak/unwrap
(weak/unwrap [A])
take the rc<A> out of a successful (weak/upgrade w).
| o | the (Option rc<A>) returned by weak/upgrade |
The contained rc<A>. The caller owns it and must `rc/drop` it.
(let [o (weak/upgrade w)]
(when (some? o)
(let [s (weak/unwrap o)]
(rc/drop s))))
Since: stdlib-weak-ref-audit WR1
weak/alive?
(weak/alive? [A])
is the weak's referent still alive?
| w | the weak<A> to test (borrowed, not consumed) |
true if a strong handle still exists, false once the value has been freed.
(let [w (let [r (rc/of 99)] (rc/downgrade r))]
(println (weak/alive? w)) ; => false -- r died at scope exit
(weak/drop w))
Since: stdlib-weak-ref-audit WR1
weak/drop
(weak/drop [A])
release a weak<A> observation.
| w | the weak<A> to release (consumed) |
Nothing.
(let [r (rc/of 42)]
(let [w (rc/downgrade r)]
(weak/drop w)))
Since: stdlib-weak-ref-audit WR1