std.weak
The weak reference module, used together with Arc to break reference cycles.
use std.weakThis module depends on atomic reference counting and is not exported for the
wasm32target.
Function Overview
| Function | Signature |
|---|---|
new | (T: Type)(arc: Arc(T)) -> Weak(T) |
upgrade | (T: Type)(weak: Weak(T)) -> Option(Arc(T)) |
Functions
new
new: (T: Type)(arc: Arc(T)) -> Weak(T)Creates the corresponding weak reference from an Arc.
arc— strong reference value; passed by value and therefore moved on call
Returns: a weak reference handle.
Known gap: The current implementation of
weak.newisRuntimeValue::Weak(Arc::downgrade(&Arc::new(arc.clone())))(src/std/weak.rs:33-39)—it first clones the value of theArcargument into a newly createdArc, then callsdowngradeon this temporaryArc. Therefore:
- The weak reference does not point to the original
Arc's allocation, but to a brand-new allocation with identical contents;- The temporary
Arcis destroyed when the function returns, bringing the strong reference count to zero → empirically,weak.upgrade(weak.new(p))always evaluates toOption.none()and expires immediately.In other words, “does not increment the strong reference count” holds, but “can be upgraded back to the original value” currently does not hold. When sharing across scopes, use
ref; do not rely on upgradingWeak.
use std.assert
use std.weak
main: () -> Void = {
// ref creates an Arc[Int]
p = ref 42
// Arc → Weak registration
w = weak.new(p)
assert(true)
}upgrade
upgrade: (T: Type)(weak: Weak(T)) -> Option(Arc(T))Attempts to upgrade a weak reference to a strong reference.
weak— weak reference handle
Returns: Option.some(Arc) if the allocation is still alive, or Option.none() if it has been released. No error is raised—use Option to indicate “whether the target still exists.”
With the current implementation of
weak.new(see “Known Gap” above), a newly created weak reference always returnsnone(). The following example demonstrates destructuring syntax, but actually takes thenonebranch.
use std.weak
use std.option
main: () -> Void = {
p = ref 42
w = weak.new(p)
// upgrade: returns some(v) while the target is alive, and none() after it is released
u = weak.upgrade(w)
match u {
some(v) -> println("alive"),
none() -> println("dropped"),
}
}Variant destructuring prerequisite: Variant destructuring for
Optionrequires the variant set to be in scope—after importing withuse std.option, you canmatch some(v)/none()(see Language Specification §2.8, match).
Semantic Notes
A weak reference does not hold ownership: the existence of a Weak does not prevent the target from being released. A typical use is to break reference cycles—a parent node holds an Arc pointing to a child node, while the child node holds only a Weak pointing back to the parent, breaking the cycle.
The above is the design intent. With the current implementation (see “Known Gap” in
new),Weakcannot yet truly fulfill this role in this version.
Related
- Language Specification: Type System — Ownership semantics of
Arc/Weak
