Skip to main content

reflect_graph

Function reflect_graph 

Source
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,
Expand description

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