Rc API Reference

Reference-counted smart pointer for shared ownership

Import

U std/rc

Structs

Rc

S Rc { ptr: i64 }

Single-threaded reference counting. Layout: {ref_count: i64, value: ...}.

Weak

S Weak { ptr: i64 }

Non-owning reference that does not prevent deallocation.

Rc Methods

MethodSignatureDescription
newF new(value: i64, value_size: i64) -> RcCreate with ref_count = 1
from_i64F from_i64(value: i64) -> RcCreate Rc for i64 value
cloneF clone(&self) -> RcIncrement ref count, return copy
getF get(&self) -> i64Get the inner value (for i64)
setF set(&self, value: i64) -> i64Set the inner value (for i64)
ref_countF ref_count(&self) -> i64Get current ref count
retainF retain(&self) -> i64Increment ref count
releaseF release(&self) -> i64Decrement ref count, free if 0
is_uniqueF is_unique(&self) -> i64Check if only reference
downgradeF downgrade(&self) -> WeakCreate a Weak reference
dropF drop(&self) -> i64Decrement ref count, free if 0

Weak Methods

MethodSignatureDescription
upgradeF upgrade(&self) -> i64Try to upgrade to Rc (returns 1 on success, 0 if freed)
is_aliveF is_alive(&self) -> i64Check if referenced value still exists

Free Functions

FunctionSignatureDescription
rc_newF rc_new(value: i64) -> RcHelper to create Rc
rc_cloneF rc_clone(rc: Rc) -> RcHelper to clone Rc
rc_dropF rc_drop(rc: Rc) -> i64Helper to drop Rc

Usage

U std/rc

F main() -> i64 {
    # Create Rc
    a := Rc.from_i64(42)
    b := a.clone()       # ref_count = 2
    val := b.get()       # 42

    # Check uniqueness
    I !a.is_unique() {
        puts("Shared reference")
    }

    # Weak reference
    weak := a.downgrade()
    I weak.is_alive() {
        puts("Still alive")
    }

    b.drop()             # ref_count = 1
    a.drop()             # ref_count = 0, freed
    0
}