/rust/registry/src/index.crates.io-1949cf8c6b5b557f/protobuf-3.7.2/src/message.rs
Line | Count | Source |
1 | | use std::io::Read; |
2 | | use std::io::Write; |
3 | | |
4 | | use crate::coded_output_stream::with::WithCodedOutputStream; |
5 | | use crate::error::ProtobufError; |
6 | | use crate::wire_format::check_message_size; |
7 | | use crate::CodedInputStream; |
8 | | use crate::CodedOutputStream; |
9 | | use crate::SpecialFields; |
10 | | use crate::UnknownFields; |
11 | | |
12 | | /// Trait which is implemented by all generated message. |
13 | | /// |
14 | | /// Note, by default all generated messages also implement [`MessageFull`](crate::MessageFull) |
15 | | /// trait which provides access to reflection and features which depend on reflection |
16 | | /// (text format and JSON serialization). |
17 | | pub trait Message: Default + Clone + Send + Sync + Sized + PartialEq + 'static { |
18 | | /// Message name as specified in `.proto` file. |
19 | | /// |
20 | | /// Message name can be accessed using |
21 | | /// [`MessageFull::descriptor`](crate::MessageFull::descriptor), |
22 | | /// but when lite runtime is requested, this field can be used. |
23 | | const NAME: &'static str; |
24 | | |
25 | | /// True iff all required fields are initialized. |
26 | | /// Always returns `true` for protobuf 3. |
27 | | fn is_initialized(&self) -> bool; |
28 | | |
29 | | /// Update this message object with fields read from given stream. |
30 | | fn merge_from(&mut self, is: &mut CodedInputStream) -> crate::Result<()>; |
31 | | |
32 | | /// Parse message from stream. |
33 | 0 | fn parse_from(is: &mut CodedInputStream) -> crate::Result<Self> { |
34 | 0 | let mut r: Self = Message::new(); |
35 | 0 | r.merge_from(is)?; |
36 | 0 | r.check_initialized()?; |
37 | 0 | Ok(r) |
38 | 0 | } |
39 | | |
40 | | /// Write message to the stream. |
41 | | /// |
42 | | /// Sizes of this messages and nested messages must be cached |
43 | | /// by calling `compute_size` prior to this call. |
44 | | fn write_to_with_cached_sizes(&self, os: &mut CodedOutputStream) -> crate::Result<()>; |
45 | | |
46 | | /// Compute and cache size of this message and all nested messages. |
47 | | /// |
48 | | /// Note if the computation overflows u32, the cached size is stored truncated. |
49 | | fn compute_size(&self) -> u64; |
50 | | |
51 | | /// Get size previously computed by `compute_size`. |
52 | | /// |
53 | | /// Note if message size exceeds u32, the cached size is stored truncated. |
54 | 0 | fn cached_size(&self) -> u32 { |
55 | 0 | self.special_fields().cached_size().get() |
56 | 0 | } Unexecuted instantiation: <pprof::protos::profile::Line as protobuf::message::Message>::cached_size Unexecuted instantiation: <pprof::protos::profile::Label as protobuf::message::Message>::cached_size Unexecuted instantiation: <pprof::protos::profile::Sample as protobuf::message::Message>::cached_size Unexecuted instantiation: <pprof::protos::profile::Mapping as protobuf::message::Message>::cached_size Unexecuted instantiation: <pprof::protos::profile::Function as protobuf::message::Message>::cached_size Unexecuted instantiation: <pprof::protos::profile::Location as protobuf::message::Message>::cached_size Unexecuted instantiation: <pprof::protos::profile::ValueType as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::descriptor::EnumOptions as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::descriptor::FileOptions as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::descriptor::FieldOptions as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::descriptor::OneofOptions as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::descriptor::MethodOptions as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::descriptor::MessageOptions as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::descriptor::ServiceOptions as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::descriptor::SourceCodeInfo as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::descriptor::DescriptorProto as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::descriptor::EnumValueOptions as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::descriptor::GeneratedCodeInfo as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::descriptor::EnumDescriptorProto as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::descriptor::FileDescriptorProto as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::descriptor::UninterpretedOption as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::descriptor::FieldDescriptorProto as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::descriptor::OneofDescriptorProto as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::descriptor::ExtensionRangeOptions as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::descriptor::MethodDescriptorProto as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::descriptor::ServiceDescriptorProto as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::descriptor::EnumValueDescriptorProto as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::plugin::Version as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::descriptor::uninterpreted_option::NamePart as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::descriptor::enum_descriptor_proto::EnumReservedRange as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::descriptor::descriptor_proto::ReservedRange as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::descriptor::descriptor_proto::ExtensionRange as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::descriptor::generated_code_info::Annotation as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::descriptor::source_code_info::Location as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::well_known_types::type_::Field as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::well_known_types::type_::Option as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::well_known_types::type_::EnumValue as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::well_known_types::struct_::Value as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::well_known_types::struct_::Struct as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::well_known_types::struct_::ListValue as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::well_known_types::source_context::SourceContext as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::well_known_types::any::Any as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::well_known_types::api::Mixin as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::well_known_types::api::Method as protobuf::message::Message>::cached_size Unexecuted instantiation: <protobuf::plugin::code_generator_response::File as protobuf::message::Message>::cached_size |
57 | | |
58 | | /// Write the message to the stream. |
59 | | /// |
60 | | /// Results in error if message is not fully initialized. |
61 | 0 | fn write_to(&self, os: &mut CodedOutputStream) -> crate::Result<()> { |
62 | 0 | self.check_initialized()?; |
63 | | |
64 | | // cache sizes |
65 | 0 | let size = self.compute_size(); |
66 | 0 | let size = check_message_size(size)?; |
67 | 0 | os.reserve_additional(size as u32, Self::NAME)?; |
68 | 0 | self.write_to_with_cached_sizes(os)?; |
69 | | |
70 | 0 | Ok(()) |
71 | 0 | } Unexecuted instantiation: <pprof::protos::profile::Profile as protobuf::message::Message>::write_to Unexecuted instantiation: <_ as protobuf::message::Message>::write_to |
72 | | |
73 | | /// Write the message to the stream prepending the message with message length |
74 | | /// encoded as varint. |
75 | 0 | fn write_length_delimited_to(&self, os: &mut CodedOutputStream) -> crate::Result<()> { |
76 | 0 | let size = self.compute_size(); |
77 | 0 | let size = check_message_size(size)?; |
78 | | |
79 | 0 | os.reserve_additional_for_length_delimited(size, Self::NAME)?; |
80 | | |
81 | 0 | os.write_raw_varint32(size)?; |
82 | | |
83 | 0 | let written = os.total_bytes_written(); |
84 | | |
85 | 0 | self.write_to_with_cached_sizes(os)?; |
86 | | |
87 | | // Self-check. |
88 | 0 | assert_eq!( |
89 | 0 | written + size as u64, |
90 | 0 | os.total_bytes_written(), |
91 | 0 | "Expected to write {}, actually wrote {}", |
92 | | size, |
93 | 0 | os.total_bytes_written() - written |
94 | | ); |
95 | | |
96 | 0 | Ok(()) |
97 | 0 | } |
98 | | |
99 | | /// Write the message to the vec, prepend the message with message length |
100 | | /// encoded as varint. |
101 | 0 | fn write_length_delimited_to_vec(&self, vec: &mut Vec<u8>) -> crate::Result<()> { |
102 | 0 | let mut os = CodedOutputStream::vec(vec); |
103 | 0 | self.write_length_delimited_to(&mut os)?; |
104 | 0 | os.flush()?; |
105 | 0 | Ok(()) |
106 | 0 | } |
107 | | |
108 | | /// Update this message object with fields read from given stream. |
109 | 0 | fn merge_from_bytes(&mut self, bytes: &[u8]) -> crate::Result<()> { |
110 | 0 | let mut is = CodedInputStream::from_bytes(bytes); |
111 | 0 | self.merge_from(&mut is) |
112 | 0 | } |
113 | | |
114 | | /// Parse message from reader. |
115 | | /// Parse stops on EOF or when error encountered. |
116 | 0 | fn parse_from_reader(reader: &mut dyn Read) -> crate::Result<Self> { |
117 | 0 | let mut is = CodedInputStream::new(reader); |
118 | 0 | let r = Message::parse_from(&mut is)?; |
119 | 0 | is.check_eof()?; |
120 | 0 | Ok(r) |
121 | 0 | } |
122 | | |
123 | | /// Parse message from byte array. |
124 | 0 | fn parse_from_bytes(bytes: &[u8]) -> crate::Result<Self> { |
125 | 0 | let mut is = CodedInputStream::from_bytes(bytes); |
126 | 0 | let r = Message::parse_from(&mut is)?; |
127 | 0 | is.check_eof()?; |
128 | 0 | Ok(r) |
129 | 0 | } |
130 | | |
131 | | /// Parse message from `Bytes` object. |
132 | | /// Resulting message may share references to the passed bytes object. |
133 | | #[cfg(feature = "bytes")] |
134 | | fn parse_from_tokio_bytes(bytes: &bytes::Bytes) -> crate::Result<Self> { |
135 | | let mut is = CodedInputStream::from_tokio_bytes(bytes); |
136 | | let r = Self::parse_from(&mut is)?; |
137 | | is.check_eof()?; |
138 | | Ok(r) |
139 | | } |
140 | | |
141 | | /// Check if all required fields of this object are initialized. |
142 | 0 | fn check_initialized(&self) -> crate::Result<()> { |
143 | 0 | if !self.is_initialized() { |
144 | 0 | Err(ProtobufError::MessageNotInitialized(Self::NAME.to_owned()).into()) |
145 | | } else { |
146 | 0 | Ok(()) |
147 | | } |
148 | 0 | } Unexecuted instantiation: <pprof::protos::profile::Line as protobuf::message::Message>::check_initialized Unexecuted instantiation: <pprof::protos::profile::Label as protobuf::message::Message>::check_initialized Unexecuted instantiation: <pprof::protos::profile::Sample as protobuf::message::Message>::check_initialized Unexecuted instantiation: <pprof::protos::profile::Mapping as protobuf::message::Message>::check_initialized Unexecuted instantiation: <pprof::protos::profile::Profile as protobuf::message::Message>::check_initialized Unexecuted instantiation: <pprof::protos::profile::Function as protobuf::message::Message>::check_initialized Unexecuted instantiation: <pprof::protos::profile::Location as protobuf::message::Message>::check_initialized Unexecuted instantiation: <pprof::protos::profile::ValueType as protobuf::message::Message>::check_initialized Unexecuted instantiation: <protobuf::descriptor::DescriptorProto as protobuf::message::Message>::check_initialized Unexecuted instantiation: <protobuf::descriptor::EnumDescriptorProto as protobuf::message::Message>::check_initialized Unexecuted instantiation: <protobuf::descriptor::FileDescriptorProto as protobuf::message::Message>::check_initialized Unexecuted instantiation: <protobuf::descriptor::UninterpretedOption as protobuf::message::Message>::check_initialized Unexecuted instantiation: <protobuf::descriptor::FieldDescriptorProto as protobuf::message::Message>::check_initialized Unexecuted instantiation: <protobuf::descriptor::OneofDescriptorProto as protobuf::message::Message>::check_initialized Unexecuted instantiation: <protobuf::descriptor::MethodDescriptorProto as protobuf::message::Message>::check_initialized Unexecuted instantiation: <protobuf::descriptor::ServiceDescriptorProto as protobuf::message::Message>::check_initialized Unexecuted instantiation: <protobuf::descriptor::EnumValueDescriptorProto as protobuf::message::Message>::check_initialized Unexecuted instantiation: <protobuf::descriptor::uninterpreted_option::NamePart as protobuf::message::Message>::check_initialized Unexecuted instantiation: <protobuf::descriptor::enum_descriptor_proto::EnumReservedRange as protobuf::message::Message>::check_initialized Unexecuted instantiation: <protobuf::descriptor::descriptor_proto::ReservedRange as protobuf::message::Message>::check_initialized Unexecuted instantiation: <protobuf::descriptor::descriptor_proto::ExtensionRange as protobuf::message::Message>::check_initialized Unexecuted instantiation: <protobuf::descriptor::generated_code_info::Annotation as protobuf::message::Message>::check_initialized Unexecuted instantiation: <protobuf::descriptor::source_code_info::Location as protobuf::message::Message>::check_initialized Unexecuted instantiation: <protobuf::well_known_types::type_::Field as protobuf::message::Message>::check_initialized Unexecuted instantiation: <protobuf::well_known_types::type_::Option as protobuf::message::Message>::check_initialized Unexecuted instantiation: <protobuf::well_known_types::type_::EnumValue as protobuf::message::Message>::check_initialized Unexecuted instantiation: <protobuf::well_known_types::struct_::Value as protobuf::message::Message>::check_initialized Unexecuted instantiation: <protobuf::well_known_types::struct_::Struct as protobuf::message::Message>::check_initialized Unexecuted instantiation: <protobuf::well_known_types::struct_::ListValue as protobuf::message::Message>::check_initialized Unexecuted instantiation: <protobuf::well_known_types::api::Mixin as protobuf::message::Message>::check_initialized Unexecuted instantiation: <protobuf::well_known_types::api::Method as protobuf::message::Message>::check_initialized Unexecuted instantiation: <protobuf::plugin::code_generator_response::File as protobuf::message::Message>::check_initialized |
149 | | |
150 | | /// Write the message to the writer. |
151 | 0 | fn write_to_writer(&self, w: &mut dyn Write) -> crate::Result<()> { |
152 | 0 | w.with_coded_output_stream(|os| self.write_to(os)) |
153 | 0 | } |
154 | | |
155 | | /// Write the message to bytes vec. |
156 | 0 | fn write_to_vec(&self, v: &mut Vec<u8>) -> crate::Result<()> { |
157 | 0 | v.with_coded_output_stream(|os| self.write_to(os)) Unexecuted instantiation: <pprof::protos::profile::Profile as protobuf::message::Message>::write_to_vec::{closure#0}Unexecuted instantiation: <_ as protobuf::message::Message>::write_to_vec::{closure#0} |
158 | 0 | } Unexecuted instantiation: <pprof::protos::profile::Profile as protobuf::message::Message>::write_to_vec Unexecuted instantiation: <_ as protobuf::message::Message>::write_to_vec |
159 | | |
160 | | /// Write the message to bytes vec. |
161 | | /// |
162 | | /// > **Note**: You can use [`Message::parse_from_bytes`] |
163 | | /// to do the reverse. |
164 | 0 | fn write_to_bytes(&self) -> crate::Result<Vec<u8>> { |
165 | 0 | self.check_initialized()?; |
166 | | |
167 | 0 | let size = self.compute_size() as usize; |
168 | 0 | let mut v = Vec::with_capacity(size); |
169 | 0 | let mut os = CodedOutputStream::vec(&mut v); |
170 | 0 | self.write_to_with_cached_sizes(&mut os)?; |
171 | 0 | os.flush()?; |
172 | 0 | drop(os); |
173 | 0 | Ok(v) |
174 | 0 | } Unexecuted instantiation: <pprof::protos::profile::Profile as protobuf::message::Message>::write_to_bytes Unexecuted instantiation: <_ as protobuf::message::Message>::write_to_bytes |
175 | | |
176 | | /// Write the message to the writer, prepend the message with message length |
177 | | /// encoded as varint. |
178 | 0 | fn write_length_delimited_to_writer(&self, w: &mut dyn Write) -> crate::Result<()> { |
179 | 0 | w.with_coded_output_stream(|os| self.write_length_delimited_to(os)) |
180 | 0 | } |
181 | | |
182 | | /// Write the message to the bytes vec, prepend the message with message length |
183 | | /// encoded as varint. |
184 | 0 | fn write_length_delimited_to_bytes(&self) -> crate::Result<Vec<u8>> { |
185 | 0 | let mut v = Vec::new(); |
186 | 0 | v.with_coded_output_stream(|os| self.write_length_delimited_to(os))?; |
187 | 0 | Ok(v) |
188 | 0 | } |
189 | | |
190 | | /// Special fields (unknown fields and cached size). |
191 | | fn special_fields(&self) -> &SpecialFields; |
192 | | /// Special fields (unknown fields and cached size). |
193 | | fn mut_special_fields(&mut self) -> &mut SpecialFields; |
194 | | |
195 | | /// Get a reference to unknown fields. |
196 | 0 | fn unknown_fields(&self) -> &UnknownFields { |
197 | 0 | &self.special_fields().unknown_fields() |
198 | 0 | } |
199 | | /// Get a mutable reference to unknown fields. |
200 | 0 | fn mut_unknown_fields(&mut self) -> &mut UnknownFields { |
201 | 0 | self.mut_special_fields().mut_unknown_fields() |
202 | 0 | } |
203 | | |
204 | | /// Create an empty message object. |
205 | | /// |
206 | | /// ``` |
207 | | /// # use protobuf::MessageFull; |
208 | | /// # fn foo<MyMessage: MessageFull>() { |
209 | | /// let m = MyMessage::new(); |
210 | | /// # } |
211 | | /// ``` |
212 | | fn new() -> Self; |
213 | | |
214 | | /// Reset all fields. |
215 | 0 | fn clear(&mut self) { |
216 | 0 | *self = Self::new(); |
217 | 0 | } |
218 | | |
219 | | /// Return a pointer to default immutable message with static lifetime. |
220 | | /// |
221 | | /// ``` |
222 | | /// # use protobuf::MessageFull; |
223 | | /// # fn foo<MyMessage: MessageFull>() { |
224 | | /// let m: &MyMessage = MyMessage::default_instance(); |
225 | | /// # } |
226 | | /// ``` |
227 | | fn default_instance() -> &'static Self; |
228 | | } |