Function in gc compiler

upgrade

fn upgrade<T>(Weak<T> handle) -> Option<T>
Import
use gc::{upgrade};
Source: compiler/src/builtin_docs/gc.hy

Returns the value while it is still alive, None once it was collected.

A weak referent is cleared during a collection in which nothing strong (a variable, a field, a Root, …) refers to it any more. Class drop finalizers run before weaks are cleared and can still upgrade a weak to the object being dropped.

Parameters#

NameType
handleWeak<T>

Returns#

Option<T>

Examples#

use gc::{weak, upgrade, collect, Weak};
use tests::assert;
fn make() -> Weak<Vec<int>> {
    let v = Vec::from([1, 2, 3]);
    // `v`, the only strong reference, ends with this call.
    return weak(v);
}

fn main() {
    let w = make();
    collect();
    let gone = match upgrade(w) {
        Option::Some(_) => false,
        Option::None => true,
    };
    assert!(gone);
}

See also#

  • gc::Root
  • root: Pins value so the garbage collector keeps it alive while the returned Root is reachable.
  • get: Returns the pinned value, leaving the pin in place.
  • unroot: Releases the pin and returns the value it held.
  • weak: Makes a handle to value that does not keep it alive.
  • heap_bytes: Returns the size of the managed heap in bytes.
  • collect: Runs a full garbage collection now and returns the number of bytes freed.
↑ ↓ navigate ↵ open