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