blob: 3bb02801562692e9a2027da3019b38fbc1c543e4 [file] [log] [blame]
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -07001//! Functions used by generated protobuf code.
2//! Should not be used by programs written by hands.
3
4use std::collections::HashMap;
5use std::default::Default;
6use std::hash::Hash;
7
8#[cfg(feature = "bytes")]
Haibo Huangd32e6ee2020-08-12 13:52:04 -07009use crate::chars::Chars;
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -070010#[cfg(feature = "bytes")]
Haibo Huangd32e6ee2020-08-12 13:52:04 -070011use bytes::Bytes;
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -070012
Joel Galensonfa77f002021-04-02 11:32:01 -070013use crate::coded_input_stream::CodedInputStream;
14use crate::coded_output_stream::CodedOutputStream;
Haibo Huangd32e6ee2020-08-12 13:52:04 -070015use crate::enums::ProtobufEnum;
16use crate::error::ProtobufError;
17use crate::error::ProtobufResult;
18use crate::error::WireError;
19use crate::message::*;
20use crate::repeated::RepeatedField;
21use crate::singular::SingularField;
22use crate::singular::SingularPtrField;
Haibo Huangd32e6ee2020-08-12 13:52:04 -070023use crate::types::*;
24use crate::zigzag::*;
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -070025
Haibo Huang52aa7852020-07-10 20:23:55 -070026pub use crate::lazy_v2::LazyV2;
Haibo Huangd32e6ee2020-08-12 13:52:04 -070027use crate::unknown::UnknownFields;
Joel Galensonfa77f002021-04-02 11:32:01 -070028use crate::wire_format;
29use crate::wire_format::WireType;
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -070030
31/// Given `u64` value compute varint encoded length.
32pub 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.
64pub 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.
69pub trait ProtobufVarint {
70 /// Size of self when encoded as varint.
71 fn len_varint(&self) -> u32;
72}
73
74/// Helper trait implemented by integer types which could be encoded as zigzag varint.
75pub trait ProtobufVarintZigzag {
76 /// Size of self when encoded as zigzag varint.
77 fn len_varint_zigzag(&self) -> u32;
78}
79
80impl ProtobufVarint for u64 {
81 fn len_varint(&self) -> u32 {
82 compute_raw_varint64_size(*self)
83 }
84}
85
86impl ProtobufVarint for u32 {
87 fn len_varint(&self) -> u32 {
88 (*self as u64).len_varint()
89 }
90}
91
92impl ProtobufVarint for i64 {
93 fn len_varint(&self) -> u32 {
94 // same as length of u64
95 (*self as u64).len_varint()
96 }
97}
98
99impl ProtobufVarintZigzag for i64 {
100 fn len_varint_zigzag(&self) -> u32 {
101 compute_raw_varint64_size(encode_zig_zag_64(*self))
102 }
103}
104
105impl ProtobufVarint for i32 {
106 fn len_varint(&self) -> u32 {
107 // sign-extend and then compute
108 (*self as i64).len_varint()
109 }
110}
111
112impl ProtobufVarintZigzag for i32 {
113 fn len_varint_zigzag(&self) -> u32 {
114 compute_raw_varint32_size(encode_zig_zag_32(*self))
115 }
116}
117
118impl ProtobufVarint for bool {
119 fn len_varint(&self) -> u32 {
120 1
121 }
122}
123
124/* Commented out due to https://github.com/mozilla/rust/issues/8075
125impl<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.
133pub 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.
138pub 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.
145pub 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
152pub 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
162pub 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
172pub 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.
182pub fn tag_size(field_number: u32) -> u32 {
Joel Galensonfa77f002021-04-02 11:32:01 -0700183 wire_format::Tag::make(field_number, WireType::WireTypeFixed64)
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700184 .value()
185 .len_varint()
186}
187
188fn value_size_no_tag<T: ProtobufVarint>(value: T, wt: WireType) -> u32 {
189 match wt {
Joel Galensonfa77f002021-04-02 11:32:01 -0700190 WireType::WireTypeFixed64 => 8,
191 WireType::WireTypeFixed32 => 4,
192 WireType::WireTypeVarint => value.len_varint(),
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700193 _ => panic!(),
194 }
195}
196
197/// Integer value size when encoded as specified wire type.
198pub 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.
203pub 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
208pub 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
212fn enum_size_no_tag<E: ProtobufEnum>(value: E) -> u32 {
213 value.value().len_varint()
214}
215
216/// Size of encoded enum field value.
217pub fn enum_size<E: ProtobufEnum>(field_number: u32, value: E) -> u32 {
218 tag_size(field_number) + enum_size_no_tag(value)
219}
220
221fn 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.
226pub fn bytes_size(field_number: u32, bytes: &[u8]) -> u32 {
227 tag_size(field_number) + bytes_size_no_tag(bytes)
228}
229
230fn string_size_no_tag(s: &str) -> u32 {
231 bytes_size_no_tag(s.as_bytes())
232}
233
234/// Size of encoded string field.
235pub 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.
240pub 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.
260pub fn read_repeated_int32_into(
261 wire_type: WireType,
262 is: &mut CodedInputStream,
263 target: &mut Vec<i32>,
264) -> ProtobufResult<()> {
265 match wire_type {
Joel Galensonfa77f002021-04-02 11:32:01 -0700266 WireType::WireTypeLengthDelimited => is.read_repeated_packed_int32_into(target),
267 WireType::WireTypeVarint => {
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700268 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.
276pub fn read_repeated_int64_into(
277 wire_type: WireType,
278 is: &mut CodedInputStream,
279 target: &mut Vec<i64>,
280) -> ProtobufResult<()> {
281 match wire_type {
Joel Galensonfa77f002021-04-02 11:32:01 -0700282 WireType::WireTypeLengthDelimited => is.read_repeated_packed_int64_into(target),
283 WireType::WireTypeVarint => {
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700284 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.
292pub fn read_repeated_uint32_into(
293 wire_type: WireType,
294 is: &mut CodedInputStream,
295 target: &mut Vec<u32>,
296) -> ProtobufResult<()> {
297 match wire_type {
Joel Galensonfa77f002021-04-02 11:32:01 -0700298 WireType::WireTypeLengthDelimited => is.read_repeated_packed_uint32_into(target),
299 WireType::WireTypeVarint => {
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700300 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.
308pub fn read_repeated_uint64_into(
309 wire_type: WireType,
310 is: &mut CodedInputStream,
311 target: &mut Vec<u64>,
312) -> ProtobufResult<()> {
313 match wire_type {
Joel Galensonfa77f002021-04-02 11:32:01 -0700314 WireType::WireTypeLengthDelimited => is.read_repeated_packed_uint64_into(target),
315 WireType::WireTypeVarint => {
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700316 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.
324pub fn read_repeated_sint32_into(
325 wire_type: WireType,
326 is: &mut CodedInputStream,
327 target: &mut Vec<i32>,
328) -> ProtobufResult<()> {
329 match wire_type {
Joel Galensonfa77f002021-04-02 11:32:01 -0700330 WireType::WireTypeLengthDelimited => is.read_repeated_packed_sint32_into(target),
331 WireType::WireTypeVarint => {
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700332 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.
340pub fn read_repeated_sint64_into(
341 wire_type: WireType,
342 is: &mut CodedInputStream,
343 target: &mut Vec<i64>,
344) -> ProtobufResult<()> {
345 match wire_type {
Joel Galensonfa77f002021-04-02 11:32:01 -0700346 WireType::WireTypeLengthDelimited => is.read_repeated_packed_sint64_into(target),
347 WireType::WireTypeVarint => {
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700348 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.
356pub fn read_repeated_fixed32_into(
357 wire_type: WireType,
358 is: &mut CodedInputStream,
359 target: &mut Vec<u32>,
360) -> ProtobufResult<()> {
361 match wire_type {
Joel Galensonfa77f002021-04-02 11:32:01 -0700362 WireType::WireTypeLengthDelimited => is.read_repeated_packed_fixed32_into(target),
363 WireType::WireTypeFixed32 => {
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700364 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.
372pub fn read_repeated_fixed64_into(
373 wire_type: WireType,
374 is: &mut CodedInputStream,
375 target: &mut Vec<u64>,
376) -> ProtobufResult<()> {
377 match wire_type {
Joel Galensonfa77f002021-04-02 11:32:01 -0700378 WireType::WireTypeLengthDelimited => is.read_repeated_packed_fixed64_into(target),
379 WireType::WireTypeFixed64 => {
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700380 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.
388pub fn read_repeated_sfixed32_into(
389 wire_type: WireType,
390 is: &mut CodedInputStream,
391 target: &mut Vec<i32>,
392) -> ProtobufResult<()> {
393 match wire_type {
Joel Galensonfa77f002021-04-02 11:32:01 -0700394 WireType::WireTypeLengthDelimited => is.read_repeated_packed_sfixed32_into(target),
395 WireType::WireTypeFixed32 => {
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700396 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.
404pub fn read_repeated_sfixed64_into(
405 wire_type: WireType,
406 is: &mut CodedInputStream,
407 target: &mut Vec<i64>,
408) -> ProtobufResult<()> {
409 match wire_type {
Joel Galensonfa77f002021-04-02 11:32:01 -0700410 WireType::WireTypeLengthDelimited => is.read_repeated_packed_sfixed64_into(target),
411 WireType::WireTypeFixed64 => {
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700412 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.
420pub fn read_repeated_double_into(
421 wire_type: WireType,
422 is: &mut CodedInputStream,
423 target: &mut Vec<f64>,
424) -> ProtobufResult<()> {
425 match wire_type {
Joel Galensonfa77f002021-04-02 11:32:01 -0700426 WireType::WireTypeLengthDelimited => is.read_repeated_packed_double_into(target),
427 WireType::WireTypeFixed64 => {
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700428 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.
436pub fn read_repeated_float_into(
437 wire_type: WireType,
438 is: &mut CodedInputStream,
439 target: &mut Vec<f32>,
440) -> ProtobufResult<()> {
441 match wire_type {
Joel Galensonfa77f002021-04-02 11:32:01 -0700442 WireType::WireTypeLengthDelimited => is.read_repeated_packed_float_into(target),
443 WireType::WireTypeFixed32 => {
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700444 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.
452pub fn read_repeated_bool_into(
453 wire_type: WireType,
454 is: &mut CodedInputStream,
455 target: &mut Vec<bool>,
456) -> ProtobufResult<()> {
457 match wire_type {
Joel Galensonfa77f002021-04-02 11:32:01 -0700458 WireType::WireTypeLengthDelimited => is.read_repeated_packed_bool_into(target),
459 WireType::WireTypeVarint => {
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700460 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.
469pub 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 {
Joel Galensonfa77f002021-04-02 11:32:01 -0700475 WireType::WireTypeLengthDelimited => is.read_repeated_packed_enum_into(target),
476 WireType::WireTypeVarint => {
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700477 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]
486fn 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<()>
492where
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
503fn 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)
524pub 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 {
Joel Galensonfa77f002021-04-02 11:32:01 -0700532 WireType::WireTypeLengthDelimited => read_repeated_packed_enum_with_unknown_fields_into(
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700533 is,
534 target,
535 field_number,
536 unknown_fields,
537 ),
Joel Galensonfa77f002021-04-02 11:32:01 -0700538 WireType::WireTypeVarint => {
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700539 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)
551pub 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)
571pub 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.
586pub fn read_repeated_string_into(
587 wire_type: WireType,
588 is: &mut CodedInputStream,
589 target: &mut RepeatedField<String>,
590) -> ProtobufResult<()> {
591 match wire_type {
Joel Galensonfa77f002021-04-02 11:32:01 -0700592 WireType::WireTypeLengthDelimited => {
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700593 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")]
602pub 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 {
Joel Galensonfa77f002021-04-02 11:32:01 -0700608 WireType::WireTypeLengthDelimited => {
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700609 target.push(is.read_carllerche_chars()?);
610 Ok(())
611 }
612 _ => Err(unexpected_wire_type(wire_type)),
613 }
614}
615
616/// Read singular `string` field.
617pub fn read_singular_string_into(
618 wire_type: WireType,
619 is: &mut CodedInputStream,
620 target: &mut SingularField<String>,
621) -> ProtobufResult<()> {
622 match wire_type {
Joel Galensonfa77f002021-04-02 11:32:01 -0700623 WireType::WireTypeLengthDelimited => {
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700624 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")]
633pub 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 {
Joel Galensonfa77f002021-04-02 11:32:01 -0700639 WireType::WireTypeLengthDelimited => {
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700640 *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.
648pub fn read_singular_proto3_string_into(
649 wire_type: WireType,
650 is: &mut CodedInputStream,
651 target: &mut String,
652) -> ProtobufResult<()> {
653 match wire_type {
Joel Galensonfa77f002021-04-02 11:32:01 -0700654 WireType::WireTypeLengthDelimited => is.read_string_into(target),
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700655 _ => Err(unexpected_wire_type(wire_type)),
656 }
657}
658
659/// Read singular `Chars` field for proto3.
660#[cfg(feature = "bytes")]
661pub 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 {
Joel Galensonfa77f002021-04-02 11:32:01 -0700667 WireType::WireTypeLengthDelimited => {
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700668 *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.
676pub 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 {
Joel Galensonfa77f002021-04-02 11:32:01 -0700682 WireType::WireTypeLengthDelimited => {
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700683 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")]
692pub 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 {
Joel Galensonfa77f002021-04-02 11:32:01 -0700698 WireType::WireTypeLengthDelimited => {
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700699 target.push(is.read_carllerche_bytes()?);
700 Ok(())
701 }
702 _ => Err(unexpected_wire_type(wire_type)),
703 }
704}
705
706/// Read singular `bytes` field.
707pub 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 {
Joel Galensonfa77f002021-04-02 11:32:01 -0700713 WireType::WireTypeLengthDelimited => {
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700714 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")]
723pub 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 {
Joel Galensonfa77f002021-04-02 11:32:01 -0700729 WireType::WireTypeLengthDelimited => {
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700730 *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.
738pub 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 {
Joel Galensonfa77f002021-04-02 11:32:01 -0700744 WireType::WireTypeLengthDelimited => is.read_bytes_into(target),
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700745 _ => Err(unexpected_wire_type(wire_type)),
746 }
747}
748
749/// Read singular `Bytes` field for proto3.
750#[cfg(feature = "bytes")]
751pub 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 {
Joel Galensonfa77f002021-04-02 11:32:01 -0700757 WireType::WireTypeLengthDelimited => {
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700758 *target = is.read_carllerche_bytes()?;
759 Ok(())
760 }
761 _ => Err(unexpected_wire_type(wire_type)),
762 }
763}
764
765/// Read repeated `message` field.
766pub 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 {
Joel Galensonfa77f002021-04-02 11:32:01 -0700772 WireType::WireTypeLengthDelimited => {
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700773 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.
784pub 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 {
Joel Galensonfa77f002021-04-02 11:32:01 -0700790 WireType::WireTypeLengthDelimited => {
Chih-Hung Hsiehcfc3a232020-06-10 20:13:05 -0700791 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
801fn 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.
813pub 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.
833pub 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.
838pub fn compute_map_size<K, V>(field_number: u32, map: &HashMap<K::Value, V::Value>) -> u32
839where
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.
859pub 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<()>
864where
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.
887pub fn read_map_into<K, V>(
888 wire_type: WireType,
889 is: &mut CodedInputStream,
890 target: &mut HashMap<K::Value, V::Value>,
891) -> ProtobufResult<()>
892where
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}