update_node

Macro update_node 

Source
macro_rules! update_node {
    ($transaction: expr, $var: path, $idx: expr, $node: ident, $func: block) => { ... };
    ($transaction: expr, $var: path, $idx: expr, | $node: ident | $func: block) => { ... };
    ($transaction: expr, $var: path, $idx: expr, move | $node: ident | $func: block) => { ... };
}
Expand description

Use the update method of the transaction, assume the node is $var variant of the NodeEnum. Panics if the enum does not match.

§Overloads:

  • Legacy: update_node!(graph, NodeEnum::Variant, idx, x, { ... })
  • Normal closure: update_node!(graph, NodeEnum::Variant, idx, |x| { ... })
  • Move closure: update_node!(graph, NodeEnum::Variant, idx, move |x| { ... })

§Example

use ttgraph::*;

#[derive(TypedNode)]
struct NodeA{
  a: usize
}

node_enum!{
  enum MyNodeEnum{
    A(NodeA)
  }
}

let ctx = Context::new();
let mut graph = Graph::<MyNodeEnum>::new(&ctx);
let mut trans = Transaction::new(&ctx);

let id = trans.insert(MyNodeEnum::A(NodeA{ a: 1 }));
graph.commit(trans);

trans = Transaction::new(&ctx);
// It is similar to this closure |x: NodeA| { NodeA{ a: x.a + 1 } }
update_node!(trans, MyNodeEnum::A, id, |x| {
  NodeA {
    a: x.a + 1,
  }
});
graph.commit(trans);

let a = get_node!(graph, MyNodeEnum::A, id);
assert!(a.is_some());
assert_eq!(a.unwrap().a, 2);