Chih-Hung Hsieh | cfc3a23 | 2020-06-10 20:13:05 -0700 | [diff] [blame] | 1 | //! Functions used by generated protobuf code. |
| 2 | //! Should not be used by programs written by hands. |
| 3 | |
| 4 | use std::collections::HashMap; |
| 5 | use std::default::Default; |
| 6 | use std::hash::Hash; |
| 7 | |
| 8 | #[cfg(feature = "bytes")] |
Haibo Huang | d32e6ee | 2020-08-12 13:52:04 -0700 | [diff] [blame^] | 9 | use crate::chars::Chars; |
Chih-Hung Hsieh | cfc3a23 | 2020-06-10 20:13:05 -0700 | [diff] [blame] | 10 | #[cfg(feature = "bytes")] |
Haibo Huang | d32e6ee | 2020-08-12 13:52:04 -0700 | [diff] [blame^] | 11 | use bytes::Bytes; |
Chih-Hung Hsieh | cfc3a23 | 2020-06-10 20:13:05 -0700 | [diff] [blame] | 12 | |
Haibo Huang | d32e6ee | 2020-08-12 13:52:04 -0700 | [diff] [blame^] | 13 | use crate::enums::ProtobufEnum; |
| 14 | use crate::error::ProtobufError; |
| 15 | use crate::error::ProtobufResult; |
| 16 | use crate::error::WireError; |
| 17 | use crate::message::*; |
| 18 | use crate::repeated::RepeatedField; |
| 19 | use crate::singular::SingularField; |
| 20 | use crate::singular::SingularPtrField; |
| 21 | use crate::stream::wire_format; |
| 22 | use crate::stream::wire_format::WireType; |
| 23 | use crate::stream::wire_format::WireTypeFixed32; |
| 24 | use crate::stream::wire_format::WireTypeFixed64; |
| 25 | use crate::stream::wire_format::WireTypeLengthDelimited; |
| 26 | use crate::stream::wire_format::WireTypeVarint; |
| 27 | use crate::stream::CodedInputStream; |
| 28 | use crate::stream::CodedOutputStream; |
| 29 | use crate::types::*; |
| 30 | use crate::zigzag::*; |
Chih-Hung Hsieh | cfc3a23 | 2020-06-10 20:13:05 -0700 | [diff] [blame] | 31 | |
Haibo Huang | 52aa785 | 2020-07-10 20:23:55 -0700 | [diff] [blame] | 32 | pub use crate::lazy_v2::LazyV2; |
Haibo Huang | d32e6ee | 2020-08-12 13:52:04 -0700 | [diff] [blame^] | 33 | use crate::unknown::UnknownFields; |
Chih-Hung Hsieh | cfc3a23 | 2020-06-10 20:13:05 -0700 | [diff] [blame] | 34 | |
| 35 | /// Given `u64` value compute varint encoded length. |
| 36 | pub fn compute_raw_varint64_size(value: u64) -> u32 { |
| 37 | if (value & (0xffffffffffffffffu64 << 7)) == 0 { |
| 38 | return 1; |
| 39 | } |
| 40 | if (value & (0xffffffffffffffffu64 << 14)) == 0 { |
| 41 | return 2; |
| 42 | } |
| 43 | if (value & (0xffffffffffffffffu64 << 21)) == 0 { |
| 44 | return 3; |
| 45 | } |
| 46 | if (value & (0xffffffffffffffffu64 << 28)) == 0 { |
| 47 | return 4; |
| 48 | } |
| 49 | if (value & (0xffffffffffffffffu64 << 35)) == 0 { |
| 50 | return 5; |
| 51 | } |
| 52 | if (value & (0xffffffffffffffffu64 << 42)) == 0 { |
| 53 | return 6; |
| 54 | } |
| 55 | if (value & (0xffffffffffffffffu64 << 49)) == 0 { |
| 56 | return 7; |
| 57 | } |
| 58 | if (value & (0xffffffffffffffffu64 << 56)) == 0 { |
| 59 | return 8; |
| 60 | } |
| 61 | if (value & (0xffffffffffffffffu64 << 63)) == 0 { |
| 62 | return 9; |
| 63 | } |
| 64 | 10 |
| 65 | } |
| 66 | |
| 67 | /// Given `u32` value compute varint encoded length. |
| 68 | pub fn compute_raw_varint32_size(value: u32) -> u32 { |
| 69 | compute_raw_varint64_size(value as u64) |
| 70 | } |
| 71 | |
| 72 | /// Helper trait implemented by integer types which could be encoded as varint. |
| 73 | pub trait ProtobufVarint { |
| 74 | /// Size of self when encoded as varint. |
| 75 | fn len_varint(&self) -> u32; |
| 76 | } |
| 77 | |
| 78 | /// Helper trait implemented by integer types which could be encoded as zigzag varint. |
| 79 | pub trait ProtobufVarintZigzag { |
| 80 | /// Size of self when encoded as zigzag varint. |
| 81 | fn len_varint_zigzag(&self) -> u32; |
| 82 | } |
| 83 | |
| 84 | impl ProtobufVarint for u64 { |
| 85 | fn len_varint(&self) -> u32 { |
| 86 | compute_raw_varint64_size(*self) |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | impl ProtobufVarint for u32 { |
| 91 | fn len_varint(&self) -> u32 { |
| 92 | (*self as u64).len_varint() |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | impl ProtobufVarint for i64 { |
| 97 | fn len_varint(&self) -> u32 { |
| 98 | // same as length of u64 |
| 99 | (*self as u64).len_varint() |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | impl ProtobufVarintZigzag for i64 { |
| 104 | fn len_varint_zigzag(&self) -> u32 { |
| 105 | compute_raw_varint64_size(encode_zig_zag_64(*self)) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | impl ProtobufVarint for i32 { |
| 110 | fn len_varint(&self) -> u32 { |
| 111 | // sign-extend and then compute |
| 112 | (*self as i64).len_varint() |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | impl ProtobufVarintZigzag for i32 { |
| 117 | fn len_varint_zigzag(&self) -> u32 { |
| 118 | compute_raw_varint32_size(encode_zig_zag_32(*self)) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | impl ProtobufVarint for bool { |
| 123 | fn len_varint(&self) -> u32 { |
| 124 | 1 |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /* Commented out due to https://github.com/mozilla/rust/issues/8075 |
| 129 | impl<E:ProtobufEnum> ProtobufVarint for E { |
| 130 | fn len_varint(&self) -> u32 { |
| 131 | self.value().len_varint() |
| 132 | } |
| 133 | } |
| 134 | */ |
| 135 | |
| 136 | /// Size of serialized repeated packed field, excluding length and tag. |
| 137 | pub fn vec_packed_varint_data_size<T: ProtobufVarint>(vec: &[T]) -> u32 { |
| 138 | vec.iter().map(|v| v.len_varint()).fold(0, |a, i| a + i) |
| 139 | } |
| 140 | |
| 141 | /// Size of serialized repeated packed field, excluding length and tag. |
| 142 | pub fn vec_packed_varint_zigzag_data_size<T: ProtobufVarintZigzag>(vec: &[T]) -> u32 { |
| 143 | vec.iter() |
| 144 | .map(|v| v.len_varint_zigzag()) |
| 145 | .fold(0, |a, i| a + i) |
| 146 | } |
| 147 | |
| 148 | /// Size of serialized repeated packed enum field, excluding length and tag. |
| 149 | pub fn vec_packed_enum_data_size<E: ProtobufEnum>(vec: &[E]) -> u32 { |
| 150 | vec.iter() |
| 151 | .map(|e| compute_raw_varint32_size(e.value() as u32)) |
| 152 | .fold(0, |a, i| a + i) |
| 153 | } |
| 154 | |
| 155 | /// Size of serialized data with length prefix and tag |
| 156 | pub fn vec_packed_varint_size<T: ProtobufVarint>(field_number: u32, vec: &[T]) -> u32 { |
| 157 | if vec.is_empty() { |
| 158 | 0 |
| 159 | } else { |
| 160 | let data_size = vec_packed_varint_data_size(vec); |
| 161 | tag_size(field_number) + data_size.len_varint() + data_size |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /// Size of serialized data with length prefix and tag |
| 166 | pub fn vec_packed_varint_zigzag_size<T: ProtobufVarintZigzag>(field_number: u32, vec: &[T]) -> u32 { |
| 167 | if vec.is_empty() { |
| 168 | 0 |
| 169 | } else { |
| 170 | let data_size = vec_packed_varint_zigzag_data_size(vec); |
| 171 | tag_size(field_number) + data_size.len_varint() + data_size |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /// Size of serialized data with length prefix and tag |
| 176 | pub fn vec_packed_enum_size<E: ProtobufEnum>(field_number: u32, vec: &[E]) -> u32 { |
| 177 | if vec.is_empty() { |
| 178 | 0 |
| 179 | } else { |
| 180 | let data_size = vec_packed_enum_data_size(vec); |
| 181 | tag_size(field_number) + data_size.len_varint() + data_size |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /// Compute tag size. Size of tag does not depend on wire type. |
| 186 | pub fn tag_size(field_number: u32) -> u32 { |
| 187 | wire_format::Tag::make(field_number, WireTypeFixed64) |
| 188 | .value() |
| 189 | .len_varint() |
| 190 | } |
| 191 | |
| 192 | fn value_size_no_tag<T: ProtobufVarint>(value: T, wt: WireType) -> u32 { |
| 193 | match wt { |
| 194 | WireTypeFixed64 => 8, |
| 195 | WireTypeFixed32 => 4, |
| 196 | WireTypeVarint => value.len_varint(), |
| 197 | _ => panic!(), |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /// Integer value size when encoded as specified wire type. |
| 202 | pub fn value_size<T: ProtobufVarint>(field_number: u32, value: T, wt: WireType) -> u32 { |
| 203 | tag_size(field_number) + value_size_no_tag(value, wt) |
| 204 | } |
| 205 | |
| 206 | /// Integer value size when encoded as specified wire type. |
| 207 | pub fn value_varint_zigzag_size_no_tag<T: ProtobufVarintZigzag>(value: T) -> u32 { |
| 208 | value.len_varint_zigzag() |
| 209 | } |
| 210 | |
| 211 | /// Length of value when encoding with zigzag encoding with tag |
| 212 | pub fn value_varint_zigzag_size<T: ProtobufVarintZigzag>(field_number: u32, value: T) -> u32 { |
| 213 | tag_size(field_number) + value_varint_zigzag_size_no_tag(value) |
| 214 | } |
| 215 | |
| 216 | fn enum_size_no_tag<E: ProtobufEnum>(value: E) -> u32 { |
| 217 | value.value().len_varint() |
| 218 | } |
| 219 | |
| 220 | /// Size of encoded enum field value. |
| 221 | pub fn enum_size<E: ProtobufEnum>(field_number: u32, value: E) -> u32 { |
| 222 | tag_size(field_number) + enum_size_no_tag(value) |
| 223 | } |
| 224 | |
| 225 | fn bytes_size_no_tag(bytes: &[u8]) -> u32 { |
| 226 | compute_raw_varint64_size(bytes.len() as u64) + bytes.len() as u32 |
| 227 | } |
| 228 | |
| 229 | /// Size of encoded bytes field. |
| 230 | pub fn bytes_size(field_number: u32, bytes: &[u8]) -> u32 { |
| 231 | tag_size(field_number) + bytes_size_no_tag(bytes) |
| 232 | } |
| 233 | |
| 234 | fn string_size_no_tag(s: &str) -> u32 { |
| 235 | bytes_size_no_tag(s.as_bytes()) |
| 236 | } |
| 237 | |
| 238 | /// Size of encoded string field. |
| 239 | pub fn string_size(field_number: u32, s: &str) -> u32 { |
| 240 | tag_size(field_number) + string_size_no_tag(s) |
| 241 | } |
| 242 | |
| 243 | /// Size of encoded unknown fields size. |
| 244 | pub fn unknown_fields_size(unknown_fields: &UnknownFields) -> u32 { |
| 245 | let mut r = 0; |
| 246 | for (number, values) in unknown_fields { |
| 247 | r += (tag_size(number) + 4) * values.fixed32.len() as u32; |
| 248 | r += (tag_size(number) + 8) * values.fixed64.len() as u32; |
| 249 | |
| 250 | r += tag_size(number) * values.varint.len() as u32; |
| 251 | for varint in &values.varint { |
| 252 | r += varint.len_varint(); |
| 253 | } |
| 254 | |
| 255 | r += tag_size(number) * values.length_delimited.len() as u32; |
| 256 | for bytes in &values.length_delimited { |
| 257 | r += bytes_size_no_tag(&bytes); |
| 258 | } |
| 259 | } |
| 260 | r |
| 261 | } |
| 262 | |
| 263 | /// Read repeated `int32` field into given vec. |
| 264 | pub fn read_repeated_int32_into( |
| 265 | wire_type: WireType, |
| 266 | is: &mut CodedInputStream, |
| 267 | target: &mut Vec<i32>, |
| 268 | ) -> ProtobufResult<()> { |
| 269 | match wire_type { |
| 270 | WireTypeLengthDelimited => is.read_repeated_packed_int32_into(target), |
| 271 | WireTypeVarint => { |
| 272 | target.push(is.read_int32()?); |
| 273 | Ok(()) |
| 274 | } |
| 275 | _ => Err(unexpected_wire_type(wire_type)), |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /// Read repeated `int64` field into given vec. |
| 280 | pub fn read_repeated_int64_into( |
| 281 | wire_type: WireType, |
| 282 | is: &mut CodedInputStream, |
| 283 | target: &mut Vec<i64>, |
| 284 | ) -> ProtobufResult<()> { |
| 285 | match wire_type { |
| 286 | WireTypeLengthDelimited => is.read_repeated_packed_int64_into(target), |
| 287 | WireTypeVarint => { |
| 288 | target.push(is.read_int64()?); |
| 289 | Ok(()) |
| 290 | } |
| 291 | _ => Err(unexpected_wire_type(wire_type)), |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /// Read repeated `uint32` field into given vec. |
| 296 | pub fn read_repeated_uint32_into( |
| 297 | wire_type: WireType, |
| 298 | is: &mut CodedInputStream, |
| 299 | target: &mut Vec<u32>, |
| 300 | ) -> ProtobufResult<()> { |
| 301 | match wire_type { |
| 302 | WireTypeLengthDelimited => is.read_repeated_packed_uint32_into(target), |
| 303 | WireTypeVarint => { |
| 304 | target.push(is.read_uint32()?); |
| 305 | Ok(()) |
| 306 | } |
| 307 | _ => Err(unexpected_wire_type(wire_type)), |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | /// Read repeated `uint64` field into given vec. |
| 312 | pub fn read_repeated_uint64_into( |
| 313 | wire_type: WireType, |
| 314 | is: &mut CodedInputStream, |
| 315 | target: &mut Vec<u64>, |
| 316 | ) -> ProtobufResult<()> { |
| 317 | match wire_type { |
| 318 | WireTypeLengthDelimited => is.read_repeated_packed_uint64_into(target), |
| 319 | WireTypeVarint => { |
| 320 | target.push(is.read_uint64()?); |
| 321 | Ok(()) |
| 322 | } |
| 323 | _ => Err(unexpected_wire_type(wire_type)), |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /// Read repeated `sint32` field into given vec. |
| 328 | pub fn read_repeated_sint32_into( |
| 329 | wire_type: WireType, |
| 330 | is: &mut CodedInputStream, |
| 331 | target: &mut Vec<i32>, |
| 332 | ) -> ProtobufResult<()> { |
| 333 | match wire_type { |
| 334 | WireTypeLengthDelimited => is.read_repeated_packed_sint32_into(target), |
| 335 | WireTypeVarint => { |
| 336 | target.push(is.read_sint32()?); |
| 337 | Ok(()) |
| 338 | } |
| 339 | _ => Err(unexpected_wire_type(wire_type)), |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | /// Read repeated `sint64` field into given vec. |
| 344 | pub fn read_repeated_sint64_into( |
| 345 | wire_type: WireType, |
| 346 | is: &mut CodedInputStream, |
| 347 | target: &mut Vec<i64>, |
| 348 | ) -> ProtobufResult<()> { |
| 349 | match wire_type { |
| 350 | WireTypeLengthDelimited => is.read_repeated_packed_sint64_into(target), |
| 351 | WireTypeVarint => { |
| 352 | target.push(is.read_sint64()?); |
| 353 | Ok(()) |
| 354 | } |
| 355 | _ => Err(unexpected_wire_type(wire_type)), |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | /// Read repeated `fixed32` field into given vec. |
| 360 | pub fn read_repeated_fixed32_into( |
| 361 | wire_type: WireType, |
| 362 | is: &mut CodedInputStream, |
| 363 | target: &mut Vec<u32>, |
| 364 | ) -> ProtobufResult<()> { |
| 365 | match wire_type { |
| 366 | WireTypeLengthDelimited => is.read_repeated_packed_fixed32_into(target), |
| 367 | WireTypeFixed32 => { |
| 368 | target.push(is.read_fixed32()?); |
| 369 | Ok(()) |
| 370 | } |
| 371 | _ => Err(unexpected_wire_type(wire_type)), |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | /// Read repeated `fixed64` field into given vec. |
| 376 | pub fn read_repeated_fixed64_into( |
| 377 | wire_type: WireType, |
| 378 | is: &mut CodedInputStream, |
| 379 | target: &mut Vec<u64>, |
| 380 | ) -> ProtobufResult<()> { |
| 381 | match wire_type { |
| 382 | WireTypeLengthDelimited => is.read_repeated_packed_fixed64_into(target), |
| 383 | WireTypeFixed64 => { |
| 384 | target.push(is.read_fixed64()?); |
| 385 | Ok(()) |
| 386 | } |
| 387 | _ => Err(unexpected_wire_type(wire_type)), |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | /// Read repeated `sfixed32` field into given vec. |
| 392 | pub fn read_repeated_sfixed32_into( |
| 393 | wire_type: WireType, |
| 394 | is: &mut CodedInputStream, |
| 395 | target: &mut Vec<i32>, |
| 396 | ) -> ProtobufResult<()> { |
| 397 | match wire_type { |
| 398 | WireTypeLengthDelimited => is.read_repeated_packed_sfixed32_into(target), |
| 399 | WireTypeFixed32 => { |
| 400 | target.push(is.read_sfixed32()?); |
| 401 | Ok(()) |
| 402 | } |
| 403 | _ => Err(unexpected_wire_type(wire_type)), |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | /// Read repeated `sfixed64` field into given vec. |
| 408 | pub fn read_repeated_sfixed64_into( |
| 409 | wire_type: WireType, |
| 410 | is: &mut CodedInputStream, |
| 411 | target: &mut Vec<i64>, |
| 412 | ) -> ProtobufResult<()> { |
| 413 | match wire_type { |
| 414 | WireTypeLengthDelimited => is.read_repeated_packed_sfixed64_into(target), |
| 415 | WireTypeFixed64 => { |
| 416 | target.push(is.read_sfixed64()?); |
| 417 | Ok(()) |
| 418 | } |
| 419 | _ => Err(unexpected_wire_type(wire_type)), |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | /// Read repeated `double` field into given vec. |
| 424 | pub fn read_repeated_double_into( |
| 425 | wire_type: WireType, |
| 426 | is: &mut CodedInputStream, |
| 427 | target: &mut Vec<f64>, |
| 428 | ) -> ProtobufResult<()> { |
| 429 | match wire_type { |
| 430 | WireTypeLengthDelimited => is.read_repeated_packed_double_into(target), |
| 431 | WireTypeFixed64 => { |
| 432 | target.push(is.read_double()?); |
| 433 | Ok(()) |
| 434 | } |
| 435 | _ => Err(unexpected_wire_type(wire_type)), |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | /// Read repeated `float` field into given vec. |
| 440 | pub fn read_repeated_float_into( |
| 441 | wire_type: WireType, |
| 442 | is: &mut CodedInputStream, |
| 443 | target: &mut Vec<f32>, |
| 444 | ) -> ProtobufResult<()> { |
| 445 | match wire_type { |
| 446 | WireTypeLengthDelimited => is.read_repeated_packed_float_into(target), |
| 447 | WireTypeFixed32 => { |
| 448 | target.push(is.read_float()?); |
| 449 | Ok(()) |
| 450 | } |
| 451 | _ => Err(unexpected_wire_type(wire_type)), |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | /// Read repeated `bool` field into given vec. |
| 456 | pub fn read_repeated_bool_into( |
| 457 | wire_type: WireType, |
| 458 | is: &mut CodedInputStream, |
| 459 | target: &mut Vec<bool>, |
| 460 | ) -> ProtobufResult<()> { |
| 461 | match wire_type { |
| 462 | WireTypeLengthDelimited => is.read_repeated_packed_bool_into(target), |
| 463 | WireTypeVarint => { |
| 464 | target.push(is.read_bool()?); |
| 465 | Ok(()) |
| 466 | } |
| 467 | _ => Err(unexpected_wire_type(wire_type)), |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | /// Read repeated `enum` field into given vec. |
| 472 | /// This function is no longer called from generated code, remove in 1.5. |
| 473 | pub fn read_repeated_enum_into<E: ProtobufEnum>( |
| 474 | wire_type: WireType, |
| 475 | is: &mut CodedInputStream, |
| 476 | target: &mut Vec<E>, |
| 477 | ) -> ProtobufResult<()> { |
| 478 | match wire_type { |
| 479 | WireTypeLengthDelimited => is.read_repeated_packed_enum_into(target), |
| 480 | WireTypeVarint => { |
| 481 | target.push(is.read_enum()?); |
| 482 | Ok(()) |
| 483 | } |
| 484 | _ => Err(unexpected_wire_type(wire_type)), |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | /// Helper function to read single enum value. |
| 489 | #[inline] |
| 490 | fn read_enum_with_unknown_fields_into<E: ProtobufEnum, C>( |
| 491 | is: &mut CodedInputStream, |
| 492 | target: C, |
| 493 | field_number: u32, |
| 494 | unknown_fields: &mut UnknownFields, |
| 495 | ) -> ProtobufResult<()> |
| 496 | where |
| 497 | C: FnOnce(E), |
| 498 | { |
| 499 | let i = is.read_int32()?; |
| 500 | match ProtobufEnum::from_i32(i) { |
| 501 | Some(e) => target(e), |
| 502 | None => unknown_fields.add_varint(field_number, i as i64 as u64), |
| 503 | } |
| 504 | Ok(()) |
| 505 | } |
| 506 | |
| 507 | fn read_repeated_packed_enum_with_unknown_fields_into<E: ProtobufEnum>( |
| 508 | is: &mut CodedInputStream, |
| 509 | target: &mut Vec<E>, |
| 510 | field_number: u32, |
| 511 | unknown_fields: &mut UnknownFields, |
| 512 | ) -> ProtobufResult<()> { |
| 513 | let len = is.read_raw_varint64()?; |
| 514 | let old_limit = is.push_limit(len)?; |
| 515 | while !is.eof()? { |
| 516 | read_enum_with_unknown_fields_into(is, |e| target.push(e), field_number, unknown_fields)?; |
| 517 | } |
| 518 | is.pop_limit(old_limit); |
| 519 | Ok(()) |
| 520 | } |
| 521 | |
| 522 | /// Read repeated `enum` field into given vec, |
| 523 | /// and when value is unknown store it in unknown fields |
| 524 | /// which matches proto2 spec. |
| 525 | /// |
| 526 | /// See explanation |
| 527 | /// [here](https://github.com/stepancheg/rust-protobuf/issues/233#issuecomment-375142710) |
| 528 | pub fn read_repeated_enum_with_unknown_fields_into<E: ProtobufEnum>( |
| 529 | wire_type: WireType, |
| 530 | is: &mut CodedInputStream, |
| 531 | target: &mut Vec<E>, |
| 532 | field_number: u32, |
| 533 | unknown_fields: &mut UnknownFields, |
| 534 | ) -> ProtobufResult<()> { |
| 535 | match wire_type { |
| 536 | WireTypeLengthDelimited => read_repeated_packed_enum_with_unknown_fields_into( |
| 537 | is, |
| 538 | target, |
| 539 | field_number, |
| 540 | unknown_fields, |
| 541 | ), |
| 542 | WireTypeVarint => { |
| 543 | read_enum_with_unknown_fields_into(is, |e| target.push(e), field_number, unknown_fields) |
| 544 | } |
| 545 | _ => Err(unexpected_wire_type(wire_type)), |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | /// Read repeated `enum` field into given vec, |
| 550 | /// and when value is unknown store it in unknown fields |
| 551 | /// which matches proto2 spec. |
| 552 | /// |
| 553 | /// See explanation |
| 554 | /// [here](https://github.com/stepancheg/rust-protobuf/issues/233#issuecomment-375142710) |
| 555 | pub fn read_proto3_enum_with_unknown_fields_into<E: ProtobufEnum>( |
| 556 | wire_type: WireType, |
| 557 | is: &mut CodedInputStream, |
| 558 | target: &mut E, |
| 559 | field_number: u32, |
| 560 | unknown_fields: &mut UnknownFields, |
| 561 | ) -> ProtobufResult<()> { |
| 562 | if wire_type != WireType::WireTypeVarint { |
| 563 | return Err(unexpected_wire_type(wire_type)); |
| 564 | } |
| 565 | |
| 566 | read_enum_with_unknown_fields_into(is, |e| *target = e, field_number, unknown_fields) |
| 567 | } |
| 568 | |
| 569 | /// Read repeated `enum` field into given vec, |
| 570 | /// and when value is unknown store it in unknown fields |
| 571 | /// which matches proto2 spec. |
| 572 | /// |
| 573 | /// See explanation |
| 574 | /// [here](https://github.com/stepancheg/rust-protobuf/issues/233#issuecomment-375142710) |
| 575 | pub fn read_proto2_enum_with_unknown_fields_into<E: ProtobufEnum>( |
| 576 | wire_type: WireType, |
| 577 | is: &mut CodedInputStream, |
| 578 | target: &mut Option<E>, |
| 579 | field_number: u32, |
| 580 | unknown_fields: &mut UnknownFields, |
| 581 | ) -> ProtobufResult<()> { |
| 582 | if wire_type != WireType::WireTypeVarint { |
| 583 | return Err(unexpected_wire_type(wire_type)); |
| 584 | } |
| 585 | |
| 586 | read_enum_with_unknown_fields_into(is, |e| *target = Some(e), field_number, unknown_fields) |
| 587 | } |
| 588 | |
| 589 | /// Read repeated `string` field into given vec. |
| 590 | pub fn read_repeated_string_into( |
| 591 | wire_type: WireType, |
| 592 | is: &mut CodedInputStream, |
| 593 | target: &mut RepeatedField<String>, |
| 594 | ) -> ProtobufResult<()> { |
| 595 | match wire_type { |
| 596 | WireTypeLengthDelimited => { |
| 597 | let tmp = target.push_default(); |
| 598 | is.read_string_into(tmp) |
| 599 | } |
| 600 | _ => Err(unexpected_wire_type(wire_type)), |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | /// Read repeated `Chars` field into given vec. |
| 605 | #[cfg(feature = "bytes")] |
| 606 | pub fn read_repeated_carllerche_string_into( |
| 607 | wire_type: WireType, |
| 608 | is: &mut CodedInputStream, |
| 609 | target: &mut Vec<Chars>, |
| 610 | ) -> ProtobufResult<()> { |
| 611 | match wire_type { |
| 612 | WireTypeLengthDelimited => { |
| 613 | target.push(is.read_carllerche_chars()?); |
| 614 | Ok(()) |
| 615 | } |
| 616 | _ => Err(unexpected_wire_type(wire_type)), |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | /// Read singular `string` field. |
| 621 | pub fn read_singular_string_into( |
| 622 | wire_type: WireType, |
| 623 | is: &mut CodedInputStream, |
| 624 | target: &mut SingularField<String>, |
| 625 | ) -> ProtobufResult<()> { |
| 626 | match wire_type { |
| 627 | WireTypeLengthDelimited => { |
| 628 | let tmp = target.set_default(); |
| 629 | is.read_string_into(tmp) |
| 630 | } |
| 631 | _ => Err(unexpected_wire_type(wire_type)), |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | /// Read singular `Chars` field. |
| 636 | #[cfg(feature = "bytes")] |
| 637 | pub fn read_singular_carllerche_string_into( |
| 638 | wire_type: WireType, |
| 639 | is: &mut CodedInputStream, |
| 640 | target: &mut Option<Chars>, |
| 641 | ) -> ProtobufResult<()> { |
| 642 | match wire_type { |
| 643 | WireTypeLengthDelimited => { |
| 644 | *target = Some(is.read_carllerche_chars()?); |
| 645 | Ok(()) |
| 646 | } |
| 647 | _ => Err(unexpected_wire_type(wire_type)), |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | /// Read singular `string` field for proto3. |
| 652 | pub fn read_singular_proto3_string_into( |
| 653 | wire_type: WireType, |
| 654 | is: &mut CodedInputStream, |
| 655 | target: &mut String, |
| 656 | ) -> ProtobufResult<()> { |
| 657 | match wire_type { |
| 658 | WireTypeLengthDelimited => is.read_string_into(target), |
| 659 | _ => Err(unexpected_wire_type(wire_type)), |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | /// Read singular `Chars` field for proto3. |
| 664 | #[cfg(feature = "bytes")] |
| 665 | pub fn read_singular_proto3_carllerche_string_into( |
| 666 | wire_type: WireType, |
| 667 | is: &mut CodedInputStream, |
| 668 | target: &mut Chars, |
| 669 | ) -> ProtobufResult<()> { |
| 670 | match wire_type { |
| 671 | WireTypeLengthDelimited => { |
| 672 | *target = is.read_carllerche_chars()?; |
| 673 | Ok(()) |
| 674 | } |
| 675 | _ => Err(unexpected_wire_type(wire_type)), |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | /// Read repeated `bytes` field into given vec. |
| 680 | pub fn read_repeated_bytes_into( |
| 681 | wire_type: WireType, |
| 682 | is: &mut CodedInputStream, |
| 683 | target: &mut RepeatedField<Vec<u8>>, |
| 684 | ) -> ProtobufResult<()> { |
| 685 | match wire_type { |
| 686 | WireTypeLengthDelimited => { |
| 687 | let tmp = target.push_default(); |
| 688 | is.read_bytes_into(tmp) |
| 689 | } |
| 690 | _ => Err(unexpected_wire_type(wire_type)), |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | /// Read repeated `Bytes` field into given vec. |
| 695 | #[cfg(feature = "bytes")] |
| 696 | pub fn read_repeated_carllerche_bytes_into( |
| 697 | wire_type: WireType, |
| 698 | is: &mut CodedInputStream, |
| 699 | target: &mut Vec<Bytes>, |
| 700 | ) -> ProtobufResult<()> { |
| 701 | match wire_type { |
| 702 | WireTypeLengthDelimited => { |
| 703 | target.push(is.read_carllerche_bytes()?); |
| 704 | Ok(()) |
| 705 | } |
| 706 | _ => Err(unexpected_wire_type(wire_type)), |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | /// Read singular `bytes` field. |
| 711 | pub fn read_singular_bytes_into( |
| 712 | wire_type: WireType, |
| 713 | is: &mut CodedInputStream, |
| 714 | target: &mut SingularField<Vec<u8>>, |
| 715 | ) -> ProtobufResult<()> { |
| 716 | match wire_type { |
| 717 | WireTypeLengthDelimited => { |
| 718 | let tmp = target.set_default(); |
| 719 | is.read_bytes_into(tmp) |
| 720 | } |
| 721 | _ => Err(unexpected_wire_type(wire_type)), |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | /// Read singular `Bytes` field. |
| 726 | #[cfg(feature = "bytes")] |
| 727 | pub fn read_singular_carllerche_bytes_into( |
| 728 | wire_type: WireType, |
| 729 | is: &mut CodedInputStream, |
| 730 | target: &mut Option<Bytes>, |
| 731 | ) -> ProtobufResult<()> { |
| 732 | match wire_type { |
| 733 | WireTypeLengthDelimited => { |
| 734 | *target = Some(is.read_carllerche_bytes()?); |
| 735 | Ok(()) |
| 736 | } |
| 737 | _ => Err(unexpected_wire_type(wire_type)), |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | /// Read singular `bytes` field for proto3. |
| 742 | pub fn read_singular_proto3_bytes_into( |
| 743 | wire_type: WireType, |
| 744 | is: &mut CodedInputStream, |
| 745 | target: &mut Vec<u8>, |
| 746 | ) -> ProtobufResult<()> { |
| 747 | match wire_type { |
| 748 | WireTypeLengthDelimited => is.read_bytes_into(target), |
| 749 | _ => Err(unexpected_wire_type(wire_type)), |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | /// Read singular `Bytes` field for proto3. |
| 754 | #[cfg(feature = "bytes")] |
| 755 | pub fn read_singular_proto3_carllerche_bytes_into( |
| 756 | wire_type: WireType, |
| 757 | is: &mut CodedInputStream, |
| 758 | target: &mut Bytes, |
| 759 | ) -> ProtobufResult<()> { |
| 760 | match wire_type { |
| 761 | WireTypeLengthDelimited => { |
| 762 | *target = is.read_carllerche_bytes()?; |
| 763 | Ok(()) |
| 764 | } |
| 765 | _ => Err(unexpected_wire_type(wire_type)), |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | /// Read repeated `message` field. |
| 770 | pub fn read_repeated_message_into<M: Message + Default>( |
| 771 | wire_type: WireType, |
| 772 | is: &mut CodedInputStream, |
| 773 | target: &mut RepeatedField<M>, |
| 774 | ) -> ProtobufResult<()> { |
| 775 | match wire_type { |
| 776 | WireTypeLengthDelimited => { |
| 777 | is.incr_recursion()?; |
| 778 | let tmp = target.push_default(); |
| 779 | let res = is.merge_message(tmp); |
| 780 | is.decr_recursion(); |
| 781 | res |
| 782 | } |
| 783 | _ => Err(unexpected_wire_type(wire_type)), |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | /// Read singular `message` field. |
| 788 | pub fn read_singular_message_into<M: Message + Default>( |
| 789 | wire_type: WireType, |
| 790 | is: &mut CodedInputStream, |
| 791 | target: &mut SingularPtrField<M>, |
| 792 | ) -> ProtobufResult<()> { |
| 793 | match wire_type { |
| 794 | WireTypeLengthDelimited => { |
| 795 | is.incr_recursion()?; |
| 796 | let tmp = target.set_default(); |
| 797 | let res = is.merge_message(tmp); |
| 798 | is.decr_recursion(); |
| 799 | res |
| 800 | } |
| 801 | _ => Err(unexpected_wire_type(wire_type)), |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | fn skip_group(is: &mut CodedInputStream) -> ProtobufResult<()> { |
| 806 | loop { |
| 807 | let (_, wire_type) = is.read_tag_unpack()?; |
| 808 | if wire_type == wire_format::WireTypeEndGroup { |
| 809 | return Ok(()); |
| 810 | } |
| 811 | is.skip_field(wire_type)?; |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | /// Handle unknown field in generated code. |
| 816 | /// Either store a value in unknown, or skip a group. |
| 817 | pub fn read_unknown_or_skip_group( |
| 818 | field_number: u32, |
| 819 | wire_type: WireType, |
| 820 | is: &mut CodedInputStream, |
| 821 | unknown_fields: &mut UnknownFields, |
| 822 | ) -> ProtobufResult<()> { |
| 823 | match wire_type { |
| 824 | wire_format::WireTypeStartGroup => skip_group(is), |
| 825 | _ => { |
| 826 | let unknown = is.read_unknown(wire_type)?; |
| 827 | unknown_fields.add_value(field_number, unknown); |
| 828 | Ok(()) |
| 829 | } |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | /// Create an error for unexpected wire type. |
| 834 | /// |
| 835 | /// Function is used in generated code, so error types can be changed, |
| 836 | /// but this function remains unchanged. |
| 837 | pub fn unexpected_wire_type(wire_type: WireType) -> ProtobufError { |
| 838 | ProtobufError::WireError(WireError::UnexpectedWireType(wire_type)) |
| 839 | } |
| 840 | |
| 841 | /// Compute serialized size of `map` field and cache nested field sizes. |
| 842 | pub fn compute_map_size<K, V>(field_number: u32, map: &HashMap<K::Value, V::Value>) -> u32 |
| 843 | where |
| 844 | K: ProtobufType, |
| 845 | V: ProtobufType, |
| 846 | K::Value: Eq + Hash, |
| 847 | { |
| 848 | let mut sum = 0; |
| 849 | for (k, v) in map { |
| 850 | let key_tag_size = 1; |
| 851 | let value_tag_size = 1; |
| 852 | |
| 853 | let key_len = K::compute_size_with_length_delimiter(k); |
| 854 | let value_len = V::compute_size_with_length_delimiter(v); |
| 855 | |
| 856 | let entry_len = key_tag_size + key_len + value_tag_size + value_len; |
| 857 | sum += tag_size(field_number) + compute_raw_varint32_size(entry_len) + entry_len; |
| 858 | } |
| 859 | sum |
| 860 | } |
| 861 | |
| 862 | /// Write map, message sizes must be already known. |
| 863 | pub fn write_map_with_cached_sizes<K, V>( |
| 864 | field_number: u32, |
| 865 | map: &HashMap<K::Value, V::Value>, |
| 866 | os: &mut CodedOutputStream, |
| 867 | ) -> ProtobufResult<()> |
| 868 | where |
| 869 | K: ProtobufType, |
| 870 | V: ProtobufType, |
| 871 | K::Value: Eq + Hash, |
| 872 | { |
| 873 | for (k, v) in map { |
| 874 | let key_tag_size = 1; |
| 875 | let value_tag_size = 1; |
| 876 | |
| 877 | let key_len = K::get_cached_size_with_length_delimiter(k); |
| 878 | let value_len = V::get_cached_size_with_length_delimiter(v); |
| 879 | |
| 880 | let entry_len = key_tag_size + key_len + value_tag_size + value_len; |
| 881 | |
| 882 | os.write_tag(field_number, WireType::WireTypeLengthDelimited)?; |
| 883 | os.write_raw_varint32(entry_len)?; |
| 884 | K::write_with_cached_size(1, k, os)?; |
| 885 | V::write_with_cached_size(2, v, os)?; |
| 886 | } |
| 887 | Ok(()) |
| 888 | } |
| 889 | |
| 890 | /// Read `map` field. |
| 891 | pub fn read_map_into<K, V>( |
| 892 | wire_type: WireType, |
| 893 | is: &mut CodedInputStream, |
| 894 | target: &mut HashMap<K::Value, V::Value>, |
| 895 | ) -> ProtobufResult<()> |
| 896 | where |
| 897 | K: ProtobufType, |
| 898 | V: ProtobufType, |
| 899 | K::Value: Eq + Hash + Default, |
| 900 | V::Value: Default, |
| 901 | { |
| 902 | if wire_type != WireType::WireTypeLengthDelimited { |
| 903 | return Err(unexpected_wire_type(wire_type)); |
| 904 | } |
| 905 | |
| 906 | let mut key = Default::default(); |
| 907 | let mut value = Default::default(); |
| 908 | |
| 909 | let len = is.read_raw_varint32()?; |
| 910 | let old_limit = is.push_limit(len as u64)?; |
| 911 | while !is.eof()? { |
| 912 | let (field_number, wire_type) = is.read_tag_unpack()?; |
| 913 | match field_number { |
| 914 | 1 => { |
| 915 | if wire_type != K::wire_type() { |
| 916 | return Err(unexpected_wire_type(wire_type)); |
| 917 | } |
| 918 | key = K::read(is)?; |
| 919 | } |
| 920 | 2 => { |
| 921 | if wire_type != V::wire_type() { |
| 922 | return Err(unexpected_wire_type(wire_type)); |
| 923 | } |
| 924 | value = V::read(is)?; |
| 925 | } |
| 926 | _ => is.skip_field(wire_type)?, |
| 927 | } |
| 928 | } |
| 929 | is.pop_limit(old_limit); |
| 930 | |
| 931 | target.insert(key, value); |
| 932 | |
| 933 | Ok(()) |
| 934 | } |