1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use another_visitor::{VisitableMut, VisitorMut};
#[derive(VisitableMut)]
struct A {
b1: B,
b2: B,
}
#[derive(VisitableMut)]
struct B {
c1: C,
c2: C,
}
#[derive(VisitableMut)]
struct C {
#[visit(skip)]
i: i32,
}
#[derive(VisitorMut)]
#[visit(A, B, C)]
struct AVisitor {}
impl another_visitor::VisitorHelper for AVisitor {
type Output = String;
#[allow(unused_variables)]
fn aggregate(&mut self, a: Self::Output, b: Self::Output) -> Self::Output {
format!("{a}{b}")
}
}
impl AVisitor {
fn visit_a(&mut self, a: &mut A) -> <Self as another_visitor::VisitorHelper>::Output {
format!("(A {} {})", self.visit(&mut a.b1), self.visit(&mut a.b2))
}
fn visit_b(&mut self, b: &mut B) -> <Self as another_visitor::VisitorHelper>::Output {
if b.c1.i == 2 {
b.c2.i = 10;
}
self.visit_children(b)
}
fn visit_c(&mut self, c: &mut C) -> <Self as another_visitor::VisitorHelper>::Output {
format!("(C {})", c.i)
}
}
fn main() {
let mut dat = A {
b1: B { c1: C { i: 0 }, c2: C { i: 1 } },
b2: B { c1: C { i: 2 }, c2: C { i: 3 } },
};
let mut vis = AVisitor {};
println!("{}", vis.visit(&mut dat));
}