1 #![doc(hidden)]
2
3 //! Version 1 reflection accessors.
4
5 use std::collections::HashMap;
6 use std::fmt;
7 use std::hash::Hash;
8
9 use crate::enums::ProtobufEnum;
10 use crate::message::message_down_cast;
11 use crate::message::Message;
12 use crate::reflect::EnumValueDescriptor;
13 use crate::reflect::ProtobufValue;
14 use crate::reflect::ReflectFieldRef;
15 use crate::reflect::ReflectValueRef;
16 use crate::types::*;
17
18 use crate::reflect::map::ReflectMap;
19 use crate::reflect::optional::ReflectOptional;
20 use crate::reflect::repeated::ReflectRepeated;
21 use crate::reflect::repeated::ReflectRepeatedEnum;
22 use crate::reflect::repeated::ReflectRepeatedMessage;
23 use crate::reflect::rt::FieldAccessor;
24 use crate::repeated::RepeatedField;
25 use crate::singular::SingularField;
26 use crate::singular::SingularPtrField;
27
28 /// this trait should not be used directly, use `FieldDescriptor` instead
29 pub trait FieldAccessorTrait: Sync + 'static {
has_field_generic(&self, m: &dyn Message) -> bool30 fn has_field_generic(&self, m: &dyn Message) -> bool;
len_field_generic(&self, m: &dyn Message) -> usize31 fn len_field_generic(&self, m: &dyn Message) -> usize;
32 // TODO: should it return default value or panic on unset field?
get_message_generic<'a>(&self, m: &'a dyn Message) -> &'a dyn Message33 fn get_message_generic<'a>(&self, m: &'a dyn Message) -> &'a dyn Message;
get_enum_generic(&self, m: &dyn Message) -> &'static EnumValueDescriptor34 fn get_enum_generic(&self, m: &dyn Message) -> &'static EnumValueDescriptor;
get_str_generic<'a>(&self, m: &'a dyn Message) -> &'a str35 fn get_str_generic<'a>(&self, m: &'a dyn Message) -> &'a str;
get_bytes_generic<'a>(&self, m: &'a dyn Message) -> &'a [u8]36 fn get_bytes_generic<'a>(&self, m: &'a dyn Message) -> &'a [u8];
get_u32_generic(&self, m: &dyn Message) -> u3237 fn get_u32_generic(&self, m: &dyn Message) -> u32;
get_u64_generic(&self, m: &dyn Message) -> u6438 fn get_u64_generic(&self, m: &dyn Message) -> u64;
get_i32_generic(&self, m: &dyn Message) -> i3239 fn get_i32_generic(&self, m: &dyn Message) -> i32;
get_i64_generic(&self, m: &dyn Message) -> i6440 fn get_i64_generic(&self, m: &dyn Message) -> i64;
get_bool_generic(&self, m: &dyn Message) -> bool41 fn get_bool_generic(&self, m: &dyn Message) -> bool;
get_f32_generic(&self, m: &dyn Message) -> f3242 fn get_f32_generic(&self, m: &dyn Message) -> f32;
get_f64_generic(&self, m: &dyn Message) -> f6443 fn get_f64_generic(&self, m: &dyn Message) -> f64;
44
get_reflect<'a>(&self, m: &'a dyn Message) -> ReflectFieldRef<'a>45 fn get_reflect<'a>(&self, m: &'a dyn Message) -> ReflectFieldRef<'a>;
46 }
47
48 pub(crate) trait GetSingularMessage<M>: Sync {
get_message<'a>(&self, m: &'a M) -> &'a dyn Message49 fn get_message<'a>(&self, m: &'a M) -> &'a dyn Message;
50 }
51
52 struct GetSingularMessageImpl<M, N> {
53 get: for<'a> fn(&'a M) -> &'a N,
54 }
55
56 impl<M: Message, N: Message + 'static> GetSingularMessage<M> for GetSingularMessageImpl<M, N> {
get_message<'a>(&self, m: &'a M) -> &'a dyn Message57 fn get_message<'a>(&self, m: &'a M) -> &'a dyn Message {
58 (self.get)(m)
59 }
60 }
61
62 pub(crate) trait GetSingularEnum<M>: Sync {
get_enum(&self, m: &M) -> &'static EnumValueDescriptor63 fn get_enum(&self, m: &M) -> &'static EnumValueDescriptor;
64 }
65
66 struct GetSingularEnumImpl<M, E> {
67 get: fn(&M) -> E,
68 }
69
70 impl<M: Message, E: ProtobufEnum> GetSingularEnum<M> for GetSingularEnumImpl<M, E> {
get_enum(&self, m: &M) -> &'static EnumValueDescriptor71 fn get_enum(&self, m: &M) -> &'static EnumValueDescriptor {
72 (self.get)(m).descriptor()
73 }
74 }
75
76 trait GetRepeatedMessage<M>: Sync {
len_field(&self, m: &M) -> usize77 fn len_field(&self, m: &M) -> usize;
get_message_item<'a>(&self, m: &'a M, index: usize) -> &'a dyn Message78 fn get_message_item<'a>(&self, m: &'a M, index: usize) -> &'a dyn Message;
reflect_repeated_message<'a>(&self, m: &'a M) -> Box<dyn ReflectRepeatedMessage<'a> + 'a>79 fn reflect_repeated_message<'a>(&self, m: &'a M) -> Box<dyn ReflectRepeatedMessage<'a> + 'a>;
80 }
81
82 trait GetRepeatedEnum<M: Message + 'static>: Sync {
len_field(&self, m: &M) -> usize83 fn len_field(&self, m: &M) -> usize;
get_enum_item(&self, m: &M, index: usize) -> &'static EnumValueDescriptor84 fn get_enum_item(&self, m: &M, index: usize) -> &'static EnumValueDescriptor;
reflect_repeated_enum<'a>(&self, m: &'a M) -> Box<dyn ReflectRepeatedEnum<'a> + 'a>85 fn reflect_repeated_enum<'a>(&self, m: &'a M) -> Box<dyn ReflectRepeatedEnum<'a> + 'a>;
86 }
87
88 pub(crate) trait GetSetCopyFns<M>: Sync {
get_field<'a>(&self, m: &'a M) -> ReflectValueRef<'a>89 fn get_field<'a>(&self, m: &'a M) -> ReflectValueRef<'a>;
90 }
91
92 struct GetSetCopyFnsImpl<M, V: ProtobufValue + Copy> {
93 get: fn(&M) -> V,
94 _set: fn(&mut M, V),
95 }
96
97 impl<M, V: ProtobufValue + Copy> GetSetCopyFns<M> for GetSetCopyFnsImpl<M, V> {
get_field<'a>(&self, m: &'a M) -> ReflectValueRef<'a>98 fn get_field<'a>(&self, m: &'a M) -> ReflectValueRef<'a> {
99 (&(self.get)(m) as &dyn ProtobufValue).as_ref_copy()
100 }
101 }
102
103 pub(crate) enum SingularGetSet<M> {
104 Copy(Box<dyn GetSetCopyFns<M>>),
105 String(for<'a> fn(&'a M) -> &'a str, fn(&mut M, String)),
106 Bytes(for<'a> fn(&'a M) -> &'a [u8], fn(&mut M, Vec<u8>)),
107 Enum(Box<dyn GetSingularEnum<M> + 'static>),
108 Message(Box<dyn GetSingularMessage<M> + 'static>),
109 }
110
111 impl<M: Message + 'static> SingularGetSet<M> {
get_ref<'a>(&self, m: &'a M) -> ReflectValueRef<'a>112 fn get_ref<'a>(&self, m: &'a M) -> ReflectValueRef<'a> {
113 match self {
114 &SingularGetSet::Copy(ref copy) => copy.get_field(m),
115 &SingularGetSet::String(get, _) => ReflectValueRef::String(get(m)),
116 &SingularGetSet::Bytes(get, _) => ReflectValueRef::Bytes(get(m)),
117 &SingularGetSet::Enum(ref get) => ReflectValueRef::Enum(get.get_enum(m)),
118 &SingularGetSet::Message(ref get) => ReflectValueRef::Message(get.get_message(m)),
119 }
120 }
121 }
122
123 pub(crate) trait FieldAccessor2<M, R: ?Sized>: Sync
124 where
125 M: Message + 'static,
126 {
get_field<'a>(&self, _: &'a M) -> &'a R127 fn get_field<'a>(&self, _: &'a M) -> &'a R;
mut_field<'a>(&self, _: &'a mut M) -> &'a mut R128 fn mut_field<'a>(&self, _: &'a mut M) -> &'a mut R;
129 }
130
131 struct MessageGetMut<M, L>
132 where
133 M: Message + 'static,
134 {
135 get_field: for<'a> fn(&'a M) -> &'a L,
136 mut_field: for<'a> fn(&'a mut M) -> &'a mut L,
137 }
138
139 pub(crate) enum FieldAccessorFunctions<M> {
140 // up to 1.0.24 optional or required
141 SingularHasGetSet {
142 has: fn(&M) -> bool,
143 get_set: SingularGetSet<M>,
144 },
145 // protobuf 3 simple field
146 Simple(Box<dyn FieldAccessor2<M, dyn ProtobufValue>>),
147 // optional, required or message
148 Optional(Box<dyn FieldAccessor2<M, dyn ReflectOptional>>),
149 // repeated
150 Repeated(Box<dyn FieldAccessor2<M, dyn ReflectRepeated>>),
151 // protobuf 3 map
152 Map(Box<dyn FieldAccessor2<M, dyn ReflectMap>>),
153 }
154
155 impl<M> fmt::Debug for FieldAccessorFunctions<M> {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result156 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
157 match self {
158 &FieldAccessorFunctions::SingularHasGetSet { .. } => {
159 write!(f, "SingularHasGetSet {{ .. }}")
160 }
161 &FieldAccessorFunctions::Simple(..) => write!(f, "Simple(..)"),
162 &FieldAccessorFunctions::Optional(..) => write!(f, "Optional(..)"),
163 &FieldAccessorFunctions::Repeated(..) => write!(f, "Repeated(..)"),
164 &FieldAccessorFunctions::Map(..) => write!(f, "Map(..)"),
165 }
166 }
167 }
168
169 pub(crate) struct FieldAccessorImpl<M> {
170 pub(crate) fns: FieldAccessorFunctions<M>,
171 }
172
173 impl<M: Message> FieldAccessorImpl<M> {
get_value_option<'a>(&self, m: &'a M) -> Option<ReflectValueRef<'a>>174 fn get_value_option<'a>(&self, m: &'a M) -> Option<ReflectValueRef<'a>> {
175 match self.fns {
176 FieldAccessorFunctions::Repeated(..) | FieldAccessorFunctions::Map(..) => {
177 panic!("repeated")
178 }
179 FieldAccessorFunctions::Simple(ref a) => Some(a.get_field(m).as_ref()),
180 FieldAccessorFunctions::Optional(ref a) => {
181 a.get_field(m).to_option().map(|v| v.as_ref())
182 }
183 FieldAccessorFunctions::SingularHasGetSet {
184 ref has,
185 ref get_set,
186 } => {
187 if !has(m) {
188 None
189 } else {
190 Some(get_set.get_ref(m))
191 }
192 }
193 }
194 }
195 }
196
197 impl<M: Message + 'static> FieldAccessorTrait for FieldAccessorImpl<M> {
has_field_generic(&self, m: &dyn Message) -> bool198 fn has_field_generic(&self, m: &dyn Message) -> bool {
199 match self.fns {
200 FieldAccessorFunctions::SingularHasGetSet { has, .. } => has(message_down_cast(m)),
201 FieldAccessorFunctions::Optional(ref a) => {
202 a.get_field(message_down_cast(m)).to_option().is_some()
203 }
204 FieldAccessorFunctions::Simple(ref a) => {
205 a.get_field(message_down_cast(m)).is_non_zero()
206 }
207 FieldAccessorFunctions::Map(..) | FieldAccessorFunctions::Repeated(..) => {
208 panic!("has_xxx is not implemented for repeated");
209 }
210 }
211 }
212
len_field_generic(&self, m: &dyn Message) -> usize213 fn len_field_generic(&self, m: &dyn Message) -> usize {
214 match self.fns {
215 FieldAccessorFunctions::Repeated(ref a) => a.get_field(message_down_cast(m)).len(),
216 FieldAccessorFunctions::Map(ref a) => a.get_field(message_down_cast(m)).len(),
217 FieldAccessorFunctions::Simple(..)
218 | FieldAccessorFunctions::SingularHasGetSet { .. }
219 | FieldAccessorFunctions::Optional(..) => {
220 panic!("not a repeated field");
221 }
222 }
223 }
224
get_message_generic<'a>(&self, m: &'a dyn Message) -> &'a dyn Message225 fn get_message_generic<'a>(&self, m: &'a dyn Message) -> &'a dyn Message {
226 match self.fns {
227 FieldAccessorFunctions::SingularHasGetSet {
228 get_set: SingularGetSet::Message(ref get),
229 ..
230 } => get.get_message(message_down_cast(m)),
231 FieldAccessorFunctions::Optional(ref t) => {
232 match t
233 .get_field(message_down_cast(m))
234 .to_option()
235 .expect("field unset")
236 .as_ref()
237 {
238 ReflectValueRef::Message(m) => m,
239 _ => panic!("not a message"),
240 }
241 }
242 ref fns => panic!("unknown accessor type: {:?}", fns),
243 }
244 }
245
get_enum_generic(&self, m: &dyn Message) -> &'static EnumValueDescriptor246 fn get_enum_generic(&self, m: &dyn Message) -> &'static EnumValueDescriptor {
247 match self.fns {
248 FieldAccessorFunctions::SingularHasGetSet {
249 get_set: SingularGetSet::Enum(ref get),
250 ..
251 } => get.get_enum(message_down_cast(m)),
252 _ => panic!(),
253 }
254 }
255
get_str_generic<'a>(&self, m: &'a dyn Message) -> &'a str256 fn get_str_generic<'a>(&self, m: &'a dyn Message) -> &'a str {
257 match self.get_value_option(message_down_cast(m)) {
258 Some(ReflectValueRef::String(v)) => v,
259 Some(_) => panic!("wrong type"),
260 None => "", // TODO: check type
261 }
262 }
263
get_bytes_generic<'a>(&self, m: &'a dyn Message) -> &'a [u8]264 fn get_bytes_generic<'a>(&self, m: &'a dyn Message) -> &'a [u8] {
265 match self.get_value_option(message_down_cast(m)) {
266 Some(ReflectValueRef::Bytes(v)) => v,
267 Some(_) => panic!("wrong type"),
268 None => b"", // TODO: check type
269 }
270 }
271
get_u32_generic(&self, m: &dyn Message) -> u32272 fn get_u32_generic(&self, m: &dyn Message) -> u32 {
273 match self.get_value_option(message_down_cast(m)) {
274 Some(ReflectValueRef::U32(v)) => v,
275 Some(_) => panic!("wrong type"),
276 None => 0, // TODO: check type
277 }
278 }
279
get_u64_generic(&self, m: &dyn Message) -> u64280 fn get_u64_generic(&self, m: &dyn Message) -> u64 {
281 match self.get_value_option(message_down_cast(m)) {
282 Some(ReflectValueRef::U64(v)) => v,
283 Some(_) => panic!("wrong type"),
284 None => 0, // TODO: check type
285 }
286 }
287
get_i32_generic(&self, m: &dyn Message) -> i32288 fn get_i32_generic(&self, m: &dyn Message) -> i32 {
289 match self.get_value_option(message_down_cast(m)) {
290 Some(ReflectValueRef::I32(v)) => v,
291 Some(_) => panic!("wrong type"),
292 None => 0, // TODO: check type
293 }
294 }
295
get_i64_generic(&self, m: &dyn Message) -> i64296 fn get_i64_generic(&self, m: &dyn Message) -> i64 {
297 match self.get_value_option(message_down_cast(m)) {
298 Some(ReflectValueRef::I64(v)) => v,
299 Some(_) => panic!("wrong type"),
300 None => 0, // TODO: check type
301 }
302 }
303
get_bool_generic(&self, m: &dyn Message) -> bool304 fn get_bool_generic(&self, m: &dyn Message) -> bool {
305 match self.get_value_option(message_down_cast(m)) {
306 Some(ReflectValueRef::Bool(v)) => v,
307 Some(_) => panic!("wrong type"),
308 None => false, // TODO: check type
309 }
310 }
311
get_f32_generic(&self, m: &dyn Message) -> f32312 fn get_f32_generic(&self, m: &dyn Message) -> f32 {
313 match self.get_value_option(message_down_cast(m)) {
314 Some(ReflectValueRef::F32(v)) => v,
315 Some(_) => panic!("wrong type"),
316 None => 0.0, // TODO: check type
317 }
318 }
319
get_f64_generic(&self, m: &dyn Message) -> f64320 fn get_f64_generic(&self, m: &dyn Message) -> f64 {
321 match self.get_value_option(message_down_cast(m)) {
322 Some(ReflectValueRef::F64(v)) => v,
323 Some(_) => panic!("wrong type"),
324 None => 0.0, // TODO: check type
325 }
326 }
327
get_reflect<'a>(&self, m: &'a dyn Message) -> ReflectFieldRef<'a>328 fn get_reflect<'a>(&self, m: &'a dyn Message) -> ReflectFieldRef<'a> {
329 match self.fns {
330 FieldAccessorFunctions::Repeated(ref accessor2) => {
331 ReflectFieldRef::Repeated(accessor2.get_field(message_down_cast(m)))
332 }
333 FieldAccessorFunctions::Map(ref accessor2) => {
334 ReflectFieldRef::Map(accessor2.get_field(message_down_cast(m)))
335 }
336 FieldAccessorFunctions::Optional(ref accessor2) => ReflectFieldRef::Optional(
337 accessor2
338 .get_field(message_down_cast(m))
339 .to_option()
340 .map(|v| v.as_ref()),
341 ),
342 FieldAccessorFunctions::Simple(ref accessor2) => ReflectFieldRef::Optional({
343 let v = accessor2.get_field(message_down_cast(m));
344 if v.is_non_zero() {
345 Some(v.as_ref())
346 } else {
347 None
348 }
349 }),
350 FieldAccessorFunctions::SingularHasGetSet {
351 ref has,
352 ref get_set,
353 } => ReflectFieldRef::Optional(if has(message_down_cast(m)) {
354 Some(get_set.get_ref(message_down_cast(m)))
355 } else {
356 None
357 }),
358 }
359 }
360 }
361
362 // singular
363
set_panic<A, B>(_: &mut A, _: B)364 fn set_panic<A, B>(_: &mut A, _: B) {
365 panic!()
366 }
367
368 // TODO: make_singular_xxx_accessor are used only for oneof fields
369 // oneof codegen should be changed
370
make_singular_u32_accessor<M: Message + 'static>( name: &'static str, has: fn(&M) -> bool, get: fn(&M) -> u32, ) -> FieldAccessor371 pub fn make_singular_u32_accessor<M: Message + 'static>(
372 name: &'static str,
373 has: fn(&M) -> bool,
374 get: fn(&M) -> u32,
375 ) -> FieldAccessor {
376 FieldAccessor::new_v1(
377 name,
378 FieldAccessorFunctions::SingularHasGetSet {
379 has,
380 get_set: SingularGetSet::Copy(Box::new(GetSetCopyFnsImpl {
381 get,
382 _set: set_panic,
383 })),
384 },
385 )
386 }
387
make_singular_i32_accessor<M: Message + 'static>( name: &'static str, has: fn(&M) -> bool, get: fn(&M) -> i32, ) -> FieldAccessor388 pub fn make_singular_i32_accessor<M: Message + 'static>(
389 name: &'static str,
390 has: fn(&M) -> bool,
391 get: fn(&M) -> i32,
392 ) -> FieldAccessor {
393 FieldAccessor::new_v1(
394 name,
395 FieldAccessorFunctions::SingularHasGetSet {
396 has,
397 get_set: SingularGetSet::Copy(Box::new(GetSetCopyFnsImpl {
398 get,
399 _set: set_panic,
400 })),
401 },
402 )
403 }
404
make_singular_u64_accessor<M: Message + 'static>( name: &'static str, has: fn(&M) -> bool, get: fn(&M) -> u64, ) -> FieldAccessor405 pub fn make_singular_u64_accessor<M: Message + 'static>(
406 name: &'static str,
407 has: fn(&M) -> bool,
408 get: fn(&M) -> u64,
409 ) -> FieldAccessor {
410 FieldAccessor::new_v1(
411 name,
412 FieldAccessorFunctions::SingularHasGetSet {
413 has,
414 get_set: SingularGetSet::Copy(Box::new(GetSetCopyFnsImpl {
415 get,
416 _set: set_panic,
417 })),
418 },
419 )
420 }
421
make_singular_i64_accessor<M: Message + 'static>( name: &'static str, has: fn(&M) -> bool, get: fn(&M) -> i64, ) -> FieldAccessor422 pub fn make_singular_i64_accessor<M: Message + 'static>(
423 name: &'static str,
424 has: fn(&M) -> bool,
425 get: fn(&M) -> i64,
426 ) -> FieldAccessor {
427 FieldAccessor::new_v1(
428 name,
429 FieldAccessorFunctions::SingularHasGetSet {
430 has,
431 get_set: SingularGetSet::Copy(Box::new(GetSetCopyFnsImpl {
432 get,
433 _set: set_panic,
434 })),
435 },
436 )
437 }
438
make_singular_f32_accessor<M: Message + 'static>( name: &'static str, has: fn(&M) -> bool, get: fn(&M) -> f32, ) -> FieldAccessor439 pub fn make_singular_f32_accessor<M: Message + 'static>(
440 name: &'static str,
441 has: fn(&M) -> bool,
442 get: fn(&M) -> f32,
443 ) -> FieldAccessor {
444 FieldAccessor::new_v1(
445 name,
446 FieldAccessorFunctions::SingularHasGetSet {
447 has,
448 get_set: SingularGetSet::Copy(Box::new(GetSetCopyFnsImpl {
449 get,
450 _set: set_panic,
451 })),
452 },
453 )
454 }
455
make_singular_f64_accessor<M: Message + 'static>( name: &'static str, has: fn(&M) -> bool, get: fn(&M) -> f64, ) -> FieldAccessor456 pub fn make_singular_f64_accessor<M: Message + 'static>(
457 name: &'static str,
458 has: fn(&M) -> bool,
459 get: fn(&M) -> f64,
460 ) -> FieldAccessor {
461 FieldAccessor::new_v1(
462 name,
463 FieldAccessorFunctions::SingularHasGetSet {
464 has,
465 get_set: SingularGetSet::Copy(Box::new(GetSetCopyFnsImpl {
466 get,
467 _set: set_panic,
468 })),
469 },
470 )
471 }
472
make_singular_bool_accessor<M: Message + 'static>( name: &'static str, has: fn(&M) -> bool, get: fn(&M) -> bool, ) -> FieldAccessor473 pub fn make_singular_bool_accessor<M: Message + 'static>(
474 name: &'static str,
475 has: fn(&M) -> bool,
476 get: fn(&M) -> bool,
477 ) -> FieldAccessor {
478 FieldAccessor::new_v1(
479 name,
480 FieldAccessorFunctions::SingularHasGetSet {
481 has,
482 get_set: SingularGetSet::Copy(Box::new(GetSetCopyFnsImpl {
483 get,
484 _set: set_panic,
485 })),
486 },
487 )
488 }
489
make_singular_enum_accessor<M: Message + 'static, E: ProtobufEnum + 'static>( name: &'static str, has: fn(&M) -> bool, get: fn(&M) -> E, ) -> FieldAccessor490 pub fn make_singular_enum_accessor<M: Message + 'static, E: ProtobufEnum + 'static>(
491 name: &'static str,
492 has: fn(&M) -> bool,
493 get: fn(&M) -> E,
494 ) -> FieldAccessor {
495 FieldAccessor::new_v1(
496 name,
497 FieldAccessorFunctions::SingularHasGetSet {
498 has,
499 get_set: SingularGetSet::Enum(Box::new(GetSingularEnumImpl { get })),
500 },
501 )
502 }
503
make_singular_string_accessor<M: Message + 'static>( name: &'static str, has: fn(&M) -> bool, get: for<'a> fn(&'a M) -> &'a str, ) -> FieldAccessor504 pub fn make_singular_string_accessor<M: Message + 'static>(
505 name: &'static str,
506 has: fn(&M) -> bool,
507 get: for<'a> fn(&'a M) -> &'a str,
508 ) -> FieldAccessor {
509 FieldAccessor::new_v1(
510 name,
511 FieldAccessorFunctions::SingularHasGetSet {
512 has,
513 get_set: SingularGetSet::String(get, set_panic),
514 },
515 )
516 }
517
make_singular_bytes_accessor<M: Message + 'static>( name: &'static str, has: fn(&M) -> bool, get: for<'a> fn(&'a M) -> &'a [u8], ) -> FieldAccessor518 pub fn make_singular_bytes_accessor<M: Message + 'static>(
519 name: &'static str,
520 has: fn(&M) -> bool,
521 get: for<'a> fn(&'a M) -> &'a [u8],
522 ) -> FieldAccessor {
523 FieldAccessor::new_v1(
524 name,
525 FieldAccessorFunctions::SingularHasGetSet {
526 has,
527 get_set: SingularGetSet::Bytes(get, set_panic),
528 },
529 )
530 }
531
make_singular_message_accessor<M: Message + 'static, F: Message + 'static>( name: &'static str, has: fn(&M) -> bool, get: for<'a> fn(&'a M) -> &'a F, ) -> FieldAccessor532 pub fn make_singular_message_accessor<M: Message + 'static, F: Message + 'static>(
533 name: &'static str,
534 has: fn(&M) -> bool,
535 get: for<'a> fn(&'a M) -> &'a F,
536 ) -> FieldAccessor {
537 FieldAccessor::new_v1(
538 name,
539 FieldAccessorFunctions::SingularHasGetSet {
540 has,
541 get_set: SingularGetSet::Message(Box::new(GetSingularMessageImpl { get })),
542 },
543 )
544 }
545
546 // repeated
547
548 impl<M, V> FieldAccessor2<M, dyn ReflectRepeated> for MessageGetMut<M, Vec<V>>
549 where
550 M: Message + 'static,
551 V: ProtobufValue + 'static,
552 {
get_field<'a>(&self, m: &'a M) -> &'a dyn ReflectRepeated553 fn get_field<'a>(&self, m: &'a M) -> &'a dyn ReflectRepeated {
554 (self.get_field)(m) as &dyn ReflectRepeated
555 }
556
mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ReflectRepeated557 fn mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ReflectRepeated {
558 (self.mut_field)(m) as &mut dyn ReflectRepeated
559 }
560 }
561
make_vec_accessor<M, V>( name: &'static str, get_vec: for<'a> fn(&'a M) -> &'a Vec<V::Value>, mut_vec: for<'a> fn(&'a mut M) -> &'a mut Vec<V::Value>, ) -> FieldAccessor where M: Message + 'static, V: ProtobufType + 'static,562 pub fn make_vec_accessor<M, V>(
563 name: &'static str,
564 get_vec: for<'a> fn(&'a M) -> &'a Vec<V::Value>,
565 mut_vec: for<'a> fn(&'a mut M) -> &'a mut Vec<V::Value>,
566 ) -> FieldAccessor
567 where
568 M: Message + 'static,
569 V: ProtobufType + 'static,
570 {
571 FieldAccessor::new_v1(
572 name,
573 FieldAccessorFunctions::Repeated(Box::new(MessageGetMut::<M, Vec<V::Value>> {
574 get_field: get_vec,
575 mut_field: mut_vec,
576 })),
577 )
578 }
579
580 impl<M, V> FieldAccessor2<M, dyn ReflectRepeated> for MessageGetMut<M, RepeatedField<V>>
581 where
582 M: Message + 'static,
583 V: ProtobufValue + 'static,
584 {
get_field<'a>(&self, m: &'a M) -> &'a dyn ReflectRepeated585 fn get_field<'a>(&self, m: &'a M) -> &'a dyn ReflectRepeated {
586 (self.get_field)(m) as &dyn ReflectRepeated
587 }
588
mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ReflectRepeated589 fn mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ReflectRepeated {
590 (self.mut_field)(m) as &mut dyn ReflectRepeated
591 }
592 }
593
make_repeated_field_accessor<M, V>( name: &'static str, get_vec: for<'a> fn(&'a M) -> &'a RepeatedField<V::Value>, mut_vec: for<'a> fn(&'a mut M) -> &'a mut RepeatedField<V::Value>, ) -> FieldAccessor where M: Message + 'static, V: ProtobufType + 'static,594 pub fn make_repeated_field_accessor<M, V>(
595 name: &'static str,
596 get_vec: for<'a> fn(&'a M) -> &'a RepeatedField<V::Value>,
597 mut_vec: for<'a> fn(&'a mut M) -> &'a mut RepeatedField<V::Value>,
598 ) -> FieldAccessor
599 where
600 M: Message + 'static,
601 V: ProtobufType + 'static,
602 {
603 FieldAccessor::new_v1(
604 name,
605 FieldAccessorFunctions::Repeated(Box::new(MessageGetMut::<M, RepeatedField<V::Value>> {
606 get_field: get_vec,
607 mut_field: mut_vec,
608 })),
609 )
610 }
611
612 impl<M, V> FieldAccessor2<M, dyn ReflectOptional> for MessageGetMut<M, Option<V>>
613 where
614 M: Message + 'static,
615 V: ProtobufValue + Clone + 'static,
616 {
get_field<'a>(&self, m: &'a M) -> &'a dyn ReflectOptional617 fn get_field<'a>(&self, m: &'a M) -> &'a dyn ReflectOptional {
618 (self.get_field)(m) as &dyn ReflectOptional
619 }
620
mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ReflectOptional621 fn mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ReflectOptional {
622 (self.mut_field)(m) as &mut dyn ReflectOptional
623 }
624 }
625
make_option_accessor<M, V>( name: &'static str, get_field: for<'a> fn(&'a M) -> &'a Option<V::Value>, mut_field: for<'a> fn(&'a mut M) -> &'a mut Option<V::Value>, ) -> FieldAccessor where M: Message + 'static, V: ProtobufType + 'static,626 pub fn make_option_accessor<M, V>(
627 name: &'static str,
628 get_field: for<'a> fn(&'a M) -> &'a Option<V::Value>,
629 mut_field: for<'a> fn(&'a mut M) -> &'a mut Option<V::Value>,
630 ) -> FieldAccessor
631 where
632 M: Message + 'static,
633 V: ProtobufType + 'static,
634 {
635 FieldAccessor::new_v1(
636 name,
637 FieldAccessorFunctions::Optional(Box::new(MessageGetMut::<M, Option<V::Value>> {
638 get_field,
639 mut_field,
640 })),
641 )
642 }
643
644 impl<M, V> FieldAccessor2<M, dyn ReflectOptional> for MessageGetMut<M, SingularField<V>>
645 where
646 M: Message + 'static,
647 V: ProtobufValue + Clone + 'static,
648 {
get_field<'a>(&self, m: &'a M) -> &'a dyn ReflectOptional649 fn get_field<'a>(&self, m: &'a M) -> &'a dyn ReflectOptional {
650 (self.get_field)(m) as &dyn ReflectOptional
651 }
652
mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ReflectOptional653 fn mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ReflectOptional {
654 (self.mut_field)(m) as &mut dyn ReflectOptional
655 }
656 }
657
make_singular_field_accessor<M, V>( name: &'static str, get_field: for<'a> fn(&'a M) -> &'a SingularField<V::Value>, mut_field: for<'a> fn(&'a mut M) -> &'a mut SingularField<V::Value>, ) -> FieldAccessor where M: Message + 'static, V: ProtobufType + 'static,658 pub fn make_singular_field_accessor<M, V>(
659 name: &'static str,
660 get_field: for<'a> fn(&'a M) -> &'a SingularField<V::Value>,
661 mut_field: for<'a> fn(&'a mut M) -> &'a mut SingularField<V::Value>,
662 ) -> FieldAccessor
663 where
664 M: Message + 'static,
665 V: ProtobufType + 'static,
666 {
667 FieldAccessor::new_v1(
668 name,
669 FieldAccessorFunctions::Optional(Box::new(MessageGetMut::<M, SingularField<V::Value>> {
670 get_field,
671 mut_field,
672 })),
673 )
674 }
675
676 impl<M, V> FieldAccessor2<M, dyn ReflectOptional> for MessageGetMut<M, SingularPtrField<V>>
677 where
678 M: Message + 'static,
679 V: ProtobufValue + Clone + 'static,
680 {
get_field<'a>(&self, m: &'a M) -> &'a dyn ReflectOptional681 fn get_field<'a>(&self, m: &'a M) -> &'a dyn ReflectOptional {
682 (self.get_field)(m) as &dyn ReflectOptional
683 }
684
mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ReflectOptional685 fn mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ReflectOptional {
686 (self.mut_field)(m) as &mut dyn ReflectOptional
687 }
688 }
689
make_singular_ptr_field_accessor<M, V>( name: &'static str, get_field: for<'a> fn(&'a M) -> &'a SingularPtrField<V::Value>, mut_field: for<'a> fn(&'a mut M) -> &'a mut SingularPtrField<V::Value>, ) -> FieldAccessor where M: Message + 'static, V: ProtobufType + 'static,690 pub fn make_singular_ptr_field_accessor<M, V>(
691 name: &'static str,
692 get_field: for<'a> fn(&'a M) -> &'a SingularPtrField<V::Value>,
693 mut_field: for<'a> fn(&'a mut M) -> &'a mut SingularPtrField<V::Value>,
694 ) -> FieldAccessor
695 where
696 M: Message + 'static,
697 V: ProtobufType + 'static,
698 {
699 FieldAccessor::new_v1(
700 name,
701 FieldAccessorFunctions::Optional(Box::new(
702 MessageGetMut::<M, SingularPtrField<V::Value>> {
703 get_field,
704 mut_field,
705 },
706 )),
707 )
708 }
709
710 impl<M, V> FieldAccessor2<M, dyn ProtobufValue> for MessageGetMut<M, V>
711 where
712 M: Message + 'static,
713 V: ProtobufValue + Clone + 'static,
714 {
get_field<'a>(&self, m: &'a M) -> &'a dyn ProtobufValue715 fn get_field<'a>(&self, m: &'a M) -> &'a dyn ProtobufValue {
716 (self.get_field)(m) as &dyn ProtobufValue
717 }
718
mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ProtobufValue719 fn mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ProtobufValue {
720 (self.mut_field)(m) as &mut dyn ProtobufValue
721 }
722 }
723
make_simple_field_accessor<M, V>( name: &'static str, get_field: for<'a> fn(&'a M) -> &'a V::Value, mut_field: for<'a> fn(&'a mut M) -> &'a mut V::Value, ) -> FieldAccessor where M: Message + 'static, V: ProtobufType + 'static,724 pub fn make_simple_field_accessor<M, V>(
725 name: &'static str,
726 get_field: for<'a> fn(&'a M) -> &'a V::Value,
727 mut_field: for<'a> fn(&'a mut M) -> &'a mut V::Value,
728 ) -> FieldAccessor
729 where
730 M: Message + 'static,
731 V: ProtobufType + 'static,
732 {
733 FieldAccessor::new_v1(
734 name,
735 FieldAccessorFunctions::Simple(Box::new(MessageGetMut::<M, V::Value> {
736 get_field,
737 mut_field,
738 })),
739 )
740 }
741
742 impl<M, K, V> FieldAccessor2<M, dyn ReflectMap> for MessageGetMut<M, HashMap<K, V>>
743 where
744 M: Message + 'static,
745 K: ProtobufValue + 'static,
746 V: ProtobufValue + 'static,
747 K: Hash + Eq,
748 {
get_field<'a>(&self, m: &'a M) -> &'a dyn ReflectMap749 fn get_field<'a>(&self, m: &'a M) -> &'a dyn ReflectMap {
750 (self.get_field)(m) as &dyn ReflectMap
751 }
752
mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ReflectMap753 fn mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ReflectMap {
754 (self.mut_field)(m) as &mut dyn ReflectMap
755 }
756 }
757
make_map_accessor<M, K, V>( name: &'static str, get_field: for<'a> fn(&'a M) -> &'a HashMap<K::Value, V::Value>, mut_field: for<'a> fn(&'a mut M) -> &'a mut HashMap<K::Value, V::Value>, ) -> FieldAccessor where M: Message + 'static, K: ProtobufType + 'static, V: ProtobufType + 'static, <K as ProtobufType>::Value: Hash + Eq,758 pub fn make_map_accessor<M, K, V>(
759 name: &'static str,
760 get_field: for<'a> fn(&'a M) -> &'a HashMap<K::Value, V::Value>,
761 mut_field: for<'a> fn(&'a mut M) -> &'a mut HashMap<K::Value, V::Value>,
762 ) -> FieldAccessor
763 where
764 M: Message + 'static,
765 K: ProtobufType + 'static,
766 V: ProtobufType + 'static,
767 <K as ProtobufType>::Value: Hash + Eq,
768 {
769 FieldAccessor::new_v1(
770 name,
771 FieldAccessorFunctions::Map(Box::new(MessageGetMut::<M, HashMap<K::Value, V::Value>> {
772 get_field,
773 mut_field,
774 })),
775 )
776 }
777