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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
use std::collections::HashMap;
use std::default::Default;
use std::marker;
use core::Message;
use core::MessageStatic;
use core::ProtobufEnum;
use descriptor::FileDescriptorProto;
use descriptor::DescriptorProto;
use descriptor::FieldDescriptorProto;
use descriptor::EnumDescriptorProto;
use descriptor::EnumValueDescriptorProto;
use descriptor::FieldDescriptorProto_Label;
use descriptorx::find_enum_by_rust_name;
use descriptorx::find_message_by_rust_name;
use reflect::accessor::FieldAccessor;
pub mod accessor;
pub struct FieldDescriptor {
proto: &'static FieldDescriptorProto,
accessor: Box<FieldAccessor + 'static>,
}
impl FieldDescriptor {
fn new(a: Box<FieldAccessor + 'static>, proto: &'static FieldDescriptorProto)
-> FieldDescriptor
{
assert_eq!(proto.get_name(), a.name_generic());
FieldDescriptor {
proto: proto,
accessor: a
}
}
pub fn proto(&self) -> &'static FieldDescriptorProto {
self.proto
}
pub fn name(&self) -> &'static str {
self.proto.get_name()
}
pub fn is_repeated(&self) -> bool {
self.proto.get_label() == FieldDescriptorProto_Label::LABEL_REPEATED
}
pub fn has_field(&self, m: &Message) -> bool {
self.accessor.has_field_generic(m)
}
pub fn len_field(&self, m: &Message) -> usize {
self.accessor.len_field_generic(m)
}
pub fn get_message<'a>(&self, m: &'a Message) -> &'a Message {
self.accessor.get_message_generic(m)
}
pub fn get_rep_message_item<'a>(&self, m: &'a Message, index: usize) -> &'a Message {
self.accessor.get_rep_message_item_generic(m, index)
}
pub fn get_enum(&self, m: &Message) -> &'static EnumValueDescriptor {
self.accessor.get_enum_generic(m)
}
pub fn get_rep_enum_item(&self, m: &Message, index: usize) -> &'static EnumValueDescriptor {
self.accessor.get_rep_enum_item_generic(m, index)
}
pub fn get_str<'a>(&self, m: &'a Message) -> &'a str {
self.accessor.get_str_generic(m)
}
pub fn get_rep_str<'a>(&self, m: &'a Message) -> &'a [String] {
self.accessor.get_rep_str_generic(m)
}
pub fn get_rep_str_item<'a>(&self, m: &'a Message, index: usize) -> &'a str {
&self.get_rep_str(m)[index]
}
pub fn get_bytes<'a>(&self, m: &'a Message) -> &'a [u8] {
self.accessor.get_bytes_generic(m)
}
pub fn get_rep_bytes<'a>(&self, m: &'a Message) -> &'a [Vec<u8>] {
self.accessor.get_rep_bytes_generic(m)
}
pub fn get_rep_bytes_item<'a>(&self, m: &'a Message, index: usize) -> &'a [u8] {
&self.get_rep_bytes(m)[index]
}
pub fn get_u32(&self, m: &Message) -> u32 {
self.accessor.get_u32_generic(m)
}
pub fn get_rep_u32<'a>(&self, m: &'a Message) -> &'a [u32] {
self.accessor.get_rep_u32_generic(m)
}
pub fn get_u64(&self, m: &Message) -> u64 {
self.accessor.get_u64_generic(m)
}
pub fn get_rep_u64<'a>(&self, m: &'a Message) -> &'a [u64] {
self.accessor.get_rep_u64_generic(m)
}
pub fn get_i32(&self, m: &Message) -> i32 {
self.accessor.get_i32_generic(m)
}
pub fn get_rep_i32<'a>(&self, m: &'a Message) -> &'a [i32] {
self.accessor.get_rep_i32_generic(m)
}
pub fn get_i64(&self, m: &Message) -> i64 {
self.accessor.get_i64_generic(m)
}
pub fn get_rep_i64<'a>(&self, m: &'a Message) -> &'a [i64] {
self.accessor.get_rep_i64_generic(m)
}
pub fn get_bool(&self, m: &Message) -> bool {
self.accessor.get_bool_generic(m)
}
pub fn get_rep_bool<'a>(&self, m: &'a Message) -> &'a [bool] {
self.accessor.get_rep_bool_generic(m)
}
pub fn get_f32(&self, m: &Message) -> f32 {
self.accessor.get_f32_generic(m)
}
pub fn get_rep_f32<'a>(&self, m: &'a Message) -> &'a [f32] {
self.accessor.get_rep_f32_generic(m)
}
pub fn get_f64(&self, m: &Message) -> f64 {
self.accessor.get_f64_generic(m)
}
pub fn get_rep_f64<'a>(&self, m: &'a Message) -> &'a [f64] {
self.accessor.get_rep_f64_generic(m)
}
}
trait MessageFactory {
fn new_instance(&self) -> Box<Message>;
}
struct MessageFactoryTyped<M> {
_dummy: (),
_phantom_data: marker::PhantomData<M>,
}
impl<M> MessageFactoryTyped<M> {
fn new() -> MessageFactoryTyped<M> {
MessageFactoryTyped {
_dummy: (),
_phantom_data: marker::PhantomData,
}
}
}
impl<M : 'static + Message + Default> MessageFactory for MessageFactoryTyped<M> {
fn new_instance(&self) -> Box<Message> {
let m: M = Default::default();
Box::new(m)
}
}
pub struct MessageDescriptor {
full_name: String,
proto: &'static DescriptorProto,
factory: Box<MessageFactory + 'static>,
fields: Vec<FieldDescriptor>,
index_by_name: HashMap<String, usize>,
index_by_number: HashMap<u32, usize>,
}
impl MessageDescriptor {
pub fn for_type<M : MessageStatic>() -> &'static MessageDescriptor {
MessageStatic::descriptor_static(None::<M>)
}
pub fn new<M : 'static + Message + Default>(
rust_name: &'static str,
fields: Vec<Box<FieldAccessor + 'static>>,
file: &'static FileDescriptorProto
) -> MessageDescriptor
{
let proto = find_message_by_rust_name(file, rust_name);
let mut field_proto_by_name = HashMap::new();
for field_proto in proto.message.get_field() {
field_proto_by_name.insert(field_proto.get_name(), field_proto);
}
let mut index_by_name = HashMap::new();
let mut index_by_number = HashMap::new();
for (i, f) in proto.message.get_field().iter().enumerate() {
index_by_number.insert(f.get_number() as u32, i);
index_by_name.insert(f.get_name().to_string(), i);
}
let mut full_name = file.get_package().to_string();
if full_name.len() > 0 {
full_name.push('.');
}
full_name.push_str(proto.message.get_name());
MessageDescriptor {
full_name: full_name,
proto: proto.message,
factory: Box::new(MessageFactoryTyped::<M>::new()),
fields: fields.into_iter()
.map(|f| {
let proto = *field_proto_by_name.get(&f.name_generic()).unwrap();
FieldDescriptor::new(f, proto)
})
.collect(),
index_by_name: index_by_name,
index_by_number: index_by_number,
}
}
pub fn new_instance(&self) -> Box<Message> {
self.factory.new_instance()
}
pub fn name(&self) -> &'static str {
self.proto.get_name()
}
pub fn full_name(&self) -> &str {
&self.full_name[..]
}
pub fn fields<'a>(&'a self) -> &'a [FieldDescriptor] {
&self.fields
}
pub fn field_by_name<'a>(&'a self, name: &str) -> &'a FieldDescriptor {
let &index = self.index_by_name.get(&name.to_string()).unwrap();
&self.fields[index]
}
pub fn field_by_number<'a>(&'a self, number: u32) -> &'a FieldDescriptor {
let &index = self.index_by_number.get(&number).unwrap();
&self.fields[index]
}
}
#[derive(Clone)]
pub struct EnumValueDescriptor {
proto: &'static EnumValueDescriptorProto,
}
impl Copy for EnumValueDescriptor {}
impl EnumValueDescriptor {
pub fn name(&self) -> &'static str {
self.proto.get_name()
}
pub fn value(&self) -> i32 {
self.proto.get_number()
}
}
pub struct EnumDescriptor {
proto: &'static EnumDescriptorProto,
values: Vec<EnumValueDescriptor>,
index_by_name: HashMap<String, usize>,
index_by_number: HashMap<i32, usize>,
}
impl EnumDescriptor {
pub fn name(&self) -> &'static str {
self.proto.get_name()
}
pub fn for_type<E : ProtobufEnum>() -> &'static EnumDescriptor {
ProtobufEnum::enum_descriptor_static(None::<E>)
}
pub fn new(rust_name: &'static str, file: &'static FileDescriptorProto) -> EnumDescriptor {
let proto = find_enum_by_rust_name(file, rust_name);
let mut index_by_name = HashMap::new();
let mut index_by_number = HashMap::new();
for (i, v) in proto.en.get_value().iter().enumerate() {
index_by_number.insert(v.get_number(), i);
index_by_name.insert(v.get_name().to_string(), i);
}
EnumDescriptor {
proto: proto.en,
values: proto.en.get_value().iter().map(|v| EnumValueDescriptor { proto: v }).collect(),
index_by_name: index_by_name,
index_by_number: index_by_number,
}
}
pub fn value_by_name<'a>(&'a self, name: &str) -> &'a EnumValueDescriptor {
let &index = self.index_by_name.get(&name.to_string()).unwrap();
&self.values[index]
}
pub fn value_by_number<'a>(&'a self, number: i32) -> &'a EnumValueDescriptor {
let &index = self.index_by_number.get(&number).unwrap();
&self.values[index]
}
}