reify-graph 0.1.0

Safe Rc/Arc graph reification and reconstruction
Documentation

reify-graph

Safe library to convert Rc<RefCell<T>>-based pointer graphs into node-indexed adjacency representations and back.

This enables serialization, inspection, and transformation of cyclic and DAG-structured data that would otherwise be difficult to work with.

Examples

use reify_graph::{reify_graph, reflect_graph};
use std::cell::RefCell;
use std::rc::Rc;

// A simple tree node
#[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()],
}));

// Reify the graph
let graph = reify_graph(root.clone(), |n| n.children.clone());
assert_eq!(graph.nodes.len(), 2);
assert_eq!(graph.edges.len(), 1);

// Reconstruct the graph
let reconstructed = reflect_graph(graph, |n, kids| n.children = kids);
assert_eq!(reconstructed.borrow().value, 0);
assert_eq!(reconstructed.borrow().children.len(), 1);
assert_eq!(reconstructed.borrow().children[0].borrow().value, 1);