1 //! Implementations of `ProtobufType` for all types.
2 
3 #![doc(hidden)]
4 
5 use std::marker;
6 
7 #[cfg(feature = "bytes")]
8 use ::bytes::Bytes;
9 
10 #[cfg(feature = "bytes")]
11 use crate::chars::Chars;
12 use crate::coded_input_stream::CodedInputStream;
13 use crate::coded_output_stream::CodedOutputStream;
14 use crate::enums::Enum;
15 use crate::error::Result;
16 pub use crate::reflect::type_dynamic::ProtobufTypeDynamic;
17 use crate::reflect::type_dynamic::ProtobufTypeDynamicImpl;
18 use crate::reflect::ProtobufValue;
19 use crate::rt;
20 use crate::rt::singular::value_varint_zigzag_size_no_tag;
21 use crate::wire_format::WireType;
22 use crate::zigzag::decode_zig_zag_32;
23 use crate::zigzag::decode_zig_zag_64;
24 use crate::EnumOrUnknown;
25 use crate::Message;
26 use crate::UnknownValueRef;
27 
28 /// Encapsulate type-specific serialization and conversion logic
29 pub(crate) trait ProtobufTypeTrait: Send + Sync + Clone + Sized + 'static {
30     /// Rust type for this protobuf type.
31     type ProtobufValue: Default;
32 
33     /// Dynamic version of this
dynamic() -> &'static dyn ProtobufTypeDynamic where Self::ProtobufValue: ProtobufValue,34     fn dynamic() -> &'static dyn ProtobufTypeDynamic
35     where
36         Self::ProtobufValue: ProtobufValue,
37     {
38         &ProtobufTypeDynamicImpl::<Self>(marker::PhantomData)
39     }
40 
41     /// Wire type for encoding objects of this type
42     const WIRE_TYPE: WireType;
43 
44     /// Read a value from `CodedInputStream`
read(is: &mut CodedInputStream) -> Result<Self::ProtobufValue>45     fn read(is: &mut CodedInputStream) -> Result<Self::ProtobufValue>;
46 
47     /// Take a value from `UnknownValues`
get_from_unknown(_unknown: UnknownValueRef) -> Option<Self::ProtobufValue>48     fn get_from_unknown(_unknown: UnknownValueRef) -> Option<Self::ProtobufValue>;
49 
50     /// Compute serialized size of a value
compute_size(value: &Self::ProtobufValue) -> u6451     fn compute_size(value: &Self::ProtobufValue) -> u64;
52 
53     /// Compute size adding length prefix if wire type is length delimited
54     /// (i. e. string, bytes, message)
compute_size_with_length_delimiter(value: &Self::ProtobufValue) -> u6455     fn compute_size_with_length_delimiter(value: &Self::ProtobufValue) -> u64 {
56         let size = Self::compute_size(value);
57         if Self::WIRE_TYPE == WireType::LengthDelimited {
58             rt::compute_raw_varint64_size(size) + size
59         } else {
60             size
61         }
62     }
63 
64     /// Get previously computed size
65     #[inline]
get_cached_size(value: &Self::ProtobufValue) -> u3266     fn get_cached_size(value: &Self::ProtobufValue) -> u32 {
67         Self::compute_size(value) as u32
68     }
69 
70     /// Get previously cached size with length prefix
71     #[inline]
get_cached_size_with_length_delimiter(value: &Self::ProtobufValue) -> u3272     fn get_cached_size_with_length_delimiter(value: &Self::ProtobufValue) -> u32 {
73         let size = Self::get_cached_size(value);
74         if Self::WIRE_TYPE == WireType::LengthDelimited {
75             rt::compute_raw_varint32_size(size) as u32 + size
76         } else {
77             size
78         }
79     }
80 
81     /// Write a value with previously cached size
write_with_cached_size( field_number: u32, value: &Self::ProtobufValue, os: &mut CodedOutputStream, ) -> Result<()>82     fn write_with_cached_size(
83         field_number: u32,
84         value: &Self::ProtobufValue,
85         os: &mut CodedOutputStream,
86     ) -> Result<()>;
87 }
88 
89 /// All fixed size types
90 pub(crate) trait ProtobufTypeFixed: ProtobufTypeTrait {
91     /// Encoded size of value in bytes of this type.
92     ///
93     /// E. g. it is `4` for `fixed32`
94     const ENCODED_SIZE: u32;
95 }
96 
97 /// `float`
98 #[derive(Copy, Clone)]
99 pub struct ProtobufTypeFloat;
100 /// `double`
101 #[derive(Copy, Clone)]
102 pub struct ProtobufTypeDouble;
103 /// `int32`
104 #[derive(Copy, Clone)]
105 pub struct ProtobufTypeInt32;
106 /// `int64`
107 #[derive(Copy, Clone)]
108 pub struct ProtobufTypeInt64;
109 /// `uint32`
110 #[derive(Copy, Clone)]
111 pub struct ProtobufTypeUint32;
112 /// `uint64`
113 #[derive(Copy, Clone)]
114 pub struct ProtobufTypeUint64;
115 /// `sint32`
116 #[derive(Copy, Clone)]
117 pub struct ProtobufTypeSint32;
118 /// `sint64`
119 #[derive(Copy, Clone)]
120 pub struct ProtobufTypeSint64;
121 /// `fixed32`
122 #[derive(Copy, Clone)]
123 pub struct ProtobufTypeFixed32;
124 /// `fixed64`
125 #[derive(Copy, Clone)]
126 pub struct ProtobufTypeFixed64;
127 /// `sfixed32`
128 #[derive(Copy, Clone)]
129 pub struct ProtobufTypeSfixed32;
130 /// `sfixed64`
131 #[derive(Copy, Clone)]
132 pub struct ProtobufTypeSfixed64;
133 /// `bool`
134 #[derive(Copy, Clone)]
135 pub struct ProtobufTypeBool;
136 /// `string`
137 #[derive(Copy, Clone)]
138 pub struct ProtobufTypeString;
139 /// `bytes`
140 #[derive(Copy, Clone)]
141 pub struct ProtobufTypeBytes;
142 
143 /// `bytes` as [`Bytes`](bytes::Bytes)
144 #[cfg(feature = "bytes")]
145 #[derive(Copy, Clone)]
146 pub struct ProtobufTypeTokioBytes;
147 /// `string` as [`Chars`](crate::Chars)
148 #[cfg(feature = "bytes")]
149 #[derive(Copy, Clone)]
150 pub struct ProtobufTypeTokioChars;
151 
152 /// `enum` as `ProtobufEnumOrUnknown`
153 #[derive(Copy, Clone)]
154 pub struct ProtobufTypeEnumOrUnknown<E: Enum>(marker::PhantomData<E>);
155 /// `message`
156 #[derive(Copy, Clone)]
157 pub struct ProtobufTypeMessage<M: Message>(marker::PhantomData<M>);
158 
159 impl ProtobufTypeTrait for ProtobufTypeFloat {
160     type ProtobufValue = f32;
161 
162     const WIRE_TYPE: WireType = WireType::Fixed32;
163 
read(is: &mut CodedInputStream) -> Result<f32>164     fn read(is: &mut CodedInputStream) -> Result<f32> {
165         is.read_float()
166     }
167 
get_from_unknown(unknown: UnknownValueRef) -> Option<f32>168     fn get_from_unknown(unknown: UnknownValueRef) -> Option<f32> {
169         match unknown {
170             UnknownValueRef::Fixed32(v) => Some(f32::from_bits(v)),
171             _ => None,
172         }
173     }
174 
compute_size(_value: &f32) -> u64175     fn compute_size(_value: &f32) -> u64 {
176         Self::ENCODED_SIZE as u64
177     }
178 
write_with_cached_size( field_number: u32, value: &f32, os: &mut CodedOutputStream, ) -> Result<()>179     fn write_with_cached_size(
180         field_number: u32,
181         value: &f32,
182         os: &mut CodedOutputStream,
183     ) -> Result<()> {
184         os.write_float(field_number, *value)
185     }
186 }
187 
188 impl ProtobufTypeFixed for ProtobufTypeFloat {
189     const ENCODED_SIZE: u32 = 4;
190 }
191 
192 impl ProtobufTypeTrait for ProtobufTypeDouble {
193     type ProtobufValue = f64;
194 
195     const WIRE_TYPE: WireType = WireType::Fixed64;
196 
read(is: &mut CodedInputStream) -> Result<f64>197     fn read(is: &mut CodedInputStream) -> Result<f64> {
198         is.read_double()
199     }
200 
get_from_unknown(unknown: UnknownValueRef) -> Option<f64>201     fn get_from_unknown(unknown: UnknownValueRef) -> Option<f64> {
202         match unknown {
203             UnknownValueRef::Fixed64(v) => Some(f64::from_bits(v)),
204             _ => None,
205         }
206     }
207 
compute_size(_value: &f64) -> u64208     fn compute_size(_value: &f64) -> u64 {
209         Self::ENCODED_SIZE as u64
210     }
211 
write_with_cached_size( field_number: u32, value: &f64, os: &mut CodedOutputStream, ) -> Result<()>212     fn write_with_cached_size(
213         field_number: u32,
214         value: &f64,
215         os: &mut CodedOutputStream,
216     ) -> Result<()> {
217         os.write_double(field_number, *value)
218     }
219 }
220 
221 impl ProtobufTypeFixed for ProtobufTypeDouble {
222     const ENCODED_SIZE: u32 = 8;
223 }
224 
225 impl ProtobufTypeTrait for ProtobufTypeInt32 {
226     type ProtobufValue = i32;
227 
228     const WIRE_TYPE: WireType = WireType::Varint;
229 
read(is: &mut CodedInputStream) -> Result<i32>230     fn read(is: &mut CodedInputStream) -> Result<i32> {
231         is.read_int32()
232     }
233 
compute_size(value: &i32) -> u64234     fn compute_size(value: &i32) -> u64 {
235         // See also: https://github.com/protocolbuffers/protobuf/blob/bd00671b924310c0353a730bf8fa77c44e0a9c72/src/google/protobuf/io/coded_stream.h#L1300-L1306
236         if *value < 0 {
237             return 10;
238         }
239         rt::compute_raw_varint32_size(*value as u32)
240     }
241 
write_with_cached_size( field_number: u32, value: &i32, os: &mut CodedOutputStream, ) -> Result<()>242     fn write_with_cached_size(
243         field_number: u32,
244         value: &i32,
245         os: &mut CodedOutputStream,
246     ) -> Result<()> {
247         os.write_int32(field_number, *value)
248     }
249 
get_from_unknown(unknown: UnknownValueRef) -> Option<i32>250     fn get_from_unknown(unknown: UnknownValueRef) -> Option<i32> {
251         match unknown {
252             UnknownValueRef::Varint(v) => Some(v as i32),
253             _ => None,
254         }
255     }
256 }
257 
258 impl ProtobufTypeTrait for ProtobufTypeInt64 {
259     type ProtobufValue = i64;
260 
261     const WIRE_TYPE: WireType = WireType::Varint;
262 
read(is: &mut CodedInputStream) -> Result<i64>263     fn read(is: &mut CodedInputStream) -> Result<i64> {
264         is.read_int64()
265     }
266 
get_from_unknown(unknown: UnknownValueRef) -> Option<i64>267     fn get_from_unknown(unknown: UnknownValueRef) -> Option<i64> {
268         match unknown {
269             UnknownValueRef::Varint(v) => Some(v as i64),
270             _ => None,
271         }
272     }
273 
compute_size(value: &i64) -> u64274     fn compute_size(value: &i64) -> u64 {
275         rt::compute_raw_varint64_size(*value as u64)
276     }
277 
write_with_cached_size( field_number: u32, value: &i64, os: &mut CodedOutputStream, ) -> Result<()>278     fn write_with_cached_size(
279         field_number: u32,
280         value: &i64,
281         os: &mut CodedOutputStream,
282     ) -> Result<()> {
283         os.write_int64(field_number, *value)
284     }
285 }
286 
287 impl ProtobufTypeTrait for ProtobufTypeUint32 {
288     type ProtobufValue = u32;
289 
290     const WIRE_TYPE: WireType = WireType::Varint;
291 
read(is: &mut CodedInputStream) -> Result<u32>292     fn read(is: &mut CodedInputStream) -> Result<u32> {
293         is.read_uint32()
294     }
295 
get_from_unknown(unknown: UnknownValueRef) -> Option<u32>296     fn get_from_unknown(unknown: UnknownValueRef) -> Option<u32> {
297         match unknown {
298             UnknownValueRef::Varint(v) => Some(v as u32),
299             _ => None,
300         }
301     }
302 
compute_size(value: &u32) -> u64303     fn compute_size(value: &u32) -> u64 {
304         rt::compute_raw_varint32_size(*value)
305     }
306 
write_with_cached_size( field_number: u32, value: &u32, os: &mut CodedOutputStream, ) -> Result<()>307     fn write_with_cached_size(
308         field_number: u32,
309         value: &u32,
310         os: &mut CodedOutputStream,
311     ) -> Result<()> {
312         os.write_uint32(field_number, *value)
313     }
314 }
315 
316 impl ProtobufTypeTrait for ProtobufTypeUint64 {
317     type ProtobufValue = u64;
318 
319     const WIRE_TYPE: WireType = WireType::Varint;
320 
read(is: &mut CodedInputStream) -> Result<u64>321     fn read(is: &mut CodedInputStream) -> Result<u64> {
322         is.read_uint64()
323     }
324 
get_from_unknown(unknown: UnknownValueRef) -> Option<u64>325     fn get_from_unknown(unknown: UnknownValueRef) -> Option<u64> {
326         match unknown {
327             UnknownValueRef::Varint(v) => Some(v as u64),
328             _ => None,
329         }
330     }
331 
compute_size(value: &u64) -> u64332     fn compute_size(value: &u64) -> u64 {
333         rt::compute_raw_varint64_size(*value)
334     }
335 
write_with_cached_size( field_number: u32, value: &u64, os: &mut CodedOutputStream, ) -> Result<()>336     fn write_with_cached_size(
337         field_number: u32,
338         value: &u64,
339         os: &mut CodedOutputStream,
340     ) -> Result<()> {
341         os.write_uint64(field_number, *value)
342     }
343 }
344 
345 impl ProtobufTypeTrait for ProtobufTypeSint32 {
346     type ProtobufValue = i32;
347 
348     const WIRE_TYPE: WireType = WireType::Varint;
349 
read(is: &mut CodedInputStream) -> Result<i32>350     fn read(is: &mut CodedInputStream) -> Result<i32> {
351         is.read_sint32()
352     }
353 
get_from_unknown(unknown: UnknownValueRef) -> Option<i32>354     fn get_from_unknown(unknown: UnknownValueRef) -> Option<i32> {
355         ProtobufTypeUint32::get_from_unknown(unknown).map(decode_zig_zag_32)
356     }
357 
compute_size(value: &i32) -> u64358     fn compute_size(value: &i32) -> u64 {
359         value_varint_zigzag_size_no_tag(*value)
360     }
361 
write_with_cached_size( field_number: u32, value: &i32, os: &mut CodedOutputStream, ) -> Result<()>362     fn write_with_cached_size(
363         field_number: u32,
364         value: &i32,
365         os: &mut CodedOutputStream,
366     ) -> Result<()> {
367         os.write_sint32(field_number, *value)
368     }
369 }
370 
371 impl ProtobufTypeTrait for ProtobufTypeSint64 {
372     type ProtobufValue = i64;
373 
374     const WIRE_TYPE: WireType = WireType::Varint;
375 
read(is: &mut CodedInputStream) -> Result<i64>376     fn read(is: &mut CodedInputStream) -> Result<i64> {
377         is.read_sint64()
378     }
379 
get_from_unknown(unknown: UnknownValueRef) -> Option<i64>380     fn get_from_unknown(unknown: UnknownValueRef) -> Option<i64> {
381         ProtobufTypeUint64::get_from_unknown(unknown).map(decode_zig_zag_64)
382     }
383 
compute_size(value: &i64) -> u64384     fn compute_size(value: &i64) -> u64 {
385         value_varint_zigzag_size_no_tag(*value)
386     }
387 
write_with_cached_size( field_number: u32, value: &i64, os: &mut CodedOutputStream, ) -> Result<()>388     fn write_with_cached_size(
389         field_number: u32,
390         value: &i64,
391         os: &mut CodedOutputStream,
392     ) -> Result<()> {
393         os.write_sint64(field_number, *value)
394     }
395 }
396 
397 impl ProtobufTypeTrait for ProtobufTypeFixed32 {
398     type ProtobufValue = u32;
399 
400     const WIRE_TYPE: WireType = WireType::Fixed32;
401 
read(is: &mut CodedInputStream) -> Result<u32>402     fn read(is: &mut CodedInputStream) -> Result<u32> {
403         is.read_fixed32()
404     }
405 
get_from_unknown(unknown: UnknownValueRef) -> Option<u32>406     fn get_from_unknown(unknown: UnknownValueRef) -> Option<u32> {
407         match unknown {
408             UnknownValueRef::Fixed32(v) => Some(v),
409             _ => None,
410         }
411     }
412 
compute_size(_value: &u32) -> u64413     fn compute_size(_value: &u32) -> u64 {
414         Self::ENCODED_SIZE as u64
415     }
416 
write_with_cached_size( field_number: u32, value: &u32, os: &mut CodedOutputStream, ) -> Result<()>417     fn write_with_cached_size(
418         field_number: u32,
419         value: &u32,
420         os: &mut CodedOutputStream,
421     ) -> Result<()> {
422         os.write_fixed32(field_number, *value)
423     }
424 }
425 
426 impl ProtobufTypeFixed for ProtobufTypeFixed32 {
427     const ENCODED_SIZE: u32 = 4;
428 }
429 
430 impl ProtobufTypeTrait for ProtobufTypeFixed64 {
431     type ProtobufValue = u64;
432 
433     const WIRE_TYPE: WireType = WireType::Fixed64;
434 
read(is: &mut CodedInputStream) -> Result<u64>435     fn read(is: &mut CodedInputStream) -> Result<u64> {
436         is.read_fixed64()
437     }
438 
get_from_unknown(unknown: UnknownValueRef) -> Option<u64>439     fn get_from_unknown(unknown: UnknownValueRef) -> Option<u64> {
440         match unknown {
441             UnknownValueRef::Fixed64(v) => Some(v),
442             _ => None,
443         }
444     }
445 
compute_size(_value: &u64) -> u64446     fn compute_size(_value: &u64) -> u64 {
447         Self::ENCODED_SIZE as u64
448     }
449 
write_with_cached_size( field_number: u32, value: &u64, os: &mut CodedOutputStream, ) -> Result<()>450     fn write_with_cached_size(
451         field_number: u32,
452         value: &u64,
453         os: &mut CodedOutputStream,
454     ) -> Result<()> {
455         os.write_fixed64(field_number, *value)
456     }
457 }
458 
459 impl ProtobufTypeFixed for ProtobufTypeFixed64 {
460     const ENCODED_SIZE: u32 = 8;
461 }
462 
463 impl ProtobufTypeTrait for ProtobufTypeSfixed32 {
464     type ProtobufValue = i32;
465 
466     const WIRE_TYPE: WireType = WireType::Fixed32;
467 
read(is: &mut CodedInputStream) -> Result<i32>468     fn read(is: &mut CodedInputStream) -> Result<i32> {
469         is.read_sfixed32()
470     }
471 
get_from_unknown(unknown: UnknownValueRef) -> Option<i32>472     fn get_from_unknown(unknown: UnknownValueRef) -> Option<i32> {
473         match unknown {
474             UnknownValueRef::Fixed32(v) => Some(v as i32),
475             _ => None,
476         }
477     }
478 
compute_size(_value: &i32) -> u64479     fn compute_size(_value: &i32) -> u64 {
480         Self::ENCODED_SIZE as u64
481     }
482 
write_with_cached_size( field_number: u32, value: &i32, os: &mut CodedOutputStream, ) -> Result<()>483     fn write_with_cached_size(
484         field_number: u32,
485         value: &i32,
486         os: &mut CodedOutputStream,
487     ) -> Result<()> {
488         os.write_sfixed32(field_number, *value)
489     }
490 }
491 
492 impl ProtobufTypeFixed for ProtobufTypeSfixed32 {
493     const ENCODED_SIZE: u32 = 4;
494 }
495 
496 impl ProtobufTypeTrait for ProtobufTypeSfixed64 {
497     type ProtobufValue = i64;
498 
499     const WIRE_TYPE: WireType = WireType::Fixed64;
500 
read(is: &mut CodedInputStream) -> Result<i64>501     fn read(is: &mut CodedInputStream) -> Result<i64> {
502         is.read_sfixed64()
503     }
504 
get_from_unknown(unknown: UnknownValueRef) -> Option<i64>505     fn get_from_unknown(unknown: UnknownValueRef) -> Option<i64> {
506         match unknown {
507             UnknownValueRef::Fixed64(v) => Some(v as i64),
508             _ => None,
509         }
510     }
511 
compute_size(_value: &i64) -> u64512     fn compute_size(_value: &i64) -> u64 {
513         8
514     }
515 
write_with_cached_size( field_number: u32, value: &i64, os: &mut CodedOutputStream, ) -> Result<()>516     fn write_with_cached_size(
517         field_number: u32,
518         value: &i64,
519         os: &mut CodedOutputStream,
520     ) -> Result<()> {
521         os.write_sfixed64(field_number, *value)
522     }
523 }
524 
525 impl ProtobufTypeFixed for ProtobufTypeSfixed64 {
526     const ENCODED_SIZE: u32 = 8;
527 }
528 
529 impl ProtobufTypeTrait for ProtobufTypeBool {
530     type ProtobufValue = bool;
531 
532     const WIRE_TYPE: WireType = WireType::Varint;
533 
read(is: &mut CodedInputStream) -> Result<bool>534     fn read(is: &mut CodedInputStream) -> Result<bool> {
535         is.read_bool()
536     }
537 
get_from_unknown(unknown: UnknownValueRef) -> Option<bool>538     fn get_from_unknown(unknown: UnknownValueRef) -> Option<bool> {
539         match unknown {
540             UnknownValueRef::Varint(b) => Some(b != 0),
541             _ => None,
542         }
543     }
544 
compute_size(_value: &bool) -> u64545     fn compute_size(_value: &bool) -> u64 {
546         1
547     }
548 
write_with_cached_size( field_number: u32, value: &bool, os: &mut CodedOutputStream, ) -> Result<()>549     fn write_with_cached_size(
550         field_number: u32,
551         value: &bool,
552         os: &mut CodedOutputStream,
553     ) -> Result<()> {
554         os.write_bool(field_number, *value)
555     }
556 }
557 
558 impl ProtobufTypeTrait for ProtobufTypeString {
559     type ProtobufValue = String;
560 
561     const WIRE_TYPE: WireType = WireType::LengthDelimited;
562 
read(is: &mut CodedInputStream) -> Result<String>563     fn read(is: &mut CodedInputStream) -> Result<String> {
564         is.read_string()
565     }
566 
get_from_unknown(unknown: UnknownValueRef) -> Option<String>567     fn get_from_unknown(unknown: UnknownValueRef) -> Option<String> {
568         match unknown {
569             UnknownValueRef::LengthDelimited(v) => String::from_utf8(v.to_vec()).ok(),
570             _ => None,
571         }
572     }
573 
compute_size(value: &String) -> u64574     fn compute_size(value: &String) -> u64 {
575         value.len() as u64
576     }
577 
write_with_cached_size( field_number: u32, value: &String, os: &mut CodedOutputStream, ) -> Result<()>578     fn write_with_cached_size(
579         field_number: u32,
580         value: &String,
581         os: &mut CodedOutputStream,
582     ) -> Result<()> {
583         os.write_string(field_number, &value)
584     }
585 }
586 
587 impl ProtobufTypeTrait for ProtobufTypeBytes {
588     type ProtobufValue = Vec<u8>;
589 
590     const WIRE_TYPE: WireType = WireType::LengthDelimited;
591 
read(is: &mut CodedInputStream) -> Result<Vec<u8>>592     fn read(is: &mut CodedInputStream) -> Result<Vec<u8>> {
593         is.read_bytes()
594     }
595 
get_from_unknown(unknown: UnknownValueRef) -> Option<Vec<u8>>596     fn get_from_unknown(unknown: UnknownValueRef) -> Option<Vec<u8>> {
597         match unknown {
598             UnknownValueRef::LengthDelimited(v) => Some(v.to_vec()),
599             _ => None,
600         }
601     }
602 
compute_size(value: &Vec<u8>) -> u64603     fn compute_size(value: &Vec<u8>) -> u64 {
604         value.len() as u64
605     }
606 
write_with_cached_size( field_number: u32, value: &Vec<u8>, os: &mut CodedOutputStream, ) -> Result<()>607     fn write_with_cached_size(
608         field_number: u32,
609         value: &Vec<u8>,
610         os: &mut CodedOutputStream,
611     ) -> Result<()> {
612         os.write_bytes(field_number, &value)
613     }
614 }
615 
616 #[cfg(feature = "bytes")]
617 impl ProtobufTypeTrait for ProtobufTypeTokioBytes {
618     type ProtobufValue = bytes::Bytes;
619 
620     const WIRE_TYPE: WireType = ProtobufTypeBytes::WIRE_TYPE;
621 
read(is: &mut CodedInputStream) -> Result<Bytes>622     fn read(is: &mut CodedInputStream) -> Result<Bytes> {
623         is.read_tokio_bytes()
624     }
625 
get_from_unknown(unknown: UnknownValueRef) -> Option<Bytes>626     fn get_from_unknown(unknown: UnknownValueRef) -> Option<Bytes> {
627         ProtobufTypeBytes::get_from_unknown(unknown).map(Bytes::from)
628     }
629 
compute_size(value: &Bytes) -> u64630     fn compute_size(value: &Bytes) -> u64 {
631         value.len() as u64
632     }
633 
write_with_cached_size( field_number: u32, value: &Bytes, os: &mut CodedOutputStream, ) -> Result<()>634     fn write_with_cached_size(
635         field_number: u32,
636         value: &Bytes,
637         os: &mut CodedOutputStream,
638     ) -> Result<()> {
639         os.write_bytes(field_number, &value)
640     }
641 }
642 
643 #[cfg(feature = "bytes")]
644 impl ProtobufTypeTrait for ProtobufTypeTokioChars {
645     type ProtobufValue = Chars;
646 
647     const WIRE_TYPE: WireType = ProtobufTypeBytes::WIRE_TYPE;
648 
read(is: &mut CodedInputStream) -> Result<Chars>649     fn read(is: &mut CodedInputStream) -> Result<Chars> {
650         is.read_tokio_chars()
651     }
652 
get_from_unknown(unknown: UnknownValueRef) -> Option<Chars>653     fn get_from_unknown(unknown: UnknownValueRef) -> Option<Chars> {
654         ProtobufTypeString::get_from_unknown(unknown).map(Chars::from)
655     }
656 
compute_size(value: &Chars) -> u64657     fn compute_size(value: &Chars) -> u64 {
658         value.len() as u64
659     }
660 
write_with_cached_size( field_number: u32, value: &Chars, os: &mut CodedOutputStream, ) -> Result<()>661     fn write_with_cached_size(
662         field_number: u32,
663         value: &Chars,
664         os: &mut CodedOutputStream,
665     ) -> Result<()> {
666         os.write_string(field_number, &value)
667     }
668 }
669 
670 impl<E: Enum> ProtobufTypeTrait for ProtobufTypeEnumOrUnknown<E> {
671     type ProtobufValue = EnumOrUnknown<E>;
672 
673     const WIRE_TYPE: WireType = WireType::Varint;
674 
read(is: &mut CodedInputStream) -> Result<EnumOrUnknown<E>>675     fn read(is: &mut CodedInputStream) -> Result<EnumOrUnknown<E>> {
676         is.read_enum_or_unknown()
677     }
678 
get_from_unknown(unknown: UnknownValueRef) -> Option<EnumOrUnknown<E>>679     fn get_from_unknown(unknown: UnknownValueRef) -> Option<EnumOrUnknown<E>> {
680         ProtobufTypeInt32::get_from_unknown(unknown).map(|i| EnumOrUnknown::from_i32(i))
681     }
682 
compute_size(value: &EnumOrUnknown<E>) -> u64683     fn compute_size(value: &EnumOrUnknown<E>) -> u64 {
684         ProtobufTypeInt32::compute_size(&value.value())
685     }
686 
write_with_cached_size( field_number: u32, value: &EnumOrUnknown<E>, os: &mut CodedOutputStream, ) -> Result<()>687     fn write_with_cached_size(
688         field_number: u32,
689         value: &EnumOrUnknown<E>,
690         os: &mut CodedOutputStream,
691     ) -> Result<()> {
692         os.write_enum_or_unknown(field_number, *value)
693     }
694 }
695 
696 impl<M: Message + Clone + Default> ProtobufTypeTrait for ProtobufTypeMessage<M> {
697     type ProtobufValue = M;
698 
699     const WIRE_TYPE: WireType = WireType::LengthDelimited;
700 
read(is: &mut CodedInputStream) -> Result<M>701     fn read(is: &mut CodedInputStream) -> Result<M> {
702         is.read_message()
703     }
704 
get_from_unknown(unknown: UnknownValueRef) -> Option<M>705     fn get_from_unknown(unknown: UnknownValueRef) -> Option<M> {
706         match unknown {
707             UnknownValueRef::LengthDelimited(v) => M::parse_from_bytes(&v).ok(),
708             _ => None,
709         }
710     }
711 
compute_size(value: &M) -> u64712     fn compute_size(value: &M) -> u64 {
713         value.compute_size()
714     }
715 
get_cached_size(value: &M) -> u32716     fn get_cached_size(value: &M) -> u32 {
717         value.cached_size()
718     }
719 
write_with_cached_size( field_number: u32, value: &Self::ProtobufValue, os: &mut CodedOutputStream, ) -> Result<()>720     fn write_with_cached_size(
721         field_number: u32,
722         value: &Self::ProtobufValue,
723         os: &mut CodedOutputStream,
724     ) -> Result<()> {
725         os.write_tag(field_number, WireType::LengthDelimited)?;
726         os.write_raw_varint32(value.cached_size())?;
727         value.write_to_with_cached_sizes(os)?;
728         Ok(())
729     }
730 }
731