Skip to main content

prost_amino/
message.rs

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