Skip to main content

gc_node_usage/
gc_node_usage.rs

1use gc_lite::{
2    GcError, GcHeap, GcNode, GcPartitionId, GcRef, GcResult, GcScope, GcTrace, GcTraceCtx,
3    gc_type_register,
4};
5
6#[derive(Debug)]
7struct StaticNode {
8    _value: i32,
9}
10
11impl GcTrace for StaticNode {
12    fn trace(&self, _ctx: &mut GcTraceCtx) {}
13}
14
15#[derive(Debug)]
16struct OtherNode {
17    _value: i32,
18}
19
20impl GcTrace for OtherNode {
21    fn trace(&self, _ctx: &mut GcTraceCtx) {}
22}
23
24gc_type_register! {
25    StaticNode, drop_pass = 1;
26    OtherNode;
27}
28
29fn alloc_static(
30    heap: &mut GcHeap,
31    scope: GcPartitionId,
32    value: i32,
33) -> GcResult<GcRef<StaticNode>> {
34    let ctx = GcScope::new(heap, scope);
35    let r = ctx
36        .alloc_local(StaticNode { _value: value })
37        .map_err(|(err, _)| err)?;
38    ctx.flush();
39    Ok(r)
40}
41
42fn alloc_other(heap: &mut GcHeap, scope: GcPartitionId, value: i32) -> GcResult<GcRef<OtherNode>> {
43    let ctx = GcScope::new(heap, scope);
44    let r = ctx
45        .alloc_local(OtherNode { _value: value })
46        .map_err(|(err, _)| err)?;
47    ctx.flush();
48    Ok(r)
49}
50
51fn main() -> GcResult<()> {
52    let mut heap = GcHeap::new(&GC_TYPE_REGISTRY);
53    let scope = heap.create_partition();
54
55    let static_ref = alloc_static(&mut heap, scope, 10)?;
56    let other_ref = alloc_other(&mut heap, scope, 20)?;
57
58    println!("Static node value: {}", static_ref._value);
59    println!("Other node value: {}", other_ref._value);
60
61    println!("gc_node_usage example verified");
62
63    Ok(())
64}