cell #
cell
RefCell[T] checks callback access at runtime for single-threaded code. Use new_ref_cell(value) to create a cell, with(callback) to read its value, and with_mut(callback) to replace it with the callback's return value. Shared callbacks may nest. An exclusive callback excludes both shared and exclusive access. The try_with and try_borrow methods return none on a conflicting exclusive borrow; try_with_mut returns false on any conflict. The other methods panic on conflicts.
Borrow counts are released when a callback returns. try_borrow does not keep a borrow active. Values follow ordinary V assignment semantics, including references contained in T; the cell does not provide deep copies or track aliases retained outside callbacks. Do not mutate such aliases during shared access. Cells do not synchronize access across threads.
fn new_ref_cell #
fn new_ref_cell[T](value T) &RefCell[T]
new_ref_cell creates a RefCell holding value, initially unborrowed.
fn (RefCell[T]) with #
fn (mut c RefCell[T]) with(f fn (v T) T) T
with runs f on the value under a shared borrow and returns its result. It panics if the cell is exclusively borrowed.
fn (RefCell[T]) try_with #
fn (mut c RefCell[T]) try_with(f fn (v T) T) ?T
try_with is with without the panic: it returns none if the cell is exclusively borrowed.
fn (RefCell[T]) with_mut #
fn (mut c RefCell[T]) with_mut(f fn (v T) T)
with_mut runs f on the value under an exclusive borrow and stores the result back. It panics if the cell is borrowed in any way.
fn (RefCell[T]) try_with_mut #
fn (mut c RefCell[T]) try_with_mut(f fn (v T) T) bool
try_with_mut is with_mut without the panic: it returns false (leaving the value untouched) if the cell is borrowed in any way.
fn (RefCell[T]) try_borrow #
fn (c &RefCell[T]) try_borrow() ?T
try_borrow returns the value using ordinary V value semantics, or none if the cell is exclusively borrowed. This access is not tracked afterwards; references contained in T are not deep-cloned. Update through with_mut.
struct RefCell #
struct RefCell[T] {
mut:
value T
borrows int // >0: shared borrows held; -1: exclusively borrowed
}
RefCell is a container with runtime-checked borrowing: many shared borrows may coexist, but an exclusive borrow excludes all others, inspired by Rust's std::cell::RefCell. Unlike shared variables it is single-threaded and needs no locks; unlike plain references the checks happen at runtime. Access goes through callbacks, since V has no guard lifetimes.