• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use std::slice;
2 
3 use super::value::ProtobufValue;
4 use super::value::ReflectValueRef;
5 
6 use crate::repeated::RepeatedField;
7 
8 pub trait ReflectRepeated: 'static {
reflect_iter(&self) -> ReflectRepeatedIter9     fn reflect_iter(&self) -> ReflectRepeatedIter;
len(&self) -> usize10     fn len(&self) -> usize;
get(&self, index: usize) -> &dyn ProtobufValue11     fn get(&self, index: usize) -> &dyn ProtobufValue;
12 }
13 
14 impl<V: ProtobufValue + 'static> ReflectRepeated for Vec<V> {
reflect_iter<'a>(&'a self) -> ReflectRepeatedIter<'a>15     fn reflect_iter<'a>(&'a self) -> ReflectRepeatedIter<'a> {
16         ReflectRepeatedIter {
17             imp: Box::new(ReflectRepeatedIterImplSlice::<'a, V> { iter: self.iter() }),
18         }
19     }
20 
len(&self) -> usize21     fn len(&self) -> usize {
22         Vec::len(self)
23     }
24 
get(&self, index: usize) -> &dyn ProtobufValue25     fn get(&self, index: usize) -> &dyn ProtobufValue {
26         &self[index]
27     }
28 }
29 
30 // useless
31 impl<V: ProtobufValue + 'static> ReflectRepeated for [V] {
reflect_iter<'a>(&'a self) -> ReflectRepeatedIter<'a>32     fn reflect_iter<'a>(&'a self) -> ReflectRepeatedIter<'a> {
33         ReflectRepeatedIter {
34             imp: Box::new(ReflectRepeatedIterImplSlice::<'a, V> { iter: self.iter() }),
35         }
36     }
37 
len(&self) -> usize38     fn len(&self) -> usize {
39         <[_]>::len(self)
40     }
41 
get(&self, index: usize) -> &dyn ProtobufValue42     fn get(&self, index: usize) -> &dyn ProtobufValue {
43         &self[index]
44     }
45 }
46 
47 impl<V: ProtobufValue + 'static> ReflectRepeated for RepeatedField<V> {
reflect_iter<'a>(&'a self) -> ReflectRepeatedIter<'a>48     fn reflect_iter<'a>(&'a self) -> ReflectRepeatedIter<'a> {
49         ReflectRepeatedIter {
50             imp: Box::new(ReflectRepeatedIterImplSlice::<'a, V> { iter: self.iter() }),
51         }
52     }
53 
len(&self) -> usize54     fn len(&self) -> usize {
55         RepeatedField::len(self)
56     }
57 
get(&self, index: usize) -> &dyn ProtobufValue58     fn get(&self, index: usize) -> &dyn ProtobufValue {
59         &self[index]
60     }
61 }
62 
63 trait ReflectRepeatedIterTrait<'a> {
next(&mut self) -> Option<&'a dyn ProtobufValue>64     fn next(&mut self) -> Option<&'a dyn ProtobufValue>;
65 }
66 
67 struct ReflectRepeatedIterImplSlice<'a, V: ProtobufValue + 'static> {
68     iter: slice::Iter<'a, V>,
69 }
70 
71 impl<'a, V: ProtobufValue + 'static> ReflectRepeatedIterTrait<'a>
72     for ReflectRepeatedIterImplSlice<'a, V>
73 {
next(&mut self) -> Option<&'a dyn ProtobufValue>74     fn next(&mut self) -> Option<&'a dyn ProtobufValue> {
75         self.iter.next().map(|v| v as &dyn ProtobufValue)
76     }
77 }
78 
79 pub struct ReflectRepeatedIter<'a> {
80     imp: Box<dyn ReflectRepeatedIterTrait<'a> + 'a>,
81 }
82 
83 impl<'a> Iterator for ReflectRepeatedIter<'a> {
84     type Item = &'a dyn ProtobufValue;
85 
next(&mut self) -> Option<Self::Item>86     fn next(&mut self) -> Option<Self::Item> {
87         self.imp.next()
88     }
89 }
90 
91 impl<'a> IntoIterator for &'a dyn ReflectRepeated {
92     type IntoIter = ReflectRepeatedIter<'a>;
93     type Item = &'a dyn ProtobufValue;
94 
into_iter(self) -> Self::IntoIter95     fn into_iter(self) -> Self::IntoIter {
96         self.reflect_iter()
97     }
98 }
99 
100 pub trait ReflectRepeatedEnum<'a> {
len(&self) -> usize101     fn len(&self) -> usize;
102 
get(&self, index: usize) -> ReflectValueRef<'a>103     fn get(&self, index: usize) -> ReflectValueRef<'a>;
104 }
105 
106 pub trait ReflectRepeatedMessage<'a> {
len(&self) -> usize107     fn len(&self) -> usize;
108 
get(&self, index: usize) -> ReflectValueRef<'a>109     fn get(&self, index: usize) -> ReflectValueRef<'a>;
110 }
111 
112 pub enum ReflectRepeatedRef<'a> {
113     Generic(&'a dyn ReflectRepeated),
114     U32(&'a [u32]),
115     U64(&'a [u64]),
116     I32(&'a [i32]),
117     I64(&'a [i64]),
118     F32(&'a [f32]),
119     F64(&'a [f64]),
120     Bool(&'a [bool]),
121     String(&'a [String]),
122     Bytes(&'a [Vec<u8>]),
123     Enum(Box<dyn ReflectRepeatedEnum<'a> + 'a>),
124     Message(Box<dyn ReflectRepeatedMessage<'a> + 'a>),
125 }
126 
127 impl<'a> ReflectRepeatedRef<'a> {
len(&self) -> usize128     fn len(&self) -> usize {
129         match *self {
130             ReflectRepeatedRef::Generic(ref r) => r.len(),
131             ReflectRepeatedRef::U32(ref r) => r.len(),
132             ReflectRepeatedRef::U64(ref r) => r.len(),
133             ReflectRepeatedRef::I32(ref r) => r.len(),
134             ReflectRepeatedRef::I64(ref r) => r.len(),
135             ReflectRepeatedRef::F32(ref r) => r.len(),
136             ReflectRepeatedRef::F64(ref r) => r.len(),
137             ReflectRepeatedRef::Bool(ref r) => r.len(),
138             ReflectRepeatedRef::String(ref r) => r.len(),
139             ReflectRepeatedRef::Bytes(ref r) => r.len(),
140             ReflectRepeatedRef::Enum(ref r) => r.len(),
141             ReflectRepeatedRef::Message(ref r) => r.len(),
142         }
143     }
144 
get(&self, index: usize) -> ReflectValueRef<'a>145     fn get(&self, index: usize) -> ReflectValueRef<'a> {
146         match *self {
147             ReflectRepeatedRef::Generic(ref r) => r.get(index).as_ref(),
148             ReflectRepeatedRef::U32(ref r) => ReflectValueRef::U32(r[index]),
149             ReflectRepeatedRef::U64(ref r) => ReflectValueRef::U64(r[index]),
150             ReflectRepeatedRef::I32(ref r) => ReflectValueRef::I32(r[index]),
151             ReflectRepeatedRef::I64(ref r) => ReflectValueRef::I64(r[index]),
152             ReflectRepeatedRef::F32(ref r) => ReflectValueRef::F32(r[index]),
153             ReflectRepeatedRef::F64(ref r) => ReflectValueRef::F64(r[index]),
154             ReflectRepeatedRef::Bool(ref r) => ReflectValueRef::Bool(r[index]),
155             ReflectRepeatedRef::String(ref r) => ReflectValueRef::String(&r[index]),
156             ReflectRepeatedRef::Bytes(ref r) => ReflectValueRef::Bytes(&r[index]),
157             ReflectRepeatedRef::Enum(ref r) => r.get(index),
158             ReflectRepeatedRef::Message(ref r) => r.get(index),
159         }
160     }
161 }
162 
163 pub struct ReflectRepeatedRefIter<'a> {
164     repeated: &'a ReflectRepeatedRef<'a>,
165     pos: usize,
166 }
167 
168 impl<'a> Iterator for ReflectRepeatedRefIter<'a> {
169     type Item = ReflectValueRef<'a>;
170 
next(&mut self) -> Option<Self::Item>171     fn next(&mut self) -> Option<Self::Item> {
172         if self.pos < self.repeated.len() {
173             let pos = self.pos;
174             self.pos += 1;
175             Some(self.repeated.get(pos))
176         } else {
177             None
178         }
179     }
180 }
181 
182 impl<'a> IntoIterator for &'a ReflectRepeatedRef<'a> {
183     type IntoIter = ReflectRepeatedRefIter<'a>;
184     type Item = ReflectValueRef<'a>;
185 
into_iter(self) -> Self::IntoIter186     fn into_iter(self) -> Self::IntoIter {
187         ReflectRepeatedRefIter {
188             repeated: self,
189             pos: 0,
190         }
191     }
192 }
193