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