prost_bytes05/
message.rs

1use std::fmt::Debug;
2use std::usize;
3
4use bytes::{Buf, BufMut};
5
6use crate::encoding::{
7    decode_key, encode_varint, encoded_len_varint, message, DecodeContext, WireType,
8};
9use crate::DecodeError;
10use crate::EncodeError;
11
12/// A Protocol Buffers message.
13pub trait Message: Debug + Send + Sync {
14    /// Encodes the message to a buffer.
15    ///
16    /// This method will panic if the buffer has insufficient capacity.
17    ///
18    /// Meant to be used only by `Message` implementations.
19    #[doc(hidden)]
20    fn encode_raw<B>(&self, buf: &mut B)
21    where
22        B: BufMut,
23        Self: Sized;
24
25    /// Decodes a field from a buffer, and merges it into `self`.
26    ///
27    /// Meant to be used only by `Message` implementations.
28    #[doc(hidden)]
29    fn merge_field<B>(
30        &mut self,
31        tag: u32,
32        wire_type: WireType,
33        buf: &mut B,
34        ctx: DecodeContext,
35    ) -> Result<(), DecodeError>
36    where
37        B: Buf,
38        Self: Sized;
39
40    /// Returns the encoded length of the message without a length delimiter.
41    fn encoded_len(&self) -> usize;
42
43    /// Encodes the message to a buffer.
44    ///
45    /// An error will be returned if the buffer does not have sufficient capacity.
46    fn encode<B>(&self, buf: &mut B) -> Result<(), EncodeError>
47    where
48        B: BufMut,
49        Self: Sized,
50    {
51        let required = self.encoded_len();
52        let remaining = buf.remaining_mut();
53        if required > buf.remaining_mut() {
54            return Err(EncodeError::new(required, remaining));
55        }
56
57        self.encode_raw(buf);
58        Ok(())
59    }
60
61    /// Encodes the message with a length-delimiter to a buffer.
62    ///
63    /// An error will be returned if the buffer does not have sufficient capacity.
64    fn encode_length_delimited<B>(&self, buf: &mut B) -> Result<(), EncodeError>
65    where
66        B: BufMut,
67        Self: Sized,
68    {
69        let len = self.encoded_len();
70        let required = len + encoded_len_varint(len as u64);
71        let remaining = buf.remaining_mut();
72        if required > remaining {
73            return Err(EncodeError::new(required, remaining));
74        }
75        encode_varint(len as u64, buf);
76        self.encode_raw(buf);
77        Ok(())
78    }
79
80    /// Decodes an instance of the message from a buffer.
81    ///
82    /// The entire buffer will be consumed.
83    fn decode<B>(mut buf: B) -> Result<Self, DecodeError>
84    where
85        B: Buf,
86        Self: Default,
87    {
88        let mut message = Self::default();
89        Self::merge(&mut message, &mut buf).map(|_| message)
90    }
91
92    /// Decodes a length-delimited instance of the message from the buffer.
93    fn decode_length_delimited<B>(buf: B) -> Result<Self, DecodeError>
94    where
95        B: Buf,
96        Self: Default,
97    {
98        let mut message = Self::default();
99        message.merge_length_delimited(buf)?;
100        Ok(message)
101    }
102
103    /// Decodes an instance of the message from a buffer, and merges it into `self`.
104    ///
105    /// The entire buffer will be consumed.
106    fn merge<B>(&mut self, mut buf: B) -> Result<(), DecodeError>
107    where
108        B: Buf,
109        Self: Sized,
110    {
111        let ctx = DecodeContext::default();
112        while buf.has_remaining() {
113            let (tag, wire_type) = decode_key(&mut buf)?;
114            self.merge_field(tag, wire_type, &mut buf, ctx.clone())?;
115        }
116        Ok(())
117    }
118
119    /// Decodes a length-delimited instance of the message from buffer, and
120    /// merges it into `self`.
121    fn merge_length_delimited<B>(&mut self, mut buf: B) -> Result<(), DecodeError>
122    where
123        B: Buf,
124        Self: Sized,
125    {
126        message::merge(
127            WireType::LengthDelimited,
128            self,
129            &mut buf,
130            DecodeContext::default(),
131        )
132    }
133
134    /// Clears the message, resetting all fields to their default.
135    fn clear(&mut self);
136}
137
138impl<M> Message for Box<M>
139where
140    M: Message,
141{
142    fn encode_raw<B>(&self, buf: &mut B)
143    where
144        B: BufMut,
145    {
146        (**self).encode_raw(buf)
147    }
148    fn merge_field<B>(
149        &mut self,
150        tag: u32,
151        wire_type: WireType,
152        buf: &mut B,
153        ctx: DecodeContext,
154    ) -> Result<(), DecodeError>
155    where
156        B: Buf,
157    {
158        (**self).merge_field(tag, wire_type, buf, ctx)
159    }
160    fn encoded_len(&self) -> usize {
161        (**self).encoded_len()
162    }
163    fn clear(&mut self) {
164        (**self).clear()
165    }
166}