Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-base

Source of release 0.44.0

0.44.0 / src / std / ownership / weak.lucb

#==============================================================================================
#
#   weak - Weak references
#
#   DESCRIPTION:
#       A weak reference watches an object without keeping it alive, and yields it again only
#       while it is still alive.
#
#==============================================================================================

# mark: Weak references ========================================================================
## A weak reference: it watches an object without keeping it alive.
pub struct Weak:
    ## The watched object, or none once it is gone.
    pub var target: Object*?

## A weak reference watching `o`.
pub func weak_make(o: Object*) -> Weak:
    check_alive(o)
    if not has(o, immortal):
        o.weak += 1
    return Weak(target = o)

## A copy of `w`, counting one more watcher of its object.
pub func weak_copy(w: Weak) -> Weak:
    if let o = w.target:
        check_thread(o)
        if not has(o, immortal):
            o.weak += 1
    return w

## Drop `w`; the object's shell is freed when its last watcher goes.
pub func weak_drop(w: Weak):
    if let o = w.target:
        check_thread(o)
        if not has(o, immortal):
            o.weak -= 1
            release_shell(o)

## The object with a fresh strong reference, or none once it is dead.
pub func weak_get(w: Weak) -> Object*?:
    if let o = w.target:
        check_thread(o)
        if not has(o, dead):
            return retain(o)
    return none