1 use crate::reflect::repeated::drain_iter::ReflectRepeatedDrainIter; 2 use crate::reflect::repeated::iter::ReflectRepeatedIter; 3 use crate::reflect::repeated::ReflectRepeated; 4 use crate::reflect::EnumDescriptor; 5 use crate::reflect::MessageDescriptor; 6 use crate::reflect::MessageRef; 7 use crate::reflect::ReflectRepeatedMut; 8 use crate::reflect::ReflectValueBox; 9 use crate::reflect::ReflectValueRef; 10 use crate::reflect::RuntimeType; 11 use crate::MessageDyn; 12 13 /// Container of repeated values for dynamic messages. 14 /// 15 /// It is logically similar to `Vec<ReflectValueBox>`, but: 16 /// * more efficient 17 /// * asserts all the elements are of the same type, the type which is specified at construction 18 #[derive(Debug, Clone)] 19 pub(crate) enum DynamicRepeated { 20 U32(Vec<u32>), 21 U64(Vec<u64>), 22 I32(Vec<i32>), 23 I64(Vec<i64>), 24 F32(Vec<f32>), 25 F64(Vec<f64>), 26 Bool(Vec<bool>), 27 String(Vec<String>), 28 Bytes(Vec<Vec<u8>>), 29 Enum(EnumDescriptor, Vec<i32>), 30 Message(MessageDescriptor, Vec<Box<dyn MessageDyn>>), 31 } 32 33 impl ReflectRepeated for DynamicRepeated { reflect_iter(&self) -> ReflectRepeatedIter34 fn reflect_iter(&self) -> ReflectRepeatedIter { 35 match self { 36 DynamicRepeated::U32(v) => ReflectRepeatedIter::new_slice(&v), 37 DynamicRepeated::U64(v) => ReflectRepeatedIter::new_slice(&v), 38 DynamicRepeated::I32(v) => ReflectRepeatedIter::new_slice(&v), 39 DynamicRepeated::I64(v) => ReflectRepeatedIter::new_slice(&v), 40 DynamicRepeated::F32(v) => ReflectRepeatedIter::new_slice(&v), 41 DynamicRepeated::F64(v) => ReflectRepeatedIter::new_slice(&v), 42 DynamicRepeated::Bool(v) => ReflectRepeatedIter::new_slice(&v), 43 DynamicRepeated::String(v) => ReflectRepeatedIter::new_slice(&v), 44 DynamicRepeated::Bytes(v) => ReflectRepeatedIter::new_slice(&v), 45 DynamicRepeated::Enum(descriptor, v) => ReflectRepeatedIter::new( 46 v.iter() 47 .map(|v| ReflectValueRef::Enum(descriptor.clone(), *v)), 48 ), 49 DynamicRepeated::Message(_descriptor, v) => ReflectRepeatedIter::new( 50 v.iter() 51 .map(|v| ReflectValueRef::Message(MessageRef::new(&**v))), 52 ), 53 } 54 } 55 reflect_drain_iter(&mut self) -> ReflectRepeatedDrainIter56 fn reflect_drain_iter(&mut self) -> ReflectRepeatedDrainIter { 57 match self { 58 DynamicRepeated::U32(v) => ReflectRepeatedDrainIter::new_vec(v), 59 DynamicRepeated::U64(v) => ReflectRepeatedDrainIter::new_vec(v), 60 DynamicRepeated::I32(v) => ReflectRepeatedDrainIter::new_vec(v), 61 DynamicRepeated::I64(v) => ReflectRepeatedDrainIter::new_vec(v), 62 DynamicRepeated::F32(v) => ReflectRepeatedDrainIter::new_vec(v), 63 DynamicRepeated::F64(v) => ReflectRepeatedDrainIter::new_vec(v), 64 DynamicRepeated::Bool(v) => ReflectRepeatedDrainIter::new_vec(v), 65 DynamicRepeated::String(v) => ReflectRepeatedDrainIter::new_vec(v), 66 DynamicRepeated::Bytes(v) => ReflectRepeatedDrainIter::new_vec(v), 67 DynamicRepeated::Enum(descriptor, v) => ReflectRepeatedDrainIter::new( 68 v.drain(..) 69 .map(|v| ReflectValueBox::Enum(descriptor.clone(), v)), 70 ), 71 DynamicRepeated::Message(_descriptor, v) => { 72 ReflectRepeatedDrainIter::new(v.drain(..).map(|v| ReflectValueBox::Message(v))) 73 } 74 } 75 } 76 len(&self) -> usize77 fn len(&self) -> usize { 78 match self { 79 DynamicRepeated::U32(v) => v.len(), 80 DynamicRepeated::U64(v) => v.len(), 81 DynamicRepeated::I32(v) => v.len(), 82 DynamicRepeated::I64(v) => v.len(), 83 DynamicRepeated::F32(v) => v.len(), 84 DynamicRepeated::F64(v) => v.len(), 85 DynamicRepeated::Bool(v) => v.len(), 86 DynamicRepeated::String(v) => v.len(), 87 DynamicRepeated::Bytes(v) => v.len(), 88 DynamicRepeated::Enum(.., v) => v.len(), 89 DynamicRepeated::Message(.., v) => v.len(), 90 } 91 } 92 get(&self, index: usize) -> ReflectValueRef93 fn get(&self, index: usize) -> ReflectValueRef { 94 match self { 95 DynamicRepeated::U32(v) => ReflectValueRef::U32(v[index]), 96 DynamicRepeated::U64(v) => ReflectValueRef::U64(v[index]), 97 DynamicRepeated::I32(v) => ReflectValueRef::I32(v[index]), 98 DynamicRepeated::I64(v) => ReflectValueRef::I64(v[index]), 99 DynamicRepeated::F32(v) => ReflectValueRef::F32(v[index]), 100 DynamicRepeated::F64(v) => ReflectValueRef::F64(v[index]), 101 DynamicRepeated::Bool(v) => ReflectValueRef::Bool(v[index]), 102 DynamicRepeated::String(v) => ReflectValueRef::String(&v[index]), 103 DynamicRepeated::Bytes(v) => ReflectValueRef::Bytes(&v[index]), 104 DynamicRepeated::Enum(descriptor, v) => { 105 ReflectValueRef::Enum(descriptor.clone(), v[index]) 106 } 107 DynamicRepeated::Message(_descriptor, v) => { 108 ReflectValueRef::Message(MessageRef::from(&*v[index])) 109 } 110 } 111 } 112 set(&mut self, index: usize, value: ReflectValueBox)113 fn set(&mut self, index: usize, value: ReflectValueBox) { 114 match self { 115 DynamicRepeated::U32(v) => v.set(index, value), 116 DynamicRepeated::U64(v) => v.set(index, value), 117 DynamicRepeated::I32(v) => v.set(index, value), 118 DynamicRepeated::I64(v) => v.set(index, value), 119 DynamicRepeated::F32(v) => v.set(index, value), 120 DynamicRepeated::F64(v) => v.set(index, value), 121 DynamicRepeated::Bool(v) => v.set(index, value), 122 DynamicRepeated::String(v) => v.set(index, value), 123 DynamicRepeated::Bytes(v) => v.set(index, value), 124 DynamicRepeated::Enum(descriptor, vs) => match value { 125 ReflectValueBox::Enum(value_description, v) => { 126 assert_eq!(*descriptor, value_description); 127 vs[index] = v; 128 } 129 _ => panic!("Expected enum value"), 130 }, 131 DynamicRepeated::Message(descriptor, vs) => match value { 132 ReflectValueBox::Message(message) => { 133 assert_eq!(*descriptor, message.descriptor_dyn()); 134 vs[index] = message; 135 } 136 _ => panic!("Expected message value"), 137 }, 138 } 139 } 140 push(&mut self, value: ReflectValueBox)141 fn push(&mut self, value: ReflectValueBox) { 142 match self { 143 DynamicRepeated::U32(vs) => ReflectRepeated::push(vs, value), 144 DynamicRepeated::U64(vs) => ReflectRepeated::push(vs, value), 145 DynamicRepeated::I32(vs) => ReflectRepeated::push(vs, value), 146 DynamicRepeated::I64(vs) => ReflectRepeated::push(vs, value), 147 DynamicRepeated::F32(vs) => ReflectRepeated::push(vs, value), 148 DynamicRepeated::F64(vs) => ReflectRepeated::push(vs, value), 149 DynamicRepeated::Bool(vs) => ReflectRepeated::push(vs, value), 150 DynamicRepeated::String(vs) => ReflectRepeated::push(vs, value), 151 DynamicRepeated::Bytes(vs) => ReflectRepeated::push(vs, value), 152 DynamicRepeated::Enum(descriptor, vs) => match value { 153 ReflectValueBox::Enum(value_description, v) => { 154 assert_eq!(*descriptor, value_description); 155 vs.push(v); 156 } 157 _ => panic!("Expected enum value"), 158 }, 159 DynamicRepeated::Message(descriptor, vs) => match value { 160 ReflectValueBox::Message(message) => { 161 assert_eq!(*descriptor, message.descriptor_dyn()); 162 vs.push(message); 163 } 164 _ => panic!("Expected message value"), 165 }, 166 } 167 } 168 reflect_extend(&mut self, values: ReflectRepeatedMut)169 fn reflect_extend(&mut self, values: ReflectRepeatedMut) { 170 match self { 171 DynamicRepeated::U32(vs) => vs.extend(values.repeated.data_u32()), 172 DynamicRepeated::U64(vs) => vs.extend(values.repeated.data_u64()), 173 DynamicRepeated::I32(vs) => vs.extend(values.repeated.data_i32()), 174 DynamicRepeated::I64(vs) => vs.extend(values.repeated.data_i64()), 175 DynamicRepeated::F32(vs) => vs.extend(values.repeated.data_f32()), 176 DynamicRepeated::F64(vs) => vs.extend(values.repeated.data_f64()), 177 DynamicRepeated::Bool(vs) => vs.extend(values.repeated.data_bool()), 178 _ => { 179 // Default less efficient implementation. 180 for value in values.repeated.reflect_drain_iter() { 181 self.push(value); 182 } 183 } 184 } 185 } 186 clear(&mut self)187 fn clear(&mut self) { 188 match self { 189 DynamicRepeated::U32(vs) => vs.clear(), 190 DynamicRepeated::U64(vs) => vs.clear(), 191 DynamicRepeated::I32(vs) => vs.clear(), 192 DynamicRepeated::I64(vs) => vs.clear(), 193 DynamicRepeated::F32(vs) => vs.clear(), 194 DynamicRepeated::F64(vs) => vs.clear(), 195 DynamicRepeated::Bool(vs) => vs.clear(), 196 DynamicRepeated::String(vs) => vs.clear(), 197 DynamicRepeated::Bytes(vs) => vs.clear(), 198 DynamicRepeated::Enum(_descriptor, vs) => vs.clear(), 199 DynamicRepeated::Message(_descriptor, vs) => vs.clear(), 200 } 201 } 202 element_type(&self) -> RuntimeType203 fn element_type(&self) -> RuntimeType { 204 match self { 205 DynamicRepeated::U32(..) => RuntimeType::U32, 206 DynamicRepeated::U64(..) => RuntimeType::U64, 207 DynamicRepeated::I32(..) => RuntimeType::I32, 208 DynamicRepeated::I64(..) => RuntimeType::I64, 209 DynamicRepeated::F32(..) => RuntimeType::F32, 210 DynamicRepeated::F64(..) => RuntimeType::F64, 211 DynamicRepeated::Bool(..) => RuntimeType::Bool, 212 DynamicRepeated::String(..) => RuntimeType::String, 213 DynamicRepeated::Bytes(..) => RuntimeType::VecU8, 214 DynamicRepeated::Enum(descriptor, _vs) => RuntimeType::Enum(descriptor.clone()), 215 DynamicRepeated::Message(descriptor, _vs) => RuntimeType::Message(descriptor.clone()), 216 } 217 } 218 data_enum_values(&self) -> &[i32]219 fn data_enum_values(&self) -> &[i32] { 220 match self { 221 DynamicRepeated::Enum(_descriptor, vs) => &vs, 222 _ => panic!("Expected enum value"), 223 } 224 } 225 data_bool(&self) -> &[bool]226 fn data_bool(&self) -> &[bool] { 227 match self { 228 DynamicRepeated::Bool(vs) => &vs, 229 _ => panic!("Expected bool value"), 230 } 231 } 232 data_u32(&self) -> &[u32]233 fn data_u32(&self) -> &[u32] { 234 match self { 235 DynamicRepeated::U32(vs) => &vs, 236 _ => panic!("Expected u32 value"), 237 } 238 } 239 data_u64(&self) -> &[u64]240 fn data_u64(&self) -> &[u64] { 241 match self { 242 DynamicRepeated::U64(vs) => &vs, 243 _ => panic!("Expected u64 value"), 244 } 245 } 246 data_i32(&self) -> &[i32]247 fn data_i32(&self) -> &[i32] { 248 match self { 249 DynamicRepeated::I32(vs) => &vs, 250 _ => panic!("Expected i32 value"), 251 } 252 } 253 data_i64(&self) -> &[i64]254 fn data_i64(&self) -> &[i64] { 255 match self { 256 DynamicRepeated::I64(vs) => &vs, 257 _ => panic!("Expected i64 value"), 258 } 259 } 260 data_f32(&self) -> &[f32]261 fn data_f32(&self) -> &[f32] { 262 match self { 263 DynamicRepeated::F32(vs) => &vs, 264 _ => panic!("Expected f32 value"), 265 } 266 } 267 data_f64(&self) -> &[f64]268 fn data_f64(&self) -> &[f64] { 269 match self { 270 DynamicRepeated::F64(vs) => &vs, 271 _ => panic!("Expected f64 value"), 272 } 273 } 274 } 275 276 impl DynamicRepeated { new(elem: RuntimeType) -> DynamicRepeated277 pub fn new(elem: RuntimeType) -> DynamicRepeated { 278 match elem { 279 RuntimeType::U32 => DynamicRepeated::U32(Vec::new()), 280 RuntimeType::U64 => DynamicRepeated::U64(Vec::new()), 281 RuntimeType::I32 => DynamicRepeated::I32(Vec::new()), 282 RuntimeType::I64 => DynamicRepeated::I64(Vec::new()), 283 RuntimeType::F32 => DynamicRepeated::F32(Vec::new()), 284 RuntimeType::F64 => DynamicRepeated::F64(Vec::new()), 285 RuntimeType::Bool => DynamicRepeated::Bool(Vec::new()), 286 RuntimeType::String => DynamicRepeated::String(Vec::new()), 287 RuntimeType::VecU8 => DynamicRepeated::Bytes(Vec::new()), 288 RuntimeType::Enum(enum_descriptor) => { 289 DynamicRepeated::Enum(enum_descriptor, Vec::new()) 290 } 291 RuntimeType::Message(message_descriptor) => { 292 DynamicRepeated::Message(message_descriptor, Vec::new()) 293 } 294 } 295 } 296 } 297