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