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::map::ReflectMap;
13 use crate::reflect::optional::ReflectOptional;
14 use crate::reflect::repeated::ReflectRepeated;
15 use crate::reflect::repeated::ReflectRepeatedEnum;
16 use crate::reflect::repeated::ReflectRepeatedMessage;
17 use crate::reflect::rt::FieldAccessor;
18 use crate::reflect::EnumValueDescriptor;
19 use crate::reflect::ProtobufValue;
20 use crate::reflect::ReflectFieldRef;
21 use crate::reflect::ReflectValueRef;
22 use crate::repeated::RepeatedField;
23 use crate::singular::SingularField;
24 use crate::singular::SingularPtrField;
25 use crate::types::*;
26
27 /// this trait should not be used directly, use `FieldDescriptor` instead
28 pub trait FieldAccessorTrait: Sync + 'static {
has_field_generic(&self, m: &dyn Message) -> bool29 fn has_field_generic(&self, m: &dyn Message) -> bool;
len_field_generic(&self, m: &dyn Message) -> usize30 fn len_field_generic(&self, m: &dyn Message) -> usize;
31 // TODO: should it return default value or panic on unset field?
get_message_generic<'a>(&self, m: &'a dyn Message) -> &'a dyn Message32 fn get_message_generic<'a>(&self, m: &'a dyn Message) -> &'a dyn Message;
get_enum_generic(&self, m: &dyn Message) -> &'static EnumValueDescriptor33 fn get_enum_generic(&self, m: &dyn Message) -> &'static EnumValueDescriptor;
get_str_generic<'a>(&self, m: &'a dyn Message) -> &'a str34 fn get_str_generic<'a>(&self, m: &'a dyn Message) -> &'a str;
get_bytes_generic<'a>(&self, m: &'a dyn Message) -> &'a [u8]35 fn get_bytes_generic<'a>(&self, m: &'a dyn Message) -> &'a [u8];
get_u32_generic(&self, m: &dyn Message) -> u3236 fn get_u32_generic(&self, m: &dyn Message) -> u32;
get_u64_generic(&self, m: &dyn Message) -> u6437 fn get_u64_generic(&self, m: &dyn Message) -> u64;
get_i32_generic(&self, m: &dyn Message) -> i3238 fn get_i32_generic(&self, m: &dyn Message) -> i32;
get_i64_generic(&self, m: &dyn Message) -> i6439 fn get_i64_generic(&self, m: &dyn Message) -> i64;
get_bool_generic(&self, m: &dyn Message) -> bool40 fn get_bool_generic(&self, m: &dyn Message) -> bool;
get_f32_generic(&self, m: &dyn Message) -> f3241 fn get_f32_generic(&self, m: &dyn Message) -> f32;
get_f64_generic(&self, m: &dyn Message) -> f6442 fn get_f64_generic(&self, m: &dyn Message) -> f64;
43
get_reflect<'a>(&self, m: &'a dyn Message) -> ReflectFieldRef<'a>44 fn get_reflect<'a>(&self, m: &'a dyn Message) -> ReflectFieldRef<'a>;
45 }
46
47 pub(crate) trait GetSingularMessage<M>: Sync {
get_message<'a>(&self, m: &'a M) -> &'a dyn Message48 fn get_message<'a>(&self, m: &'a M) -> &'a dyn Message;
49 }
50
51 struct GetSingularMessageImpl<M, N> {
52 get: for<'a> fn(&'a M) -> &'a N,
53 }
54
55 impl<M: Message, N: Message + 'static> GetSingularMessage<M> for GetSingularMessageImpl<M, N> {
get_message<'a>(&self, m: &'a M) -> &'a dyn Message56 fn get_message<'a>(&self, m: &'a M) -> &'a dyn Message {
57 (self.get)(m)
58 }
59 }
60
61 pub(crate) trait GetSingularEnum<M>: Sync {
get_enum(&self, m: &M) -> &'static EnumValueDescriptor62 fn get_enum(&self, m: &M) -> &'static EnumValueDescriptor;
63 }
64
65 struct GetSingularEnumImpl<M, E> {
66 get: fn(&M) -> E,
67 }
68
69 impl<M: Message, E: ProtobufEnum> GetSingularEnum<M> for GetSingularEnumImpl<M, E> {
get_enum(&self, m: &M) -> &'static EnumValueDescriptor70 fn get_enum(&self, m: &M) -> &'static EnumValueDescriptor {
71 (self.get)(m).descriptor()
72 }
73 }
74
75 trait GetRepeatedMessage<M>: Sync {
len_field(&self, m: &M) -> usize76 fn len_field(&self, m: &M) -> usize;
get_message_item<'a>(&self, m: &'a M, index: usize) -> &'a dyn Message77 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>78 fn reflect_repeated_message<'a>(&self, m: &'a M) -> Box<dyn ReflectRepeatedMessage<'a> + 'a>;
79 }
80
81 trait GetRepeatedEnum<M: Message + 'static>: Sync {
len_field(&self, m: &M) -> usize82 fn len_field(&self, m: &M) -> usize;
get_enum_item(&self, m: &M, index: usize) -> &'static EnumValueDescriptor83 fn get_enum_item(&self, m: &M, index: usize) -> &'static EnumValueDescriptor;
reflect_repeated_enum<'a>(&self, m: &'a M) -> Box<dyn ReflectRepeatedEnum<'a> + 'a>84 fn reflect_repeated_enum<'a>(&self, m: &'a M) -> Box<dyn ReflectRepeatedEnum<'a> + 'a>;
85 }
86
87 pub(crate) trait GetSetCopyFns<M>: Sync {
get_field<'a>(&self, m: &'a M) -> ReflectValueRef<'a>88 fn get_field<'a>(&self, m: &'a M) -> ReflectValueRef<'a>;
89 }
90
91 struct GetSetCopyFnsImpl<M, V: ProtobufValue + Copy> {
92 get: fn(&M) -> V,
93 _set: fn(&mut M, V),
94 }
95
96 impl<M, V: ProtobufValue + Copy> GetSetCopyFns<M> for GetSetCopyFnsImpl<M, V> {
get_field<'a>(&self, m: &'a M) -> ReflectValueRef<'a>97 fn get_field<'a>(&self, m: &'a M) -> ReflectValueRef<'a> {
98 (&(self.get)(m) as &dyn ProtobufValue).as_ref_copy()
99 }
100 }
101
102 pub(crate) enum SingularGetSet<M> {
103 Copy(Box<dyn GetSetCopyFns<M>>),
104 String(for<'a> fn(&'a M) -> &'a str, fn(&mut M, String)),
105 Bytes(for<'a> fn(&'a M) -> &'a [u8], fn(&mut M, Vec<u8>)),
106 Enum(Box<dyn GetSingularEnum<M> + 'static>),
107 Message(Box<dyn GetSingularMessage<M> + 'static>),
108 }
109
110 impl<M: Message + 'static> SingularGetSet<M> {
get_ref<'a>(&self, m: &'a M) -> ReflectValueRef<'a>111 fn get_ref<'a>(&self, m: &'a M) -> ReflectValueRef<'a> {
112 match self {
113 &SingularGetSet::Copy(ref copy) => copy.get_field(m),
114 &SingularGetSet::String(get, _) => ReflectValueRef::String(get(m)),
115 &SingularGetSet::Bytes(get, _) => ReflectValueRef::Bytes(get(m)),
116 &SingularGetSet::Enum(ref get) => ReflectValueRef::Enum(get.get_enum(m)),
117 &SingularGetSet::Message(ref get) => ReflectValueRef::Message(get.get_message(m)),
118 }
119 }
120 }
121
122 pub(crate) trait FieldAccessor2<M, R: ?Sized>: Sync
123 where
124 M: Message + 'static,
125 {
get_field<'a>(&self, _: &'a M) -> &'a R126 fn get_field<'a>(&self, _: &'a M) -> &'a R;
mut_field<'a>(&self, _: &'a mut M) -> &'a mut R127 fn mut_field<'a>(&self, _: &'a mut M) -> &'a mut R;
128 }
129
130 struct MessageGetMut<M, L>
131 where
132 M: Message + 'static,
133 {
134 get_field: for<'a> fn(&'a M) -> &'a L,
135 mut_field: for<'a> fn(&'a mut M) -> &'a mut L,
136 }
137
138 pub(crate) enum FieldAccessorFunctions<M> {
139 // up to 1.0.24 optional or required
140 SingularHasGetSet {
141 has: fn(&M) -> bool,
142 get_set: SingularGetSet<M>,
143 },
144 // protobuf 3 simple field
145 Simple(Box<dyn FieldAccessor2<M, dyn ProtobufValue>>),
146 // optional, required or message
147 Optional(Box<dyn FieldAccessor2<M, dyn ReflectOptional>>),
148 // repeated
149 Repeated(Box<dyn FieldAccessor2<M, dyn ReflectRepeated>>),
150 // protobuf 3 map
151 Map(Box<dyn FieldAccessor2<M, dyn ReflectMap>>),
152 }
153
154 impl<M> fmt::Debug for FieldAccessorFunctions<M> {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result155 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
156 match self {
157 &FieldAccessorFunctions::SingularHasGetSet { .. } => {
158 write!(f, "SingularHasGetSet {{ .. }}")
159 }
160 &FieldAccessorFunctions::Simple(..) => write!(f, "Simple(..)"),
161 &FieldAccessorFunctions::Optional(..) => write!(f, "Optional(..)"),
162 &FieldAccessorFunctions::Repeated(..) => write!(f, "Repeated(..)"),
163 &FieldAccessorFunctions::Map(..) => write!(f, "Map(..)"),
164 }
165 }
166 }
167
168 pub(crate) struct FieldAccessorImpl<M> {
169 pub(crate) fns: FieldAccessorFunctions<M>,
170 }
171
172 impl<M: Message> FieldAccessorImpl<M> {
get_value_option<'a>(&self, m: &'a M) -> Option<ReflectValueRef<'a>>173 fn get_value_option<'a>(&self, m: &'a M) -> Option<ReflectValueRef<'a>> {
174 match self.fns {
175 FieldAccessorFunctions::Repeated(..) | FieldAccessorFunctions::Map(..) => {
176 panic!("repeated")
177 }
178 FieldAccessorFunctions::Simple(ref a) => Some(a.get_field(m).as_ref()),
179 FieldAccessorFunctions::Optional(ref a) => {
180 a.get_field(m).to_option().map(|v| v.as_ref())
181 }
182 FieldAccessorFunctions::SingularHasGetSet {
183 ref has,
184 ref get_set,
185 } => {
186 if !has(m) {
187 None
188 } else {
189 Some(get_set.get_ref(m))
190 }
191 }
192 }
193 }
194 }
195
196 impl<M: Message + 'static> FieldAccessorTrait for FieldAccessorImpl<M> {
has_field_generic(&self, m: &dyn Message) -> bool197 fn has_field_generic(&self, m: &dyn Message) -> bool {
198 match self.fns {
199 FieldAccessorFunctions::SingularHasGetSet { has, .. } => has(message_down_cast(m)),
200 FieldAccessorFunctions::Optional(ref a) => {
201 a.get_field(message_down_cast(m)).to_option().is_some()
202 }
203 FieldAccessorFunctions::Simple(ref a) => {
204 a.get_field(message_down_cast(m)).is_non_zero()
205 }
206 FieldAccessorFunctions::Map(..) | FieldAccessorFunctions::Repeated(..) => {
207 panic!("has_xxx is not implemented for repeated");
208 }
209 }
210 }
211
len_field_generic(&self, m: &dyn Message) -> usize212 fn len_field_generic(&self, m: &dyn Message) -> usize {
213 match self.fns {
214 FieldAccessorFunctions::Repeated(ref a) => a.get_field(message_down_cast(m)).len(),
215 FieldAccessorFunctions::Map(ref a) => a.get_field(message_down_cast(m)).len(),
216 FieldAccessorFunctions::Simple(..)
217 | FieldAccessorFunctions::SingularHasGetSet { .. }
218 | FieldAccessorFunctions::Optional(..) => {
219 panic!("not a repeated field");
220 }
221 }
222 }
223
get_message_generic<'a>(&self, m: &'a dyn Message) -> &'a dyn Message224 fn get_message_generic<'a>(&self, m: &'a dyn Message) -> &'a dyn Message {
225 match self.fns {
226 FieldAccessorFunctions::SingularHasGetSet {
227 get_set: SingularGetSet::Message(ref get),
228 ..
229 } => get.get_message(message_down_cast(m)),
230 FieldAccessorFunctions::Optional(ref t) => {
231 match t
232 .get_field(message_down_cast(m))
233 .to_option()
234 .expect("field unset")
235 .as_ref()
236 {
237 ReflectValueRef::Message(m) => m,
238 _ => panic!("not a message"),
239 }
240 }
241 ref fns => panic!("unknown accessor type: {:?}", fns),
242 }
243 }
244
get_enum_generic(&self, m: &dyn Message) -> &'static EnumValueDescriptor245 fn get_enum_generic(&self, m: &dyn Message) -> &'static EnumValueDescriptor {
246 match self.fns {
247 FieldAccessorFunctions::SingularHasGetSet {
248 get_set: SingularGetSet::Enum(ref get),
249 ..
250 } => get.get_enum(message_down_cast(m)),
251 FieldAccessorFunctions::Optional(ref t) => {
252 match t
253 .get_field(message_down_cast(m))
254 .to_option()
255 .expect("field unset")
256 .as_ref()
257 {
258 ReflectValueRef::Enum(e) => e,
259 _ => panic!("not an enum"),
260 }
261 }
262 FieldAccessorFunctions::Simple(ref t) => {
263 match t.get_field(message_down_cast(m)).as_ref() {
264 ReflectValueRef::Enum(e) => e,
265 _ => panic!("not an enum"),
266 }
267 }
268 ref fns => panic!("unknown accessor type: {:?}", fns),
269 }
270 }
271
get_str_generic<'a>(&self, m: &'a dyn Message) -> &'a str272 fn get_str_generic<'a>(&self, m: &'a dyn Message) -> &'a str {
273 match self.get_value_option(message_down_cast(m)) {
274 Some(ReflectValueRef::String(v)) => v,
275 Some(_) => panic!("wrong type"),
276 None => "", // TODO: check type
277 }
278 }
279
get_bytes_generic<'a>(&self, m: &'a dyn Message) -> &'a [u8]280 fn get_bytes_generic<'a>(&self, m: &'a dyn Message) -> &'a [u8] {
281 match self.get_value_option(message_down_cast(m)) {
282 Some(ReflectValueRef::Bytes(v)) => v,
283 Some(_) => panic!("wrong type"),
284 None => b"", // TODO: check type
285 }
286 }
287
get_u32_generic(&self, m: &dyn Message) -> u32288 fn get_u32_generic(&self, m: &dyn Message) -> u32 {
289 match self.get_value_option(message_down_cast(m)) {
290 Some(ReflectValueRef::U32(v)) => v,
291 Some(_) => panic!("wrong type"),
292 None => 0, // TODO: check type
293 }
294 }
295
get_u64_generic(&self, m: &dyn Message) -> u64296 fn get_u64_generic(&self, m: &dyn Message) -> u64 {
297 match self.get_value_option(message_down_cast(m)) {
298 Some(ReflectValueRef::U64(v)) => v,
299 Some(_) => panic!("wrong type"),
300 None => 0, // TODO: check type
301 }
302 }
303
get_i32_generic(&self, m: &dyn Message) -> i32304 fn get_i32_generic(&self, m: &dyn Message) -> i32 {
305 match self.get_value_option(message_down_cast(m)) {
306 Some(ReflectValueRef::I32(v)) => v,
307 Some(_) => panic!("wrong type"),
308 None => 0, // TODO: check type
309 }
310 }
311
get_i64_generic(&self, m: &dyn Message) -> i64312 fn get_i64_generic(&self, m: &dyn Message) -> i64 {
313 match self.get_value_option(message_down_cast(m)) {
314 Some(ReflectValueRef::I64(v)) => v,
315 Some(_) => panic!("wrong type"),
316 None => 0, // TODO: check type
317 }
318 }
319
get_bool_generic(&self, m: &dyn Message) -> bool320 fn get_bool_generic(&self, m: &dyn Message) -> bool {
321 match self.get_value_option(message_down_cast(m)) {
322 Some(ReflectValueRef::Bool(v)) => v,
323 Some(_) => panic!("wrong type"),
324 None => false, // TODO: check type
325 }
326 }
327
get_f32_generic(&self, m: &dyn Message) -> f32328 fn get_f32_generic(&self, m: &dyn Message) -> f32 {
329 match self.get_value_option(message_down_cast(m)) {
330 Some(ReflectValueRef::F32(v)) => v,
331 Some(_) => panic!("wrong type"),
332 None => 0.0, // TODO: check type
333 }
334 }
335
get_f64_generic(&self, m: &dyn Message) -> f64336 fn get_f64_generic(&self, m: &dyn Message) -> f64 {
337 match self.get_value_option(message_down_cast(m)) {
338 Some(ReflectValueRef::F64(v)) => v,
339 Some(_) => panic!("wrong type"),
340 None => 0.0, // TODO: check type
341 }
342 }
343
get_reflect<'a>(&self, m: &'a dyn Message) -> ReflectFieldRef<'a>344 fn get_reflect<'a>(&self, m: &'a dyn Message) -> ReflectFieldRef<'a> {
345 match self.fns {
346 FieldAccessorFunctions::Repeated(ref accessor2) => {
347 ReflectFieldRef::Repeated(accessor2.get_field(message_down_cast(m)))
348 }
349 FieldAccessorFunctions::Map(ref accessor2) => {
350 ReflectFieldRef::Map(accessor2.get_field(message_down_cast(m)))
351 }
352 FieldAccessorFunctions::Optional(ref accessor2) => ReflectFieldRef::Optional(
353 accessor2
354 .get_field(message_down_cast(m))
355 .to_option()
356 .map(|v| v.as_ref()),
357 ),
358 FieldAccessorFunctions::Simple(ref accessor2) => ReflectFieldRef::Optional({
359 let v = accessor2.get_field(message_down_cast(m));
360 if v.is_non_zero() {
361 Some(v.as_ref())
362 } else {
363 None
364 }
365 }),
366 FieldAccessorFunctions::SingularHasGetSet {
367 ref has,
368 ref get_set,
369 } => ReflectFieldRef::Optional(if has(message_down_cast(m)) {
370 Some(get_set.get_ref(message_down_cast(m)))
371 } else {
372 None
373 }),
374 }
375 }
376 }
377
378 // singular
379
set_panic<A, B>(_: &mut A, _: B)380 fn set_panic<A, B>(_: &mut A, _: B) {
381 panic!()
382 }
383
384 // TODO: make_singular_xxx_accessor are used only for oneof fields
385 // oneof codegen should be changed
386
make_singular_u32_accessor<M: Message + 'static>( name: &'static str, has: fn(&M) -> bool, get: fn(&M) -> u32, ) -> FieldAccessor387 pub fn make_singular_u32_accessor<M: Message + 'static>(
388 name: &'static str,
389 has: fn(&M) -> bool,
390 get: fn(&M) -> u32,
391 ) -> FieldAccessor {
392 FieldAccessor::new_v1(
393 name,
394 FieldAccessorFunctions::SingularHasGetSet {
395 has,
396 get_set: SingularGetSet::Copy(Box::new(GetSetCopyFnsImpl {
397 get,
398 _set: set_panic,
399 })),
400 },
401 )
402 }
403
make_singular_i32_accessor<M: Message + 'static>( name: &'static str, has: fn(&M) -> bool, get: fn(&M) -> i32, ) -> FieldAccessor404 pub fn make_singular_i32_accessor<M: Message + 'static>(
405 name: &'static str,
406 has: fn(&M) -> bool,
407 get: fn(&M) -> i32,
408 ) -> FieldAccessor {
409 FieldAccessor::new_v1(
410 name,
411 FieldAccessorFunctions::SingularHasGetSet {
412 has,
413 get_set: SingularGetSet::Copy(Box::new(GetSetCopyFnsImpl {
414 get,
415 _set: set_panic,
416 })),
417 },
418 )
419 }
420
make_singular_u64_accessor<M: Message + 'static>( name: &'static str, has: fn(&M) -> bool, get: fn(&M) -> u64, ) -> FieldAccessor421 pub fn make_singular_u64_accessor<M: Message + 'static>(
422 name: &'static str,
423 has: fn(&M) -> bool,
424 get: fn(&M) -> u64,
425 ) -> FieldAccessor {
426 FieldAccessor::new_v1(
427 name,
428 FieldAccessorFunctions::SingularHasGetSet {
429 has,
430 get_set: SingularGetSet::Copy(Box::new(GetSetCopyFnsImpl {
431 get,
432 _set: set_panic,
433 })),
434 },
435 )
436 }
437
make_singular_i64_accessor<M: Message + 'static>( name: &'static str, has: fn(&M) -> bool, get: fn(&M) -> i64, ) -> FieldAccessor438 pub fn make_singular_i64_accessor<M: Message + 'static>(
439 name: &'static str,
440 has: fn(&M) -> bool,
441 get: fn(&M) -> i64,
442 ) -> FieldAccessor {
443 FieldAccessor::new_v1(
444 name,
445 FieldAccessorFunctions::SingularHasGetSet {
446 has,
447 get_set: SingularGetSet::Copy(Box::new(GetSetCopyFnsImpl {
448 get,
449 _set: set_panic,
450 })),
451 },
452 )
453 }
454
make_singular_f32_accessor<M: Message + 'static>( name: &'static str, has: fn(&M) -> bool, get: fn(&M) -> f32, ) -> FieldAccessor455 pub fn make_singular_f32_accessor<M: Message + 'static>(
456 name: &'static str,
457 has: fn(&M) -> bool,
458 get: fn(&M) -> f32,
459 ) -> FieldAccessor {
460 FieldAccessor::new_v1(
461 name,
462 FieldAccessorFunctions::SingularHasGetSet {
463 has,
464 get_set: SingularGetSet::Copy(Box::new(GetSetCopyFnsImpl {
465 get,
466 _set: set_panic,
467 })),
468 },
469 )
470 }
471
make_singular_f64_accessor<M: Message + 'static>( name: &'static str, has: fn(&M) -> bool, get: fn(&M) -> f64, ) -> FieldAccessor472 pub fn make_singular_f64_accessor<M: Message + 'static>(
473 name: &'static str,
474 has: fn(&M) -> bool,
475 get: fn(&M) -> f64,
476 ) -> FieldAccessor {
477 FieldAccessor::new_v1(
478 name,
479 FieldAccessorFunctions::SingularHasGetSet {
480 has,
481 get_set: SingularGetSet::Copy(Box::new(GetSetCopyFnsImpl {
482 get,
483 _set: set_panic,
484 })),
485 },
486 )
487 }
488
make_singular_bool_accessor<M: Message + 'static>( name: &'static str, has: fn(&M) -> bool, get: fn(&M) -> bool, ) -> FieldAccessor489 pub fn make_singular_bool_accessor<M: Message + 'static>(
490 name: &'static str,
491 has: fn(&M) -> bool,
492 get: fn(&M) -> bool,
493 ) -> FieldAccessor {
494 FieldAccessor::new_v1(
495 name,
496 FieldAccessorFunctions::SingularHasGetSet {
497 has,
498 get_set: SingularGetSet::Copy(Box::new(GetSetCopyFnsImpl {
499 get,
500 _set: set_panic,
501 })),
502 },
503 )
504 }
505
make_singular_enum_accessor<M: Message + 'static, E: ProtobufEnum + 'static>( name: &'static str, has: fn(&M) -> bool, get: fn(&M) -> E, ) -> FieldAccessor506 pub fn make_singular_enum_accessor<M: Message + 'static, E: ProtobufEnum + 'static>(
507 name: &'static str,
508 has: fn(&M) -> bool,
509 get: fn(&M) -> E,
510 ) -> FieldAccessor {
511 FieldAccessor::new_v1(
512 name,
513 FieldAccessorFunctions::SingularHasGetSet {
514 has,
515 get_set: SingularGetSet::Enum(Box::new(GetSingularEnumImpl { get })),
516 },
517 )
518 }
519
make_singular_string_accessor<M: Message + 'static>( name: &'static str, has: fn(&M) -> bool, get: for<'a> fn(&'a M) -> &'a str, ) -> FieldAccessor520 pub fn make_singular_string_accessor<M: Message + 'static>(
521 name: &'static str,
522 has: fn(&M) -> bool,
523 get: for<'a> fn(&'a M) -> &'a str,
524 ) -> FieldAccessor {
525 FieldAccessor::new_v1(
526 name,
527 FieldAccessorFunctions::SingularHasGetSet {
528 has,
529 get_set: SingularGetSet::String(get, set_panic),
530 },
531 )
532 }
533
make_singular_bytes_accessor<M: Message + 'static>( name: &'static str, has: fn(&M) -> bool, get: for<'a> fn(&'a M) -> &'a [u8], ) -> FieldAccessor534 pub fn make_singular_bytes_accessor<M: Message + 'static>(
535 name: &'static str,
536 has: fn(&M) -> bool,
537 get: for<'a> fn(&'a M) -> &'a [u8],
538 ) -> FieldAccessor {
539 FieldAccessor::new_v1(
540 name,
541 FieldAccessorFunctions::SingularHasGetSet {
542 has,
543 get_set: SingularGetSet::Bytes(get, set_panic),
544 },
545 )
546 }
547
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, ) -> FieldAccessor548 pub fn make_singular_message_accessor<M: Message + 'static, F: Message + 'static>(
549 name: &'static str,
550 has: fn(&M) -> bool,
551 get: for<'a> fn(&'a M) -> &'a F,
552 ) -> FieldAccessor {
553 FieldAccessor::new_v1(
554 name,
555 FieldAccessorFunctions::SingularHasGetSet {
556 has,
557 get_set: SingularGetSet::Message(Box::new(GetSingularMessageImpl { get })),
558 },
559 )
560 }
561
562 // repeated
563
564 impl<M, V> FieldAccessor2<M, dyn ReflectRepeated> for MessageGetMut<M, Vec<V>>
565 where
566 M: Message + 'static,
567 V: ProtobufValue + 'static,
568 {
get_field<'a>(&self, m: &'a M) -> &'a dyn ReflectRepeated569 fn get_field<'a>(&self, m: &'a M) -> &'a dyn ReflectRepeated {
570 (self.get_field)(m) as &dyn ReflectRepeated
571 }
572
mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ReflectRepeated573 fn mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ReflectRepeated {
574 (self.mut_field)(m) as &mut dyn ReflectRepeated
575 }
576 }
577
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,578 pub fn make_vec_accessor<M, V>(
579 name: &'static str,
580 get_vec: for<'a> fn(&'a M) -> &'a Vec<V::Value>,
581 mut_vec: for<'a> fn(&'a mut M) -> &'a mut Vec<V::Value>,
582 ) -> FieldAccessor
583 where
584 M: Message + 'static,
585 V: ProtobufType + 'static,
586 {
587 FieldAccessor::new_v1(
588 name,
589 FieldAccessorFunctions::Repeated(Box::new(MessageGetMut::<M, Vec<V::Value>> {
590 get_field: get_vec,
591 mut_field: mut_vec,
592 })),
593 )
594 }
595
596 impl<M, V> FieldAccessor2<M, dyn ReflectRepeated> for MessageGetMut<M, RepeatedField<V>>
597 where
598 M: Message + 'static,
599 V: ProtobufValue + 'static,
600 {
get_field<'a>(&self, m: &'a M) -> &'a dyn ReflectRepeated601 fn get_field<'a>(&self, m: &'a M) -> &'a dyn ReflectRepeated {
602 (self.get_field)(m) as &dyn ReflectRepeated
603 }
604
mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ReflectRepeated605 fn mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ReflectRepeated {
606 (self.mut_field)(m) as &mut dyn ReflectRepeated
607 }
608 }
609
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,610 pub fn make_repeated_field_accessor<M, V>(
611 name: &'static str,
612 get_vec: for<'a> fn(&'a M) -> &'a RepeatedField<V::Value>,
613 mut_vec: for<'a> fn(&'a mut M) -> &'a mut RepeatedField<V::Value>,
614 ) -> FieldAccessor
615 where
616 M: Message + 'static,
617 V: ProtobufType + 'static,
618 {
619 FieldAccessor::new_v1(
620 name,
621 FieldAccessorFunctions::Repeated(Box::new(MessageGetMut::<M, RepeatedField<V::Value>> {
622 get_field: get_vec,
623 mut_field: mut_vec,
624 })),
625 )
626 }
627
628 impl<M, V> FieldAccessor2<M, dyn ReflectOptional> for MessageGetMut<M, Option<V>>
629 where
630 M: Message + 'static,
631 V: ProtobufValue + Clone + 'static,
632 {
get_field<'a>(&self, m: &'a M) -> &'a dyn ReflectOptional633 fn get_field<'a>(&self, m: &'a M) -> &'a dyn ReflectOptional {
634 (self.get_field)(m) as &dyn ReflectOptional
635 }
636
mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ReflectOptional637 fn mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ReflectOptional {
638 (self.mut_field)(m) as &mut dyn ReflectOptional
639 }
640 }
641
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,642 pub fn make_option_accessor<M, V>(
643 name: &'static str,
644 get_field: for<'a> fn(&'a M) -> &'a Option<V::Value>,
645 mut_field: for<'a> fn(&'a mut M) -> &'a mut Option<V::Value>,
646 ) -> FieldAccessor
647 where
648 M: Message + 'static,
649 V: ProtobufType + 'static,
650 {
651 FieldAccessor::new_v1(
652 name,
653 FieldAccessorFunctions::Optional(Box::new(MessageGetMut::<M, Option<V::Value>> {
654 get_field,
655 mut_field,
656 })),
657 )
658 }
659
660 impl<M, V> FieldAccessor2<M, dyn ReflectOptional> for MessageGetMut<M, SingularField<V>>
661 where
662 M: Message + 'static,
663 V: ProtobufValue + Clone + 'static,
664 {
get_field<'a>(&self, m: &'a M) -> &'a dyn ReflectOptional665 fn get_field<'a>(&self, m: &'a M) -> &'a dyn ReflectOptional {
666 (self.get_field)(m) as &dyn ReflectOptional
667 }
668
mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ReflectOptional669 fn mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ReflectOptional {
670 (self.mut_field)(m) as &mut dyn ReflectOptional
671 }
672 }
673
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,674 pub fn make_singular_field_accessor<M, V>(
675 name: &'static str,
676 get_field: for<'a> fn(&'a M) -> &'a SingularField<V::Value>,
677 mut_field: for<'a> fn(&'a mut M) -> &'a mut SingularField<V::Value>,
678 ) -> FieldAccessor
679 where
680 M: Message + 'static,
681 V: ProtobufType + 'static,
682 {
683 FieldAccessor::new_v1(
684 name,
685 FieldAccessorFunctions::Optional(Box::new(MessageGetMut::<M, SingularField<V::Value>> {
686 get_field,
687 mut_field,
688 })),
689 )
690 }
691
692 impl<M, V> FieldAccessor2<M, dyn ReflectOptional> for MessageGetMut<M, SingularPtrField<V>>
693 where
694 M: Message + 'static,
695 V: ProtobufValue + Clone + 'static,
696 {
get_field<'a>(&self, m: &'a M) -> &'a dyn ReflectOptional697 fn get_field<'a>(&self, m: &'a M) -> &'a dyn ReflectOptional {
698 (self.get_field)(m) as &dyn ReflectOptional
699 }
700
mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ReflectOptional701 fn mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ReflectOptional {
702 (self.mut_field)(m) as &mut dyn ReflectOptional
703 }
704 }
705
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,706 pub fn make_singular_ptr_field_accessor<M, V>(
707 name: &'static str,
708 get_field: for<'a> fn(&'a M) -> &'a SingularPtrField<V::Value>,
709 mut_field: for<'a> fn(&'a mut M) -> &'a mut SingularPtrField<V::Value>,
710 ) -> FieldAccessor
711 where
712 M: Message + 'static,
713 V: ProtobufType + 'static,
714 {
715 FieldAccessor::new_v1(
716 name,
717 FieldAccessorFunctions::Optional(Box::new(
718 MessageGetMut::<M, SingularPtrField<V::Value>> {
719 get_field,
720 mut_field,
721 },
722 )),
723 )
724 }
725
726 impl<M, V> FieldAccessor2<M, dyn ProtobufValue> for MessageGetMut<M, V>
727 where
728 M: Message + 'static,
729 V: ProtobufValue + Clone + 'static,
730 {
get_field<'a>(&self, m: &'a M) -> &'a dyn ProtobufValue731 fn get_field<'a>(&self, m: &'a M) -> &'a dyn ProtobufValue {
732 (self.get_field)(m) as &dyn ProtobufValue
733 }
734
mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ProtobufValue735 fn mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ProtobufValue {
736 (self.mut_field)(m) as &mut dyn ProtobufValue
737 }
738 }
739
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,740 pub fn make_simple_field_accessor<M, V>(
741 name: &'static str,
742 get_field: for<'a> fn(&'a M) -> &'a V::Value,
743 mut_field: for<'a> fn(&'a mut M) -> &'a mut V::Value,
744 ) -> FieldAccessor
745 where
746 M: Message + 'static,
747 V: ProtobufType + 'static,
748 {
749 FieldAccessor::new_v1(
750 name,
751 FieldAccessorFunctions::Simple(Box::new(MessageGetMut::<M, V::Value> {
752 get_field,
753 mut_field,
754 })),
755 )
756 }
757
758 impl<M, K, V> FieldAccessor2<M, dyn ReflectMap> for MessageGetMut<M, HashMap<K, V>>
759 where
760 M: Message + 'static,
761 K: ProtobufValue + 'static,
762 V: ProtobufValue + 'static,
763 K: Hash + Eq,
764 {
get_field<'a>(&self, m: &'a M) -> &'a dyn ReflectMap765 fn get_field<'a>(&self, m: &'a M) -> &'a dyn ReflectMap {
766 (self.get_field)(m) as &dyn ReflectMap
767 }
768
mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ReflectMap769 fn mut_field<'a>(&self, m: &'a mut M) -> &'a mut dyn ReflectMap {
770 (self.mut_field)(m) as &mut dyn ReflectMap
771 }
772 }
773
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,774 pub fn make_map_accessor<M, K, V>(
775 name: &'static str,
776 get_field: for<'a> fn(&'a M) -> &'a HashMap<K::Value, V::Value>,
777 mut_field: for<'a> fn(&'a mut M) -> &'a mut HashMap<K::Value, V::Value>,
778 ) -> FieldAccessor
779 where
780 M: Message + 'static,
781 K: ProtobufType + 'static,
782 V: ProtobufType + 'static,
783 <K as ProtobufType>::Value: Hash + Eq,
784 {
785 FieldAccessor::new_v1(
786 name,
787 FieldAccessorFunctions::Map(Box::new(MessageGetMut::<M, HashMap<K::Value, V::Value>> {
788 get_field,
789 mut_field,
790 })),
791 )
792 }
793