1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use std::any::Any;
use std::any::TypeId;
use std::fmt;
use std::io::Write;
use crate::coded_output_stream::WithCodedOutputStream;
use crate::reflect::MessageDescriptor;
use crate::reflect::ReflectEqMode;
use crate::CodedInputStream;
use crate::CodedOutputStream;
use crate::Message;
use crate::ProtobufError;
use crate::ProtobufResult;
use crate::UnknownFields;
pub trait MessageDyn: Any + fmt::Debug + Send + Sync + 'static {
fn descriptor_dyn(&self) -> MessageDescriptor;
fn merge_from_dyn(&mut self, is: &mut CodedInputStream) -> ProtobufResult<()>;
fn write_to_with_cached_sizes_dyn(&self, os: &mut CodedOutputStream) -> ProtobufResult<()>;
fn compute_size_dyn(&self) -> u32;
fn is_initialized_dyn(&self) -> bool;
fn get_unknown_fields_dyn(&self) -> &UnknownFields;
fn mut_unknown_fields_dyn(&mut self) -> &mut UnknownFields;
}
impl<M: Message> MessageDyn for M {
fn descriptor_dyn(&self) -> MessageDescriptor {
self.descriptor_by_instance()
}
fn merge_from_dyn(&mut self, is: &mut CodedInputStream) -> ProtobufResult<()> {
self.merge_from(is)
}
fn write_to_with_cached_sizes_dyn(&self, os: &mut CodedOutputStream) -> ProtobufResult<()> {
self.write_to_with_cached_sizes(os)
}
fn compute_size_dyn(&self) -> u32 {
self.compute_size()
}
fn is_initialized_dyn(&self) -> bool {
self.is_initialized()
}
fn get_unknown_fields_dyn(&self) -> &UnknownFields {
self.get_unknown_fields()
}
fn mut_unknown_fields_dyn(&mut self) -> &mut UnknownFields {
self.mut_unknown_fields()
}
}
impl dyn MessageDyn {
pub fn check_initialized_dyn(&self) -> ProtobufResult<()> {
if !self.is_initialized_dyn() {
Err(ProtobufError::MessageNotInitialized(
self.descriptor_dyn().name().to_owned(),
))
} else {
Ok(())
}
}
pub fn write_to_writer_dyn(&self, w: &mut dyn Write) -> ProtobufResult<()> {
w.with_coded_output_stream(|os| self.write_to_dyn(os))
}
pub fn write_to_vec_dyn(&self, v: &mut Vec<u8>) -> ProtobufResult<()> {
v.with_coded_output_stream(|os| self.write_to_dyn(os))
}
pub fn write_to_dyn(&self, os: &mut CodedOutputStream) -> ProtobufResult<()> {
self.check_initialized_dyn()?;
self.compute_size_dyn();
self.write_to_with_cached_sizes_dyn(os)?;
Ok(())
}
pub fn write_length_delimited_to_vec_dyn(&self, vec: &mut Vec<u8>) -> ProtobufResult<()> {
let mut os = CodedOutputStream::vec(vec);
self.write_length_delimited_to_dyn(&mut os)?;
os.flush()?;
Ok(())
}
pub fn merge_from_bytes_dyn(&mut self, bytes: &[u8]) -> ProtobufResult<()> {
let mut is = CodedInputStream::from_bytes(bytes);
self.merge_from_dyn(&mut is)
}
pub fn write_to_bytes_dyn(&self) -> ProtobufResult<Vec<u8>> {
self.check_initialized_dyn()?;
let size = self.compute_size_dyn() as usize;
let mut v = Vec::with_capacity(size);
unsafe {
v.set_len(size);
}
{
let mut os = CodedOutputStream::bytes(&mut v);
self.write_to_with_cached_sizes_dyn(&mut os)?;
os.check_eof();
}
Ok(v)
}
pub fn write_length_delimited_to_dyn(&self, os: &mut CodedOutputStream) -> ProtobufResult<()> {
let size = self.compute_size_dyn();
os.write_raw_varint32(size)?;
self.write_to_with_cached_sizes_dyn(os)?;
Ok(())
}
pub fn write_length_delimited_to_writer_dyn(&self, w: &mut dyn Write) -> ProtobufResult<()> {
w.with_coded_output_stream(|os| self.write_length_delimited_to_dyn(os))
}
pub fn write_length_delimited_to_bytes_dyn(&self) -> ProtobufResult<Vec<u8>> {
let mut v = Vec::new();
v.with_coded_output_stream(|os| self.write_length_delimited_to_dyn(os))?;
Ok(v)
}
pub fn downcast_box<T: Any>(self: Box<dyn MessageDyn>) -> Result<Box<T>, Box<dyn MessageDyn>> {
if Any::type_id(&*self) == TypeId::of::<T>() {
unsafe {
let raw: *mut dyn MessageDyn = Box::into_raw(self);
Ok(Box::from_raw(raw as *mut T))
}
} else {
Err(self)
}
}
pub fn downcast_ref<'a, M: Message + 'a>(&'a self) -> Option<&'a M> {
if Any::type_id(&*self) == TypeId::of::<M>() {
unsafe { Some(&*(self as *const dyn MessageDyn as *const M)) }
} else {
None
}
}
pub fn downcast_mut<'a, M: Message + 'a>(&'a mut self) -> Option<&'a mut M> {
if Any::type_id(&*self) == TypeId::of::<M>() {
unsafe { Some(&mut *(self as *mut dyn MessageDyn as *mut M)) }
} else {
None
}
}
pub fn clone_box(&self) -> Box<dyn MessageDyn> {
self.descriptor_dyn().clone_message(self)
}
pub fn reflect_eq_dyn(&self, other: &dyn MessageDyn, mode: &ReflectEqMode) -> bool {
MessageDescriptor::reflect_eq_maybe_unrelated(self, other, mode)
}
}
impl Clone for Box<dyn MessageDyn> {
fn clone(&self) -> Self {
(*self).clone_box()
}
}
impl PartialEq for Box<dyn MessageDyn> {
fn eq(&self, other: &Box<dyn MessageDyn>) -> bool {
MessageDescriptor::reflect_eq_maybe_unrelated(&**self, &**other, &ReflectEqMode::default())
}
}
#[cfg(test)]
mod test {
use crate::descriptor::FileDescriptorProto;
use crate::MessageDyn;
#[test]
fn downcast_ref() {
let m = FileDescriptorProto::new();
let d = &m as &dyn MessageDyn;
let c: &FileDescriptorProto = d.downcast_ref().unwrap();
assert_eq!(
c as *const FileDescriptorProto,
&m as *const FileDescriptorProto
);
}
#[test]
fn downcast_mut() {
let mut m = FileDescriptorProto::new();
let d = &mut m as &mut dyn MessageDyn;
let c: &mut FileDescriptorProto = d.downcast_mut().unwrap();
assert_eq!(
c as *const FileDescriptorProto,
&m as *const FileDescriptorProto
);
}
#[test]
fn downcast_box() {
let m = FileDescriptorProto::new();
let d: Box<dyn MessageDyn> = Box::new(m);
let mut _c: Box<FileDescriptorProto> = d.downcast_box().unwrap();
}
}