| Crates.io | rudo-gc |
| lib.rs | rudo-gc |
| version | 0.2.3 |
| created_at | 2026-01-16 15:53:10.501762+00 |
| updated_at | 2026-01-25 17:30:08.282412+00 |
| description | A garbage-collected smart pointer with automatic cycle detection using BiBOP memory layout and Mark-Sweep collection |
| homepage | |
| repository | https://github.com/XuHaoJun/rudo |
| max_upload_size | |
| id | 2048819 |
| size | 279,436 |
A garbage-collected smart pointer library for Rust with automatic cycle detection.
Gc<T> smart pointer: Similar to Rc<T>, but with automatic cycle detection#[derive(Trace)]: Easy integration for custom typesAdd to your Cargo.toml:
[dependencies]
rudo-gc = "0.1"
Basic usage:
use rudo_gc::{Gc, Trace};
// Simple allocation
let x = Gc::new(42);
println!("Value: {}", *x);
// Shared ownership
let y = Gc::clone(&x);
assert!(Gc::ptr_eq(&x, &y));
// Custom types with derive
#[derive(Trace)]
struct Node {
value: i32,
next: Option<Gc<Node>>,
}
let node = Gc::new(Node { value: 1, next: None });
Unlike Rc<T>, Gc<T> can collect cyclic references:
use rudo_gc::{Gc, Trace, collect};
use std::cell::RefCell;
#[derive(Trace)]
struct Node {
next: RefCell<Option<Gc<Node>>>,
}
let a = Gc::new(Node { next: RefCell::new(None) });
let b = Gc::new(Node { next: RefCell::new(None) });
// Create cycle: a -> b -> a
*a.next.borrow_mut() = Some(Gc::clone(&b));
*b.next.borrow_mut() = Some(Gc::clone(&a));
drop(a);
drop(b);
collect(); // Cycle is detected and freed
Gc<T>: Garbage-collected smart pointerTrace: Trait for types that can be traced by the GCVisitor: Trait for traversing the object graphCollectInfo: Statistics about heap statecollect(): Force immediate garbage collectionset_collect_condition(f): Set custom collection triggerdefault_collect_condition(info): The default trigger (amortized O(1))Gc::new(value): Create a new GcGc::new_cyclic(f): Create a self-referential GcGc::clone(&gc): Create another referenceGc::ptr_eq(&a, &b): Check if two Gcs point to the same allocationGc::ref_count(&gc): Get current reference countGc::is_dead(&gc): Check if Gc is dead (during Drop)Gc::try_deref(&gc): Fallible dereferenceGc::try_clone(&gc): Fallible cloneGc<T> is !Send and !Sync - it can only be used within a single thread.
Each thread has its own heap and garbage collector.
This library is inspired by:
Trace trait┌─────────────────────────────────────────────────────────┐
│ User Code │
│ Gc::new(val) → allocate in heap │
│ drop(gc) → may trigger collection │
└─────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ GlobalHeap │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │ Seg<16> │ │ Seg<32> │ │ Seg<64> │ ... │
│ └─────────┘ └─────────┘ └─────────┘ │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │ Page │ │ Page │ │ Page │ │
│ │ Header │ │ Header │ │ Header │ │
│ │ + Slots │ │ + Slots │ │ + Slots │ │
│ └─────────┘ └─────────┘ └─────────┘ │
└─────────────────────────────────────────────────────────┘
MIT OR Apache-2.0