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