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