A reference-counted object provides a way to manage shared resources that need cleanup
when they're no longer in use. There are two types of references:
Strong references (Rc<T>):
Created with Rc() or Rc.clone()
Keep the resource alive as long as at least one strong reference exists
Must be explicitly disposed with Rc.dispose() when no longer needed
Weak references (Weak<T>):
Created with Rc.weak() or Rc.intoWeak()
Don't prevent the resource from being cleaned up
Can be upgraded to strong references with Rc.upgrade() if the resource is still alive
When the last strong reference to a resource is disposed, the resource's cleanup
function will be called automatically. Weak references can still exist at this point,
but they can no longer be upgraded to strong references.
An Rc<T> is a proxy to the inner object, so you can use the Rc to access all the
properties and methods of the inner object (assuming the reference is not disposed).
Example:
constresource = Rc(connection, conn=>conn.close()); resource.query("SELECT count(*) FROM users");
// Creates another strong reference constref2 = Rc.clone(resource); // Creates a weak reference constweak = Rc.weak(resource);
// Resource stays alive (ref2 exists) Rc.dispose(resource); // Returns Some(Rc<T>) (resource still alive) constupgraded = Rc.upgrade(weak); // Resource is cleaned up Rc.dispose(ref2); // Returns None (resource disposed) constfailed = Rc.upgrade(weak);
Overview
A reference-counted object provides a way to manage shared resources that need cleanup when they're no longer in use. There are two types of references:
Strong references (
Rc<T>
):Rc()
orRc.clone()
Rc.dispose()
when no longer neededWeak references (
Weak<T>
):Rc.weak()
orRc.intoWeak()
Rc.upgrade()
if the resource is still aliveWhen the last strong reference to a resource is disposed, the resource's cleanup function will be called automatically. Weak references can still exist at this point, but they can no longer be upgraded to strong references.
An
Rc<T>
is a proxy to the inner object, so you can use theRc
to access all the properties and methods of the inner object (assuming the reference is not disposed).Example:
The
intoWeak
function is a special case that:This is equivalent to, but more efficient than:
See
Rc for the
Rc
API