prost_bytes05/
types.rs

1//! Protocol Buffers well-known wrapper types.
2//!
3//! This module provides implementations of `Message` for Rust standard library types which
4//! correspond to a Protobuf well-known wrapper type. The remaining well-known types are defined in
5//! the `prost-types` crate in order to avoid a cyclic dependency between `prost` and
6//! `prost-build`.
7
8use ::bytes::{Buf, BufMut};
9
10use crate::{
11    encoding::{
12        bool, bytes, double, float, int32, int64, skip_field, string, uint32, uint64,
13        DecodeContext, WireType,
14    },
15    DecodeError, Message,
16};
17
18/// `google.protobuf.BoolValue`
19impl Message for bool {
20    fn encode_raw<B>(&self, buf: &mut B)
21    where
22        B: BufMut,
23    {
24        if *self {
25            bool::encode(1, self, buf)
26        }
27    }
28    fn merge_field<B>(
29        &mut self,
30        tag: u32,
31        wire_type: WireType,
32        buf: &mut B,
33        ctx: DecodeContext,
34    ) -> Result<(), DecodeError>
35    where
36        B: Buf,
37    {
38        if tag == 1 {
39            bool::merge(wire_type, self, buf, ctx)
40        } else {
41            skip_field(wire_type, tag, buf)
42        }
43    }
44    fn encoded_len(&self) -> usize {
45        if *self {
46            2
47        } else {
48            0
49        }
50    }
51    fn clear(&mut self) {
52        *self = false;
53    }
54}
55
56/// `google.protobuf.UInt32Value`
57impl Message for u32 {
58    fn encode_raw<B>(&self, buf: &mut B)
59    where
60        B: BufMut,
61    {
62        if *self != 0 {
63            uint32::encode(1, self, buf)
64        }
65    }
66    fn merge_field<B>(
67        &mut self,
68        tag: u32,
69        wire_type: WireType,
70        buf: &mut B,
71        ctx: DecodeContext,
72    ) -> Result<(), DecodeError>
73    where
74        B: Buf,
75    {
76        if tag == 1 {
77            uint32::merge(wire_type, self, buf, ctx)
78        } else {
79            skip_field(wire_type, tag, buf)
80        }
81    }
82    fn encoded_len(&self) -> usize {
83        if *self != 0 {
84            uint32::encoded_len(1, self)
85        } else {
86            0
87        }
88    }
89    fn clear(&mut self) {
90        *self = 0;
91    }
92}
93
94/// `google.protobuf.UInt64Value`
95impl Message for u64 {
96    fn encode_raw<B>(&self, buf: &mut B)
97    where
98        B: BufMut,
99    {
100        if *self != 0 {
101            uint64::encode(1, self, buf)
102        }
103    }
104    fn merge_field<B>(
105        &mut self,
106        tag: u32,
107        wire_type: WireType,
108        buf: &mut B,
109        ctx: DecodeContext,
110    ) -> Result<(), DecodeError>
111    where
112        B: Buf,
113    {
114        if tag == 1 {
115            uint64::merge(wire_type, self, buf, ctx)
116        } else {
117            skip_field(wire_type, tag, buf)
118        }
119    }
120    fn encoded_len(&self) -> usize {
121        if *self != 0 {
122            uint64::encoded_len(1, self)
123        } else {
124            0
125        }
126    }
127    fn clear(&mut self) {
128        *self = 0;
129    }
130}
131
132/// `google.protobuf.Int32Value`
133impl Message for i32 {
134    fn encode_raw<B>(&self, buf: &mut B)
135    where
136        B: BufMut,
137    {
138        if *self != 0 {
139            int32::encode(1, self, buf)
140        }
141    }
142    fn merge_field<B>(
143        &mut self,
144        tag: u32,
145        wire_type: WireType,
146        buf: &mut B,
147        ctx: DecodeContext,
148    ) -> Result<(), DecodeError>
149    where
150        B: Buf,
151    {
152        if tag == 1 {
153            int32::merge(wire_type, self, buf, ctx)
154        } else {
155            skip_field(wire_type, tag, buf)
156        }
157    }
158    fn encoded_len(&self) -> usize {
159        if *self != 0 {
160            int32::encoded_len(1, self)
161        } else {
162            0
163        }
164    }
165    fn clear(&mut self) {
166        *self = 0;
167    }
168}
169
170/// `google.protobuf.Int64Value`
171impl Message for i64 {
172    fn encode_raw<B>(&self, buf: &mut B)
173    where
174        B: BufMut,
175    {
176        if *self != 0 {
177            int64::encode(1, self, buf)
178        }
179    }
180    fn merge_field<B>(
181        &mut self,
182        tag: u32,
183        wire_type: WireType,
184        buf: &mut B,
185        ctx: DecodeContext,
186    ) -> Result<(), DecodeError>
187    where
188        B: Buf,
189    {
190        if tag == 1 {
191            int64::merge(wire_type, self, buf, ctx)
192        } else {
193            skip_field(wire_type, tag, buf)
194        }
195    }
196    fn encoded_len(&self) -> usize {
197        if *self != 0 {
198            int64::encoded_len(1, self)
199        } else {
200            0
201        }
202    }
203    fn clear(&mut self) {
204        *self = 0;
205    }
206}
207
208/// `google.protobuf.FloatValue`
209impl Message for f32 {
210    fn encode_raw<B>(&self, buf: &mut B)
211    where
212        B: BufMut,
213    {
214        if *self != 0.0 {
215            float::encode(1, self, buf)
216        }
217    }
218    fn merge_field<B>(
219        &mut self,
220        tag: u32,
221        wire_type: WireType,
222        buf: &mut B,
223        ctx: DecodeContext,
224    ) -> Result<(), DecodeError>
225    where
226        B: Buf,
227    {
228        if tag == 1 {
229            float::merge(wire_type, self, buf, ctx)
230        } else {
231            skip_field(wire_type, tag, buf)
232        }
233    }
234    fn encoded_len(&self) -> usize {
235        if *self != 0.0 {
236            float::encoded_len(1, self)
237        } else {
238            0
239        }
240    }
241    fn clear(&mut self) {
242        *self = 0.0;
243    }
244}
245
246/// `google.protobuf.DoubleValue`
247impl Message for f64 {
248    fn encode_raw<B>(&self, buf: &mut B)
249    where
250        B: BufMut,
251    {
252        if *self != 0.0 {
253            double::encode(1, self, buf)
254        }
255    }
256    fn merge_field<B>(
257        &mut self,
258        tag: u32,
259        wire_type: WireType,
260        buf: &mut B,
261        ctx: DecodeContext,
262    ) -> Result<(), DecodeError>
263    where
264        B: Buf,
265    {
266        if tag == 1 {
267            double::merge(wire_type, self, buf, ctx)
268        } else {
269            skip_field(wire_type, tag, buf)
270        }
271    }
272    fn encoded_len(&self) -> usize {
273        if *self != 0.0 {
274            double::encoded_len(1, self)
275        } else {
276            0
277        }
278    }
279    fn clear(&mut self) {
280        *self = 0.0;
281    }
282}
283
284/// `google.protobuf.StringValue`
285impl Message for String {
286    fn encode_raw<B>(&self, buf: &mut B)
287    where
288        B: BufMut,
289    {
290        if !self.is_empty() {
291            string::encode(1, self, buf)
292        }
293    }
294    fn merge_field<B>(
295        &mut self,
296        tag: u32,
297        wire_type: WireType,
298        buf: &mut B,
299        ctx: DecodeContext,
300    ) -> Result<(), DecodeError>
301    where
302        B: Buf,
303    {
304        if tag == 1 {
305            string::merge(wire_type, self, buf, ctx)
306        } else {
307            skip_field(wire_type, tag, buf)
308        }
309    }
310    fn encoded_len(&self) -> usize {
311        if !self.is_empty() {
312            string::encoded_len(1, self)
313        } else {
314            0
315        }
316    }
317    fn clear(&mut self) {
318        self.clear();
319    }
320}
321
322/// `google.protobuf.BytesValue`
323impl Message for Vec<u8> {
324    fn encode_raw<B>(&self, buf: &mut B)
325    where
326        B: BufMut,
327    {
328        if !self.is_empty() {
329            bytes::encode(1, self, buf)
330        }
331    }
332    fn merge_field<B>(
333        &mut self,
334        tag: u32,
335        wire_type: WireType,
336        buf: &mut B,
337        ctx: DecodeContext,
338    ) -> Result<(), DecodeError>
339    where
340        B: Buf,
341    {
342        if tag == 1 {
343            bytes::merge(wire_type, self, buf, ctx)
344        } else {
345            skip_field(wire_type, tag, buf)
346        }
347    }
348    fn encoded_len(&self) -> usize {
349        if !self.is_empty() {
350            bytes::encoded_len(1, self)
351        } else {
352            0
353        }
354    }
355    fn clear(&mut self) {
356        self.clear();
357    }
358}
359
360/// `google.protobuf.Empty`
361impl Message for () {
362    fn encode_raw<B>(&self, _buf: &mut B)
363    where
364        B: BufMut,
365    {
366    }
367    fn merge_field<B>(
368        &mut self,
369        tag: u32,
370        wire_type: WireType,
371        buf: &mut B,
372        _ctx: DecodeContext,
373    ) -> Result<(), DecodeError>
374    where
375        B: Buf,
376    {
377        skip_field(wire_type, tag, buf)
378    }
379    fn encoded_len(&self) -> usize {
380        0
381    }
382    fn clear(&mut self) {}
383}