warg_crypto/
visit_bytes.rs

1pub trait ByteVisitor {
2    fn visit_bytes(&mut self, bytes: impl AsRef<[u8]>);
3
4    fn visit_nested(&mut self, nested: impl VisitBytes) {
5        nested.visit(self)
6    }
7}
8
9impl<'a, BV: ?Sized + ByteVisitor> ByteVisitor for &'a mut BV {
10    fn visit_bytes(&mut self, bytes: impl AsRef<[u8]>) {
11        (self as &mut BV).visit_bytes(bytes)
12    }
13}
14
15pub trait VisitBytes {
16    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV);
17}
18
19impl<'a, VB: ?Sized + VisitBytes> VisitBytes for &'a VB {
20    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV) {
21        (self as &VB).visit(visitor)
22    }
23}
24
25impl VisitBytes for u8 {
26    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV) {
27        visitor.visit_bytes([*self]);
28    }
29}
30
31impl<'a> VisitBytes for &'a [u8] {
32    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV) {
33        visitor.visit_bytes(self);
34    }
35}
36
37impl<'a> VisitBytes for &'a str {
38    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV) {
39        visitor.visit_bytes(self.as_bytes());
40    }
41}
42
43impl VisitBytes for () {
44    fn visit<BV: ?Sized + ByteVisitor>(&self, _visitor: &mut BV) {}
45}
46
47impl<T1> VisitBytes for (T1,)
48where
49    T1: VisitBytes,
50{
51    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV) {
52        self.0.visit(visitor);
53    }
54}
55
56impl<T1, T2> VisitBytes for (T1, T2)
57where
58    T1: VisitBytes,
59    T2: VisitBytes,
60{
61    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV) {
62        self.0.visit(visitor);
63        self.1.visit(visitor);
64    }
65}
66
67impl<T1, T2, T3> VisitBytes for (T1, T2, T3)
68where
69    T1: VisitBytes,
70    T2: VisitBytes,
71    T3: VisitBytes,
72{
73    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV) {
74        self.0.visit(visitor);
75        self.1.visit(visitor);
76        self.2.visit(visitor);
77    }
78}
79
80impl<T1, T2, T3, T4> VisitBytes for (T1, T2, T3, T4)
81where
82    T1: VisitBytes,
83    T2: VisitBytes,
84    T3: VisitBytes,
85    T4: VisitBytes,
86{
87    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV) {
88        self.0.visit(visitor);
89        self.1.visit(visitor);
90        self.2.visit(visitor);
91        self.3.visit(visitor);
92    }
93}
94
95impl<T1, T2, T3, T4, T5> VisitBytes for (T1, T2, T3, T4, T5)
96where
97    T1: VisitBytes,
98    T2: VisitBytes,
99    T3: VisitBytes,
100    T4: VisitBytes,
101    T5: VisitBytes,
102{
103    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV) {
104        self.0.visit(visitor);
105        self.1.visit(visitor);
106        self.2.visit(visitor);
107        self.3.visit(visitor);
108        self.4.visit(visitor);
109    }
110}
111
112impl VisitBytes for [u8; 32] {
113    fn visit<BV: ?Sized + ByteVisitor>(&self, visitor: &mut BV) {
114        visitor.visit_bytes(self.as_slice())
115    }
116}