reify-graph 0.1.1

Safe Rc/Arc graph reification and reconstruction
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#![deny(unsafe_code)]

//! Convert `Rc<RefCell<T>>` and `Arc<Mutex<T>>` pointer graphs into a flat,
//! inspectable, serializable form, and back.
//!
//! Pointer-shaped data (trees with shared subtrees, DAGs, cyclic graphs)
//! is awkward to serialize, log, or transform: walking it naively either
//! duplicates shared nodes or loops forever on cycles. This crate gives
//! you a small, non-invasive API to:
//!
//! - **Reify**: turn a pointer graph rooted at an `Rc<RefCell<T>>` (or
//!   `Arc<Mutex<T>>`) into a flat [`ReifiedGraph`] of nodes and edges,
//!   keyed by pointer identity, detecting cycles and preserving sharing.
//! - **Reflect**: rebuild the original pointer graph from a [`ReifiedGraph`],
//!   restoring the same sharing topology (one allocation per node id, even
//!   when many edges point to it).
//!
//! With the default `serde` feature enabled, [`ReifiedGraph`] is
//! `Serialize` + `Deserialize`, which gives you JSON / postcard / etc.
//! serialization of arbitrary cyclic data essentially for free.
//!
//! # When to use it
//!
//! - You want to dump or log a graph for debugging (an AST with shared
//!   sub-expressions, a circuit netlist, a scene graph).
//! - You want to send pointer-shaped state across a process boundary.
//! - You want to apply a structural transform (renumbering, GC, deduping)
//!   that's awkward on the live `Rc` graph but easy on a flat node+edge
//!   form.
//!
//! # Design choices
//!
//! - Node identity comes from [`Rc::as_ptr`] (or `Arc::as_ptr`) cast to
//!   `usize`. No traits or wrappers required on your `T`.
//! - Children are extracted by a closure you supply, so the library never
//!   guesses at the structure of your type.
//! - Node data is cloned into the [`ReifiedGraph`] (use `Arc<Inner>` inside
//!   `T` if cloning is expensive).
//! - Reconstruction in [`reflect_graph`] preserves sharing exactly: each
//!   [`NodeId`] becomes one [`Rc`] and is re-pointed everywhere it was
//!   referenced.
//!
//! See the [`docs/phase2-graph-reification.md`][phase2] design note, and
//! the [`serialize_graph` example][example] for an end-to-end `serde_json`
//! round trip.
//!
//! [phase2]: https://github.com/joshburgess/reify-reflect/blob/main/docs/phase2-graph-reification.md
//! [example]: https://github.com/joshburgess/reify-reflect/blob/main/reify-graph/examples/serialize_graph.rs
//!
//! # Examples
//!
//! Round-trip a small `Rc<RefCell<_>>` tree through the flat form:
//!
//! ```
//! use reify_graph::{reify_graph, reflect_graph};
//! use std::cell::RefCell;
//! use std::rc::Rc;
//!
//! #[derive(Clone, Debug)]
//! struct Node {
//!     value: i32,
//!     children: Vec<Rc<RefCell<Node>>>,
//! }
//!
//! let leaf = Rc::new(RefCell::new(Node { value: 1, children: vec![] }));
//! let root = Rc::new(RefCell::new(Node {
//!     value: 0,
//!     children: vec![leaf.clone()],
//! }));
//!
//! // Flatten: 2 nodes, 1 edge
//! let graph = reify_graph(root.clone(), |n| n.children.clone());
//! assert_eq!(graph.nodes.len(), 2);
//! assert_eq!(graph.edges.len(), 1);
//!
//! // Rebuild, restoring the original sharing
//! let reconstructed = reflect_graph(graph, |n, kids| n.children = kids);
//! assert_eq!(reconstructed.borrow().value, 0);
//! assert_eq!(reconstructed.borrow().children[0].borrow().value, 1);
//! ```
//!
//! For `Arc<Mutex<T>>` graphs (the same pattern, thread-safe), see the
//! [`arc`] module's `reify_graph_arc` and `reflect_graph_arc`.

use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;

pub mod arc;

/// A stable node identifier derived from pointer identity.
///
/// # Examples
///
/// ```
/// use reify_graph::NodeId;
///
/// let id = NodeId(42);
/// assert_eq!(id.0, 42);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct NodeId(pub usize);

/// An adjacency-list representation of a pointer graph.
///
/// Contains all nodes with their data, directed edges between nodes,
/// and the identifier of the root node.
///
/// # Examples
///
/// ```
/// use reify_graph::{ReifiedGraph, NodeId};
///
/// let graph = ReifiedGraph {
///     nodes: vec![(NodeId(0), "root"), (NodeId(1), "child")],
///     edges: vec![(NodeId(0), NodeId(1))],
///     root: NodeId(0),
/// };
/// assert_eq!(graph.nodes.len(), 2);
/// ```
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ReifiedGraph<T> {
    /// The nodes in the graph, each paired with its stable identifier.
    pub nodes: Vec<(NodeId, T)>,
    /// Directed edges from parent to child.
    pub edges: Vec<(NodeId, NodeId)>,
    /// The root node's identifier.
    pub root: NodeId,
}

/// Extract a [`NodeId`] from an `Rc<RefCell<T>>` using pointer identity.
///
/// # Examples
///
/// ```
/// use reify_graph::{node_id_of, NodeId};
/// use std::cell::RefCell;
/// use std::rc::Rc;
///
/// let a = Rc::new(RefCell::new(42));
/// let b = a.clone();
/// assert_eq!(node_id_of(&a), node_id_of(&b));
/// ```
pub fn node_id_of<T>(rc: &Rc<RefCell<T>>) -> NodeId {
    NodeId(Rc::as_ptr(rc) as *const () as usize)
}

/// Collect all reachable nodes from `root`, detecting shared pointers.
///
/// Returns a map from [`NodeId`] to the corresponding `Rc<RefCell<T>>`.
///
/// # Examples
///
/// ```
/// use reify_graph::collect_nodes;
/// use std::cell::RefCell;
/// use std::rc::Rc;
///
/// #[derive(Clone)]
/// struct Node { children: Vec<Rc<RefCell<Node>>> }
///
/// let leaf = Rc::new(RefCell::new(Node { children: vec![] }));
/// let root = Rc::new(RefCell::new(Node { children: vec![leaf.clone()] }));
///
/// let nodes = collect_nodes(&root, &|n: &Node| n.children.clone());
/// assert_eq!(nodes.len(), 2);
/// ```
pub fn collect_nodes<T, F>(root: &Rc<RefCell<T>>, children: &F) -> HashMap<NodeId, Rc<RefCell<T>>>
where
    F: Fn(&T) -> Vec<Rc<RefCell<T>>>,
{
    let mut visited = HashMap::new();
    collect_nodes_inner(root, children, &mut visited);
    visited
}

fn collect_nodes_inner<T, F>(
    node: &Rc<RefCell<T>>,
    children: &F,
    visited: &mut HashMap<NodeId, Rc<RefCell<T>>>,
) where
    F: Fn(&T) -> Vec<Rc<RefCell<T>>>,
{
    let id = node_id_of(node);
    if visited.contains_key(&id) {
        return;
    }
    visited.insert(id, node.clone());

    let kids = children(&node.borrow());
    for kid in &kids {
        collect_nodes_inner(kid, children, visited);
    }
}

/// Reify an `Rc<RefCell<T>>` graph into a [`ReifiedGraph`].
///
/// The `children` closure extracts child pointers from a node's data.
/// The resulting graph contains cloned node data and edges derived
/// from pointer identity.
///
/// # Examples
///
/// ```
/// use reify_graph::reify_graph;
/// use std::cell::RefCell;
/// use std::rc::Rc;
///
/// #[derive(Clone, Debug)]
/// struct Node {
///     value: i32,
///     children: Vec<Rc<RefCell<Node>>>,
/// }
///
/// let shared = Rc::new(RefCell::new(Node { value: 2, children: vec![] }));
/// let root = Rc::new(RefCell::new(Node {
///     value: 0,
///     children: vec![
///         Rc::new(RefCell::new(Node { value: 1, children: vec![shared.clone()] })),
///         shared.clone(),
///     ],
/// }));
///
/// let graph = reify_graph(root, |n| n.children.clone());
/// // 3 unique nodes: root(0), child(1), shared(2)
/// assert_eq!(graph.nodes.len(), 3);
/// ```
pub fn reify_graph<T, F>(root: Rc<RefCell<T>>, children: F) -> ReifiedGraph<T>
where
    F: Fn(&T) -> Vec<Rc<RefCell<T>>>,
    T: Clone,
{
    let all_nodes = collect_nodes(&root, &children);
    let root_id = node_id_of(&root);

    let mut nodes = Vec::with_capacity(all_nodes.len());
    let mut edges = Vec::new();

    for (id, rc) in &all_nodes {
        let borrowed = rc.borrow();
        nodes.push((*id, borrowed.clone()));

        for kid in children(&borrowed) {
            let kid_id = node_id_of(&kid);
            edges.push((*id, kid_id));
        }
    }

    ReifiedGraph {
        nodes,
        edges,
        root: root_id,
    }
}

/// Reconstruct an `Rc<RefCell<T>>` graph from a [`ReifiedGraph`].
///
/// The `set_children` closure wires up child pointers on a node given
/// its reconstructed children.
///
/// # Examples
///
/// ```
/// use reify_graph::{reify_graph, reflect_graph};
/// use std::cell::RefCell;
/// use std::rc::Rc;
///
/// #[derive(Clone, Debug)]
/// struct Node {
///     value: i32,
///     children: Vec<Rc<RefCell<Node>>>,
/// }
///
/// let leaf = Rc::new(RefCell::new(Node { value: 1, children: vec![] }));
/// let root = Rc::new(RefCell::new(Node { value: 0, children: vec![leaf] }));
///
/// let graph = reify_graph(root, |n| n.children.clone());
/// let rebuilt = reflect_graph(graph, |n, kids| n.children = kids);
///
/// assert_eq!(rebuilt.borrow().value, 0);
/// assert_eq!(rebuilt.borrow().children[0].borrow().value, 1);
/// ```
pub fn reflect_graph<T, F>(graph: ReifiedGraph<T>, set_children: F) -> Rc<RefCell<T>>
where
    F: Fn(&mut T, Vec<Rc<RefCell<T>>>),
    T: Clone,
{
    // Build Rc<RefCell<T>> for each node (without children wired up yet)
    let mut rc_map: HashMap<NodeId, Rc<RefCell<T>>> = HashMap::new();
    for (id, data) in &graph.nodes {
        rc_map.insert(*id, Rc::new(RefCell::new(data.clone())));
    }

    // Build adjacency: parent -> [child_ids] preserving edge order
    let mut adj: HashMap<NodeId, Vec<NodeId>> = HashMap::new();
    for (from, to) in &graph.edges {
        adj.entry(*from).or_default().push(*to);
    }

    // Wire up children
    for (id, rc) in &rc_map {
        if let Some(child_ids) = adj.get(id) {
            let children: Vec<Rc<RefCell<T>>> =
                child_ids.iter().map(|cid| rc_map[cid].clone()).collect();
            set_children(&mut rc.borrow_mut(), children);
        }
    }

    rc_map[&graph.root].clone()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Clone, Debug, PartialEq)]
    struct Node {
        value: i32,
        children: Vec<Rc<RefCell<Node>>>,
    }

    fn children_of(n: &Node) -> Vec<Rc<RefCell<Node>>> {
        n.children.clone()
    }

    fn set_children_of(n: &mut Node, kids: Vec<Rc<RefCell<Node>>>) {
        n.children = kids;
    }

    #[test]
    fn node_id_identity() {
        let a = Rc::new(RefCell::new(Node {
            value: 1,
            children: vec![],
        }));
        let b = a.clone();
        assert_eq!(node_id_of(&a), node_id_of(&b));

        let c = Rc::new(RefCell::new(Node {
            value: 1,
            children: vec![],
        }));
        assert_ne!(node_id_of(&a), node_id_of(&c));
    }

    #[test]
    fn collect_nodes_simple_linked_list() {
        let c = Rc::new(RefCell::new(Node {
            value: 2,
            children: vec![],
        }));
        let b = Rc::new(RefCell::new(Node {
            value: 1,
            children: vec![c.clone()],
        }));
        let a = Rc::new(RefCell::new(Node {
            value: 0,
            children: vec![b.clone()],
        }));

        let nodes = collect_nodes(&a, &children_of);
        assert_eq!(nodes.len(), 3);
    }

    #[test]
    fn collect_nodes_with_cycle() {
        let a = Rc::new(RefCell::new(Node {
            value: 0,
            children: vec![],
        }));
        let b = Rc::new(RefCell::new(Node {
            value: 1,
            children: vec![a.clone()],
        }));
        // Create cycle: a -> b -> a
        a.borrow_mut().children.push(b.clone());

        let nodes = collect_nodes(&a, &children_of);
        assert_eq!(nodes.len(), 2);
    }

    #[test]
    fn reify_dag_with_shared_children() {
        // DAG: root -> {a, b, shared}, a -> {shared}, b -> {shared}
        let shared = Rc::new(RefCell::new(Node {
            value: 99,
            children: vec![],
        }));
        let a = Rc::new(RefCell::new(Node {
            value: 1,
            children: vec![shared.clone()],
        }));
        let b = Rc::new(RefCell::new(Node {
            value: 2,
            children: vec![shared.clone()],
        }));
        let root = Rc::new(RefCell::new(Node {
            value: 0,
            children: vec![a.clone(), b.clone(), shared.clone()],
        }));

        let graph = reify_graph(root, children_of);

        // 4 unique nodes
        assert_eq!(graph.nodes.len(), 4);
        // root->a, root->b, root->shared, a->shared, b->shared = 5 edges
        assert_eq!(graph.edges.len(), 5);
    }

    #[test]
    fn reify_five_node_dag() {
        // n0 -> {n1, n2}, n1 -> {n3, n4}, n2 -> {n3}, n3 -> {n4}
        let n4 = Rc::new(RefCell::new(Node {
            value: 4,
            children: vec![],
        }));
        let n3 = Rc::new(RefCell::new(Node {
            value: 3,
            children: vec![n4.clone()],
        }));
        let n2 = Rc::new(RefCell::new(Node {
            value: 2,
            children: vec![n3.clone()],
        }));
        let n1 = Rc::new(RefCell::new(Node {
            value: 1,
            children: vec![n3.clone(), n4.clone()],
        }));
        let n0 = Rc::new(RefCell::new(Node {
            value: 0,
            children: vec![n1.clone(), n2.clone()],
        }));

        let graph = reify_graph(n0, children_of);
        assert_eq!(graph.nodes.len(), 5);
        // n0->n1, n0->n2, n1->n3, n1->n4, n2->n3, n3->n4 = 6 edges
        assert_eq!(graph.edges.len(), 6);
    }

    #[test]
    fn round_trip_reify_reflect() {
        let leaf1 = Rc::new(RefCell::new(Node {
            value: 10,
            children: vec![],
        }));
        let leaf2 = Rc::new(RefCell::new(Node {
            value: 20,
            children: vec![],
        }));
        let root = Rc::new(RefCell::new(Node {
            value: 0,
            children: vec![leaf1, leaf2],
        }));

        let graph = reify_graph(root.clone(), children_of);
        let rebuilt = reflect_graph(graph, set_children_of);

        assert_eq!(rebuilt.borrow().value, 0);
        assert_eq!(rebuilt.borrow().children.len(), 2);

        let mut child_values: Vec<i32> = rebuilt
            .borrow()
            .children
            .iter()
            .map(|c| c.borrow().value)
            .collect();
        child_values.sort();
        assert_eq!(child_values, vec![10, 20]);
    }

    #[test]
    fn round_trip_preserves_sharing() {
        // shared node should be the same Rc in the reconstruction
        let shared = Rc::new(RefCell::new(Node {
            value: 42,
            children: vec![],
        }));
        let root = Rc::new(RefCell::new(Node {
            value: 0,
            children: vec![shared.clone(), shared.clone()],
        }));

        let graph = reify_graph(root, children_of);
        let rebuilt = reflect_graph(graph, set_children_of);

        let children = &rebuilt.borrow().children;
        assert_eq!(children.len(), 2);
        // Both children should point to the same Rc
        assert_eq!(
            Rc::as_ptr(&children[0]) as usize,
            Rc::as_ptr(&children[1]) as usize
        );
    }
}