Skip to content

stdlib.rc

Reference Counting

Generated from v0.60.1. 1 source files, 18 documented symbols.

rc.xi

type RcInner

Control block behind Rc/Weak: strong and weak counts plus the value.

Field Type
strong Int
weak Int
value T

Invariants: - strong >= 0 - weak >= 0 - strong + weak > 0

type Rc

Shared-ownership smart pointer; cloning a handle bumps the strong count.

Field Type
ptr *RcInner[T]

fn new[T](value: T) -> Rc[T]

Allocate a fresh control block with strong count 1 and no weak handles.

  • Postcondition: strong_count == 1
  • Postcondition: ptr != null

fn clone[T](self: Self) -> Rc[T]

Increment the strong count; the returned handle aliases the same value.

  • Precondition: ptr != null
  • Postcondition: strong_count() == strong_count()@pre + 1

fn strong_count[T](self: Self) -> Int

Number of strong handles; at least 1 while any Rc is live.

  • Precondition: ptr != null
  • Postcondition: result >= 1

fn weak_count[T](self: Self) -> Int

Number of weak handles (they do not keep the value alive).

  • Precondition: ptr != null

fn get[T](self: Self) -> T

Read the value by value (moves/copies it out of the control block).

  • Precondition: ptr != null

fn ptr_eq[T, U](self: Self, other: &Rc[U]) -> Bool

True when both handles reference the same control block (T and U may differ).

  • Precondition: ptr != null

fn downgrade[T](self: Self) -> Weak[T]

Create a Weak handle; it does not keep the value alive.

  • Precondition: ptr != null
  • Postcondition: result.weak_count() > 0

fn unwrap_or_clone[T](self: Self) -> T

Return the value without cloning when this is the last strong handle; otherwise return a clone.

fn drop[T](self: Self)

Decrement the strong count; free the control block when the last strong and weak handles are gone.

  • Precondition: ptr != null

fn deref[T](self: Self) -> &T

Generated summary: Method; Takes no arguments; returns &T. No source comment yet.

  • Precondition: ptr != null
  • Postcondition: true

fn as_ref[T](self: Self) -> &T

Generated summary: Method; Takes no arguments; returns &T. No source comment yet.

  • Precondition: ptr != null

type Weak

Non-owning handle; upgrade re-acquires a strong Rc while the value is alive.

Field Type
ptr *RcInner[T]

fn upgrade[T](self: Self) -> Option[Rc[T]]

Some(new strong handle) while the value is alive, None after the last strong handle dropped.

  • Precondition: ptr != null
  • Postcondition: result is Some(_) => strong_count() == strong_count()@pre + 1
  • Postcondition: result is None => strong_count() == 0

fn strong_count[T](self: Self) -> Int

Strong count observed through the weak handle (0 after the value dies).

  • Precondition: ptr != null

fn weak_count[T](self: Self) -> Int

Weak count observed through the weak handle.

  • Precondition: ptr != null

fn drop[T](self: Self)

Decrement the weak count; free the control block when no strong or weak handles remain.