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