Skip to main content

Crate reify_graph

Crate reify_graph 

Source
Expand description

§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);

Modules§

arc
Arc-pointer graph reification, parallel to the Rc<RefCell<T>> API.

Structs§

NodeId
A stable node identifier derived from pointer identity.
ReifiedGraph
An adjacency-list representation of a pointer graph.

Functions§

collect_nodes
Collect all reachable nodes from root, detecting shared pointers.
node_id_of
Extract a NodeId from an Rc<RefCell<T>> using pointer identity.
reflect_graph
Reconstruct an Rc<RefCell<T>> graph from a ReifiedGraph.
reify_graph
Reify an Rc<RefCell<T>> graph into a ReifiedGraph.