#![allow(dead_code)]
use std::vec::Vec;
use borrow::partial as p;
use borrow::traits::*;
type NodeId = usize;
type EdgeId = usize;
#[derive(Debug)]
struct Node {
outputs: Vec<EdgeId>,
inputs: Vec<EdgeId>,
}
#[derive(Debug)]
struct Edge {
from: Option<NodeId>,
to: Option<NodeId>,
}
#[derive(Debug)]
struct Group {
nodes: Vec<NodeId>,
}
#[derive(Debug, borrow::Partial)]
#[module(crate)]
struct Graph {
nodes: Vec<Node>,
edges: Vec<Edge>,
groups: Vec<Group>,
}
fn detach_node(graph: p!(&<mut edges> Graph), node: &mut Node) {
for edge_id in std::mem::take(&mut node.outputs) {
graph.edges[edge_id].from = None;
}
for edge_id in std::mem::take(&mut node.inputs) {
graph.edges[edge_id].to = None;
}
}
fn detach_all_nodes(graph: p!(&<mut *> Graph)) {
let (nodes, mut graph2) = graph.borrow_nodes_mut();
for node in nodes {
detach_node(p!(&mut graph2), node);
}
}
#[test]
fn test() {
let mut graph = Graph {
nodes: vec![
Node { outputs: vec![0], inputs: vec![2] }, Node { outputs: vec![1], inputs: vec![0] }, Node { outputs: vec![2], inputs: vec![1] }, ],
edges: vec![
Edge { from: Some(0), to: Some(1) }, Edge { from: Some(1), to: Some(2) }, Edge { from: Some(2), to: Some(0) }, ],
groups: vec![]
};
detach_all_nodes(p!(&mut graph));
for node in &graph.nodes {
assert!(node.outputs.is_empty());
assert!(node.inputs.is_empty());
}
for edge in &graph.edges {
assert!(edge.from.is_none());
assert!(edge.to.is_none());
}
}