1 pub(crate) mod drain_iter;
2 pub(crate) mod iter;
3 mod transmute;
4 mod vec_downcast;
5 
6 use std::any::type_name;
7 use std::fmt;
8 
9 use crate::reflect::dynamic::repeated::DynamicRepeated;
10 use crate::reflect::reflect_eq::ReflectEq;
11 use crate::reflect::reflect_eq::ReflectEqMode;
12 use crate::reflect::repeated::drain_iter::ReflectRepeatedDrainIter;
13 use crate::reflect::repeated::iter::ReflectRepeatedIter;
14 use crate::reflect::repeated::transmute::transmute_ref_if_eq;
15 use crate::reflect::repeated::vec_downcast::VecMutVariant;
16 use crate::reflect::runtime_types::RuntimeTypeTrait;
17 use crate::reflect::value::value_ref::ReflectValueRef;
18 use crate::reflect::ProtobufValue;
19 use crate::reflect::ReflectValueBox;
20 use crate::reflect::RuntimeType;
21 
22 pub(crate) trait ReflectRepeated: Sync + 'static + fmt::Debug {
reflect_iter(&self) -> ReflectRepeatedIter23     fn reflect_iter(&self) -> ReflectRepeatedIter;
reflect_drain_iter(&mut self) -> ReflectRepeatedDrainIter24     fn reflect_drain_iter(&mut self) -> ReflectRepeatedDrainIter;
len(&self) -> usize25     fn len(&self) -> usize;
get(&self, index: usize) -> ReflectValueRef26     fn get(&self, index: usize) -> ReflectValueRef;
27     /// Set element at index.
28     ///
29     /// # Panics
30     ///
31     /// * if index is out of bounds
32     /// * if the element type does not match the collection element type
set(&mut self, index: usize, value: ReflectValueBox)33     fn set(&mut self, index: usize, value: ReflectValueBox);
34     /// Append element.
35     ///
36     /// # Panics
37     ///
38     /// * if the element type does not match the collection element type
push(&mut self, value: ReflectValueBox)39     fn push(&mut self, value: ReflectValueBox);
40 
reflect_extend(&mut self, values: ReflectRepeatedMut)41     fn reflect_extend(&mut self, values: ReflectRepeatedMut);
42 
clear(&mut self)43     fn clear(&mut self);
44     /// Get the collection element type.
element_type(&self) -> RuntimeType45     fn element_type(&self) -> RuntimeType;
46 
47     /// Get array data for enum elements.
48     ///
49     /// # Panics
50     ///
51     /// * if the element type is not an enum
data_enum_values(&self) -> &[i32]52     fn data_enum_values(&self) -> &[i32];
53 
54     /// Get array data if the element type is bool.
data_bool(&self) -> &[bool]55     fn data_bool(&self) -> &[bool];
56     /// Get array data if the element type is i32.
data_i32(&self) -> &[i32]57     fn data_i32(&self) -> &[i32];
58     /// Get array data if the element type is u32.
data_u32(&self) -> &[u32]59     fn data_u32(&self) -> &[u32];
60     /// Get array data if the element type is i64.
data_i64(&self) -> &[i64]61     fn data_i64(&self) -> &[i64];
62     /// Get array data if the element type is u64.
data_u64(&self) -> &[u64]63     fn data_u64(&self) -> &[u64];
64     /// Get array data if the element type is f32.
data_f32(&self) -> &[f32]65     fn data_f32(&self) -> &[f32];
66     /// Get array data if the element type is f64.
data_f64(&self) -> &[f64]67     fn data_f64(&self) -> &[f64];
68 }
69 
data_impl<V: ProtobufValue, X: ProtobufValue>(v: &Vec<V>) -> &[X]70 fn data_impl<V: ProtobufValue, X: ProtobufValue>(v: &Vec<V>) -> &[X] {
71     match transmute_ref_if_eq::<_, Vec<X>>(v) {
72         Ok(v) => v.as_slice(),
73         Err(_) => panic!("not {}", type_name::<X>()),
74     }
75 }
76 
77 impl<V: ProtobufValue> ReflectRepeated for Vec<V> {
reflect_iter<'a>(&'a self) -> ReflectRepeatedIter<'a>78     fn reflect_iter<'a>(&'a self) -> ReflectRepeatedIter<'a> {
79         ReflectRepeatedIter::new_slice(self.as_slice())
80     }
81 
reflect_drain_iter<'a>(&'a mut self) -> ReflectRepeatedDrainIter<'a>82     fn reflect_drain_iter<'a>(&'a mut self) -> ReflectRepeatedDrainIter<'a> {
83         ReflectRepeatedDrainIter::new_vec(self)
84     }
85 
len(&self) -> usize86     fn len(&self) -> usize {
87         Vec::len(self)
88     }
89 
get(&self, index: usize) -> ReflectValueRef90     fn get(&self, index: usize) -> ReflectValueRef {
91         V::RuntimeType::as_ref(&self[index])
92     }
93 
set(&mut self, index: usize, value: ReflectValueBox)94     fn set(&mut self, index: usize, value: ReflectValueBox) {
95         let value = value.downcast().expect("wrong type");
96         self[index] = value;
97     }
98 
push(&mut self, value: ReflectValueBox)99     fn push(&mut self, value: ReflectValueBox) {
100         let value = value.downcast().expect("wrong type");
101         self.push(value)
102     }
103 
reflect_extend(&mut self, values: ReflectRepeatedMut)104     fn reflect_extend(&mut self, values: ReflectRepeatedMut) {
105         match VecMutVariant::downcast(self) {
106             Some(VecMutVariant::U32(v)) => v.extend(values.repeated.data_u32()),
107             Some(VecMutVariant::U64(v)) => v.extend(values.repeated.data_u64()),
108             Some(VecMutVariant::I32(v)) => v.extend(values.repeated.data_i32()),
109             Some(VecMutVariant::I64(v)) => v.extend(values.repeated.data_i64()),
110             Some(VecMutVariant::F32(v)) => v.extend(values.repeated.data_f32()),
111             Some(VecMutVariant::F64(v)) => v.extend(values.repeated.data_f64()),
112             Some(VecMutVariant::Bool(v)) => v.extend(values.repeated.data_bool()),
113             None => {
114                 for value in values.repeated.reflect_drain_iter() {
115                     // Less efficient.
116                     ReflectRepeated::push(self, value);
117                 }
118             }
119         }
120     }
121 
clear(&mut self)122     fn clear(&mut self) {
123         self.clear()
124     }
125 
element_type(&self) -> RuntimeType126     fn element_type(&self) -> RuntimeType {
127         V::RuntimeType::runtime_type_box()
128     }
129 
data_enum_values(&self) -> &[i32]130     fn data_enum_values(&self) -> &[i32] {
131         V::RuntimeType::cast_to_enum_values(&self)
132     }
133 
data_bool(&self) -> &[bool]134     fn data_bool(&self) -> &[bool] {
135         data_impl(self)
136     }
137 
data_i32(&self) -> &[i32]138     fn data_i32(&self) -> &[i32] {
139         data_impl(self)
140     }
141 
data_u32(&self) -> &[u32]142     fn data_u32(&self) -> &[u32] {
143         data_impl(self)
144     }
145 
data_i64(&self) -> &[i64]146     fn data_i64(&self) -> &[i64] {
147         data_impl(self)
148     }
149 
data_u64(&self) -> &[u64]150     fn data_u64(&self) -> &[u64] {
151         data_impl(self)
152     }
153 
data_f32(&self) -> &[f32]154     fn data_f32(&self) -> &[f32] {
155         data_impl(self)
156     }
157 
data_f64(&self) -> &[f64]158     fn data_f64(&self) -> &[f64] {
159         data_impl(self)
160     }
161 }
162 
163 impl<'a> IntoIterator for &'a dyn ReflectRepeated {
164     type Item = ReflectValueRef<'a>;
165     type IntoIter = ReflectRepeatedIter<'a>;
166 
into_iter(self) -> Self::IntoIter167     fn into_iter(self) -> Self::IntoIter {
168         self.reflect_iter()
169     }
170 }
171 
172 #[derive(Clone)]
173 enum ReflectRepeatedRefImpl<'a> {
174     Generated(&'a dyn ReflectRepeated),
175     DynamicEmpty(DynamicRepeated),
176 }
177 
178 impl<'a> fmt::Debug for ReflectRepeatedRefImpl<'a> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result179     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
180         match self {
181             ReflectRepeatedRefImpl::Generated(r) => fmt::Debug::fmt(r, f),
182             ReflectRepeatedRefImpl::DynamicEmpty(r) => fmt::Debug::fmt(r, f),
183         }
184     }
185 }
186 
187 /// Dynamic reference to repeated field
188 #[derive(Clone)]
189 pub struct ReflectRepeatedRef<'a> {
190     imp: ReflectRepeatedRefImpl<'a>,
191 }
192 
193 /// Dynamic mutable reference to repeated field
194 pub struct ReflectRepeatedMut<'a> {
195     pub(crate) repeated: &'a mut dyn ReflectRepeated,
196 }
197 
198 impl<'a> ReflectRepeatedRef<'a> {
new(repeated: &'a dyn ReflectRepeated) -> ReflectRepeatedRef<'a>199     pub(crate) fn new(repeated: &'a dyn ReflectRepeated) -> ReflectRepeatedRef<'a> {
200         ReflectRepeatedRef {
201             imp: ReflectRepeatedRefImpl::Generated(repeated),
202         }
203     }
204 
new_empty(elem: RuntimeType) -> ReflectRepeatedRef<'static>205     pub(crate) fn new_empty(elem: RuntimeType) -> ReflectRepeatedRef<'static> {
206         ReflectRepeatedRef {
207             imp: ReflectRepeatedRefImpl::DynamicEmpty(DynamicRepeated::new(elem)),
208         }
209     }
210 
211     /// Number of elements in repeated field
len(&self) -> usize212     pub fn len(&self) -> usize {
213         match &self.imp {
214             ReflectRepeatedRefImpl::Generated(g) => g.len(),
215             ReflectRepeatedRefImpl::DynamicEmpty(d) => d.len(),
216         }
217     }
218 
219     /// Repeated field is empty
is_empty(&self) -> bool220     pub fn is_empty(&self) -> bool {
221         self.len() == 0
222     }
223 
224     /// Get item by index
225     // TODO: replace with index
get(&self, index: usize) -> ReflectValueRef<'a>226     pub fn get(&self, index: usize) -> ReflectValueRef<'a> {
227         match &self.imp {
228             ReflectRepeatedRefImpl::Generated(r) => r.get(index),
229             ReflectRepeatedRefImpl::DynamicEmpty(..) => panic!("empty"),
230         }
231     }
232 
233     /// Runtime type of element
element_type(&self) -> RuntimeType234     pub fn element_type(&self) -> RuntimeType {
235         match &self.imp {
236             ReflectRepeatedRefImpl::Generated(r) => r.element_type(),
237             ReflectRepeatedRefImpl::DynamicEmpty(r) => r.element_type(),
238         }
239     }
240 
data_enum_values(&self) -> &[i32]241     pub(crate) fn data_enum_values(&self) -> &[i32] {
242         match &self.imp {
243             ReflectRepeatedRefImpl::Generated(r) => r.data_enum_values(),
244             ReflectRepeatedRefImpl::DynamicEmpty(r) => r.data_enum_values(),
245         }
246     }
247 
data_bool(&self) -> &[bool]248     pub(crate) fn data_bool(&self) -> &[bool] {
249         match &self.imp {
250             ReflectRepeatedRefImpl::Generated(r) => r.data_bool(),
251             ReflectRepeatedRefImpl::DynamicEmpty(r) => r.data_bool(),
252         }
253     }
254 
data_u32(&self) -> &[u32]255     pub(crate) fn data_u32(&self) -> &[u32] {
256         match &self.imp {
257             ReflectRepeatedRefImpl::Generated(r) => r.data_u32(),
258             ReflectRepeatedRefImpl::DynamicEmpty(r) => r.data_u32(),
259         }
260     }
261 
data_i32(&self) -> &[i32]262     pub(crate) fn data_i32(&self) -> &[i32] {
263         match &self.imp {
264             ReflectRepeatedRefImpl::Generated(r) => r.data_i32(),
265             ReflectRepeatedRefImpl::DynamicEmpty(r) => r.data_i32(),
266         }
267     }
268 
data_u64(&self) -> &[u64]269     pub(crate) fn data_u64(&self) -> &[u64] {
270         match &self.imp {
271             ReflectRepeatedRefImpl::Generated(r) => r.data_u64(),
272             ReflectRepeatedRefImpl::DynamicEmpty(r) => r.data_u64(),
273         }
274     }
275 
data_i64(&self) -> &[i64]276     pub(crate) fn data_i64(&self) -> &[i64] {
277         match &self.imp {
278             ReflectRepeatedRefImpl::Generated(r) => r.data_i64(),
279             ReflectRepeatedRefImpl::DynamicEmpty(r) => r.data_i64(),
280         }
281     }
282 
data_f32(&self) -> &[f32]283     pub(crate) fn data_f32(&self) -> &[f32] {
284         match &self.imp {
285             ReflectRepeatedRefImpl::Generated(r) => r.data_f32(),
286             ReflectRepeatedRefImpl::DynamicEmpty(r) => r.data_f32(),
287         }
288     }
289 
data_f64(&self) -> &[f64]290     pub(crate) fn data_f64(&self) -> &[f64] {
291         match &self.imp {
292             ReflectRepeatedRefImpl::Generated(r) => r.data_f64(),
293             ReflectRepeatedRefImpl::DynamicEmpty(r) => r.data_f64(),
294         }
295     }
296 }
297 
298 impl<'a> ReflectEq for ReflectRepeatedRef<'a> {
reflect_eq(&self, that: &Self, mode: &ReflectEqMode) -> bool299     fn reflect_eq(&self, that: &Self, mode: &ReflectEqMode) -> bool {
300         let len = self.len();
301 
302         if len != that.len() {
303             return false;
304         }
305 
306         if self.element_type() != that.element_type() {
307             return false;
308         }
309 
310         for i in 0..len {
311             let a = self.get(i);
312             let b = that.get(i);
313             if !a.reflect_eq(&b, mode) {
314                 return false;
315             }
316         }
317 
318         true
319     }
320 }
321 
322 impl<'a> PartialEq for ReflectRepeatedRef<'a> {
eq(&self, other: &Self) -> bool323     fn eq(&self, other: &Self) -> bool {
324         self.reflect_eq(other, &ReflectEqMode::default())
325     }
326 }
327 
328 impl<'a> PartialEq<[ReflectValueBox]> for ReflectRepeatedRef<'a> {
eq(&self, other: &[ReflectValueBox]) -> bool329     fn eq(&self, other: &[ReflectValueBox]) -> bool {
330         if self.len() != other.len() {
331             return false;
332         }
333 
334         for i in 0..self.len() {
335             if self.get(i) != other[i] {
336                 return false;
337             }
338         }
339 
340         return true;
341     }
342 }
343 
344 impl<'a> PartialEq<ReflectRepeatedRef<'a>> for [ReflectValueBox] {
eq(&self, other: &ReflectRepeatedRef) -> bool345     fn eq(&self, other: &ReflectRepeatedRef) -> bool {
346         other == self
347     }
348 }
349 
350 impl<'a> PartialEq<Vec<ReflectValueBox>> for ReflectRepeatedRef<'a> {
eq(&self, other: &Vec<ReflectValueBox>) -> bool351     fn eq(&self, other: &Vec<ReflectValueBox>) -> bool {
352         self == other.as_slice()
353     }
354 }
355 
356 impl<'a> PartialEq<ReflectRepeatedRef<'a>> for Vec<ReflectValueBox> {
eq(&self, other: &ReflectRepeatedRef) -> bool357     fn eq(&self, other: &ReflectRepeatedRef) -> bool {
358         self.as_slice() == other
359     }
360 }
361 
362 impl<'a> ReflectRepeatedMut<'a> {
new(repeated: &'a mut dyn ReflectRepeated) -> ReflectRepeatedMut<'a>363     pub(crate) fn new(repeated: &'a mut dyn ReflectRepeated) -> ReflectRepeatedMut<'a> {
364         ReflectRepeatedMut { repeated }
365     }
366 
as_ref(&'a self) -> ReflectRepeatedRef<'a>367     fn as_ref(&'a self) -> ReflectRepeatedRef<'a> {
368         ReflectRepeatedRef::new(self.repeated)
369     }
370 
371     /// Number of elements in repeated field
len(&self) -> usize372     pub fn len(&self) -> usize {
373         self.repeated.len()
374     }
375 
376     /// Self-explanatory
is_empty(&self) -> bool377     pub fn is_empty(&self) -> bool {
378         self.len() == 0
379     }
380 
381     /// Get an item by index
382     ///
383     /// Note: return immutable reference.
get(&'a self, index: usize) -> ReflectValueRef<'a>384     pub fn get(&'a self, index: usize) -> ReflectValueRef<'a> {
385         self.repeated.get(index)
386     }
387 
388     /// Runtime type of element
element_type(&self) -> RuntimeType389     pub fn element_type(&self) -> RuntimeType {
390         self.repeated.element_type()
391     }
392 
393     /// Set a value at given index.
394     ///
395     /// # Panics
396     ///
397     /// If index if out of range or value type does not match container element type
set(&mut self, index: usize, value: ReflectValueBox)398     pub fn set(&mut self, index: usize, value: ReflectValueBox) {
399         self.repeated.set(index, value);
400     }
401 
402     /// Push an item to repeated field.
403     ///
404     /// # Panics
405     ///
406     /// If index if out of range or value type does not match container element type
push(&mut self, value: ReflectValueBox)407     pub fn push(&mut self, value: ReflectValueBox) {
408         self.repeated.push(value);
409     }
410 
extend(&mut self, values: ReflectRepeatedMut)411     pub(crate) fn extend(&mut self, values: ReflectRepeatedMut) {
412         self.repeated.reflect_extend(values);
413     }
414 
415     /// Self-explanatory
clear(&mut self)416     pub fn clear(&mut self) {
417         self.repeated.clear();
418     }
419 }
420 
421 /// Iterator over repeated field.
422 pub struct ReflectRepeatedRefIter<'a> {
423     repeated: ReflectRepeatedRef<'a>,
424     index: usize,
425 }
426 
427 impl<'a> Iterator for ReflectRepeatedRefIter<'a> {
428     type Item = ReflectValueRef<'a>;
429 
next(&mut self) -> Option<Self::Item>430     fn next(&mut self) -> Option<Self::Item> {
431         let index = self.index;
432         if index != self.repeated.len() {
433             let r = self.repeated.get(index);
434             self.index += 1;
435             Some(r)
436         } else {
437             None
438         }
439     }
440 }
441 
442 impl<'a> IntoIterator for &'a ReflectRepeatedRef<'a> {
443     type Item = ReflectValueRef<'a>;
444     type IntoIter = ReflectRepeatedRefIter<'a>;
445 
into_iter(self) -> Self::IntoIter446     fn into_iter(self) -> Self::IntoIter {
447         ReflectRepeatedRefIter {
448             repeated: self.clone(),
449             index: 0,
450         }
451     }
452 }
453 
454 impl<'a> IntoIterator for ReflectRepeatedRef<'a> {
455     type Item = ReflectValueRef<'a>;
456     type IntoIter = ReflectRepeatedRefIter<'a>;
457 
into_iter(self) -> Self::IntoIter458     fn into_iter(self) -> Self::IntoIter {
459         ReflectRepeatedRefIter {
460             repeated: self,
461             index: 0,
462         }
463     }
464 }
465 
466 impl<'a> IntoIterator for &'a ReflectRepeatedMut<'a> {
467     type Item = ReflectValueRef<'a>;
468     type IntoIter = ReflectRepeatedRefIter<'a>;
469 
into_iter(self) -> Self::IntoIter470     fn into_iter(self) -> Self::IntoIter {
471         self.as_ref().into_iter()
472     }
473 }
474 
475 impl<'a> fmt::Debug for ReflectRepeatedRef<'a> {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result476     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
477         fmt::Debug::fmt(&self.imp, f)
478     }
479 }
480 
481 impl<'a> fmt::Debug for ReflectRepeatedMut<'a> {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result482     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
483         fmt::Debug::fmt(self.repeated, f)
484     }
485 }
486 
487 impl<'a> PartialEq for ReflectRepeatedMut<'a> {
eq(&self, other: &Self) -> bool488     fn eq(&self, other: &Self) -> bool {
489         self.as_ref() == other.as_ref()
490     }
491 }
492 
493 impl<'a> PartialEq<ReflectRepeatedRef<'a>> for ReflectRepeatedMut<'a> {
eq(&self, other: &ReflectRepeatedRef) -> bool494     fn eq(&self, other: &ReflectRepeatedRef) -> bool {
495         PartialEq::eq(&self.as_ref(), other)
496     }
497 }
498 
499 impl<'a> PartialEq<[ReflectValueBox]> for ReflectRepeatedMut<'a> {
eq(&self, other: &[ReflectValueBox]) -> bool500     fn eq(&self, other: &[ReflectValueBox]) -> bool {
501         PartialEq::eq(&self.as_ref(), other)
502     }
503 }
504 
505 impl<'a> PartialEq<ReflectRepeatedMut<'a>> for [ReflectValueBox] {
eq(&self, other: &ReflectRepeatedMut) -> bool506     fn eq(&self, other: &ReflectRepeatedMut) -> bool {
507         PartialEq::eq(self, &other.as_ref())
508     }
509 }
510 
511 impl<'a> PartialEq<Vec<ReflectValueBox>> for ReflectRepeatedMut<'a> {
eq(&self, other: &Vec<ReflectValueBox>) -> bool512     fn eq(&self, other: &Vec<ReflectValueBox>) -> bool {
513         self == other.as_slice()
514     }
515 }
516 
517 impl<'a> PartialEq<ReflectRepeatedMut<'a>> for Vec<ReflectValueBox> {
eq(&self, other: &ReflectRepeatedMut) -> bool518     fn eq(&self, other: &ReflectRepeatedMut) -> bool {
519         self.as_slice() == other
520     }
521 }
522