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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
pub trait ByteVisitor {
    fn visit_bytes(&mut self, bytes: impl AsRef<[u8]>);

    fn visit_nested(&mut self, nested: impl VisitBytes) {
        nested.visit(self)
    }
}

impl<'a, BV: ?Sized + ByteVisitor> ByteVisitor for &'a mut BV {
    fn visit_bytes(&mut self, bytes: impl AsRef<[u8]>) {
        (self as &mut BV).visit_bytes(bytes)
    }
}

pub trait VisitBytes {
    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV);
}

impl<'a, VB: ?Sized + VisitBytes> VisitBytes for &'a VB {
    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV) {
        (self as &VB).visit(visitor)
    }
}

impl VisitBytes for u8 {
    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV) {
        visitor.visit_bytes([*self]);
    }
}

impl<'a> VisitBytes for &'a [u8] {
    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV) {
        visitor.visit_bytes(self);
    }
}

impl<'a> VisitBytes for &'a str {
    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV) {
        visitor.visit_bytes(self.as_bytes());
    }
}

impl VisitBytes for () {
    fn visit<BV: ?Sized + ByteVisitor>(&self, _visitor: &mut BV) {}
}

impl<T1> VisitBytes for (T1,)
where
    T1: VisitBytes,
{
    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV) {
        self.0.visit(visitor);
    }
}

impl<T1, T2> VisitBytes for (T1, T2)
where
    T1: VisitBytes,
    T2: VisitBytes,
{
    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV) {
        self.0.visit(visitor);
        self.1.visit(visitor);
    }
}

impl<T1, T2, T3> VisitBytes for (T1, T2, T3)
where
    T1: VisitBytes,
    T2: VisitBytes,
    T3: VisitBytes,
{
    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV) {
        self.0.visit(visitor);
        self.1.visit(visitor);
        self.2.visit(visitor);
    }
}

impl<T1, T2, T3, T4> VisitBytes for (T1, T2, T3, T4)
where
    T1: VisitBytes,
    T2: VisitBytes,
    T3: VisitBytes,
    T4: VisitBytes,
{
    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV) {
        self.0.visit(visitor);
        self.1.visit(visitor);
        self.2.visit(visitor);
        self.3.visit(visitor);
    }
}

impl<T1, T2, T3, T4, T5> VisitBytes for (T1, T2, T3, T4, T5)
where
    T1: VisitBytes,
    T2: VisitBytes,
    T3: VisitBytes,
    T4: VisitBytes,
    T5: VisitBytes,
{
    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV) {
        self.0.visit(visitor);
        self.1.visit(visitor);
        self.2.visit(visitor);
        self.3.visit(visitor);
        self.4.visit(visitor);
    }
}

impl VisitBytes for [u8; 32] {
    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV) {
        visitor.visit_bytes(self.as_slice())
    }
}