The author presents safe-gc, a Rust garbage collection library implemented entirely without unsafe code — no unsafe in the API, no unsafe in the implementation, enforced via forbid(unsafe_code). The library uses a mark-and-sweep algorithm with arenas backed by Vec, indexed via Gc<T> and Root<T> types rather than direct pointer derefs, which is the key design choice enabling safe Rust compliance. The post explains the heap structure, root set management, the two-level mark stack for heterogeneous types, and how classic GC footguns like dangling references and finalizer misuse are downgraded from memory-safety violations to ordinary bugs. The author also details a failed attempt at implementing a copying collector in safe Rust, explaining why heterogeneous heap types make forwarding pointers difficult to express within Rust's borrow checker.
Table of contents
Using safe-gcPeeking Under the HoodPreventing Classic FootgunsCopying Collector False StartWhy safe-gc ?ConclusionSort: