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