protobuf/reflect/
runtime_types.rs

1//! Implementations of `RuntimeType` for all types.
2
3use std::collections::BTreeMap;
4use std::collections::HashMap;
5use std::fmt;
6use std::marker;
7
8#[cfg(feature = "bytes")]
9use bytes::Bytes;
10
11#[cfg(feature = "bytes")]
12use crate::chars::Chars;
13use crate::descriptor::field_descriptor_proto::Type;
14use crate::enum_or_unknown::EnumOrUnknown;
15use crate::message_full::MessageFull;
16use crate::reflect::runtime_type_box::RuntimeType;
17use crate::reflect::types::ProtobufTypeBool;
18use crate::reflect::types::ProtobufTypeBytes;
19use crate::reflect::types::ProtobufTypeDouble;
20use crate::reflect::types::ProtobufTypeEnumOrUnknown;
21use crate::reflect::types::ProtobufTypeFixed32;
22use crate::reflect::types::ProtobufTypeFixed64;
23use crate::reflect::types::ProtobufTypeFloat;
24use crate::reflect::types::ProtobufTypeInt32;
25use crate::reflect::types::ProtobufTypeInt64;
26use crate::reflect::types::ProtobufTypeMessage;
27use crate::reflect::types::ProtobufTypeSfixed32;
28use crate::reflect::types::ProtobufTypeSfixed64;
29use crate::reflect::types::ProtobufTypeSint32;
30use crate::reflect::types::ProtobufTypeSint64;
31use crate::reflect::types::ProtobufTypeString;
32#[cfg(feature = "bytes")]
33use crate::reflect::types::ProtobufTypeTokioBytes;
34#[cfg(feature = "bytes")]
35use crate::reflect::types::ProtobufTypeTokioChars;
36use crate::reflect::types::ProtobufTypeTrait;
37use crate::reflect::types::ProtobufTypeUint32;
38use crate::reflect::types::ProtobufTypeUint64;
39use crate::reflect::value::value_ref::ReflectValueMut;
40use crate::reflect::MessageRef;
41use crate::reflect::ProtobufValue;
42use crate::reflect::ReflectValueBox;
43use crate::reflect::ReflectValueRef;
44use crate::EnumFull;
45use crate::UnknownValueRef;
46
47/// `RuntimeType` is not implemented by all protobuf types directly
48/// because it's not possible to implement `RuntimeType` for all `Message`
49/// implementations at once: each `Message` implementation has to reimplement
50/// all the methods again. With current strategy there's only implementation
51/// for all messages, which is `RuntimeTypeMessage`.
52///
53/// The downside is that we have to explicitly specify type parameters
54/// in a lot of places.
55pub trait RuntimeTypeTrait: fmt::Debug + Send + Sync + Sized + 'static {
56    /// Actual value for this type.
57    type Value: ProtobufValue + Clone + Sized + fmt::Debug + Default;
58
59    /// "Box" version of type type.
60    fn runtime_type_box() -> RuntimeType;
61
62    /// Default value for this type.
63    fn default_value_ref() -> ReflectValueRef<'static>;
64
65    /// Construct a value from given reflective value.
66    ///
67    /// # Panics
68    ///
69    /// If reflective value is of incompatible type.
70    fn from_value_box(value_box: ReflectValueBox) -> Result<Self::Value, ReflectValueBox>;
71
72    /// Convert a value into a refletive box value.
73    fn into_value_box(value: Self::Value) -> ReflectValueBox;
74
75    /// Convert a value into a ref value if possible.
76    ///
77    /// # Panics
78    ///
79    /// For message and enum.
80    // TODO: move the operation into a separate trait
81    fn into_static_value_ref(value: Self::Value) -> ReflectValueRef<'static> {
82        panic!("value {:?} cannot be converted to static ref", value)
83    }
84
85    /// Pointer to a dynamic reference.
86    fn as_ref(value: &Self::Value) -> ReflectValueRef;
87    /// Mutable pointer to a dynamic mutable reference.
88    fn as_mut(value: &mut Self::Value) -> ReflectValueMut;
89
90    /// Value is non-default?
91    fn is_non_zero(value: &Self::Value) -> bool;
92
93    /// Write the value.
94    fn set_from_value_box(target: &mut Self::Value, value_box: ReflectValueBox) {
95        *target = Self::from_value_box(value_box).expect("wrong type");
96    }
97
98    /// Cast values to enum values.
99    ///
100    /// # Panics
101    ///
102    /// If self is not an enum.
103    fn cast_to_enum_values(values: &[Self::Value]) -> &[i32] {
104        let _ = values;
105        panic!("not enum")
106    }
107
108    /// Parse the value from unknown fields.
109    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value>;
110}
111
112/// Runtime type which can be dereferenced.
113pub trait RuntimeTypeWithDeref: RuntimeTypeTrait {
114    /// Deref target.
115    type DerefTarget: ?Sized;
116
117    /// Deref.
118    // TODO: rename to `deref`
119    fn deref_as_ref(value: &Self::DerefTarget) -> ReflectValueRef;
120}
121
122/// Types which can be map keys.
123pub trait RuntimeTypeMapKey: RuntimeTypeTrait {
124    /// Query hash map with a given key.
125    fn hash_map_get<'a, V>(map: &'a HashMap<Self::Value, V>, key: ReflectValueRef)
126        -> Option<&'a V>;
127
128    /// Query btree map with a given key.
129    fn btree_map_get<'a, V>(
130        map: &'a BTreeMap<Self::Value, V>,
131        key: ReflectValueRef,
132    ) -> Option<&'a V>;
133}
134
135/// Implementation for `f32`
136#[derive(Debug, Copy, Clone)]
137pub struct RuntimeTypeF32;
138/// Implementation for `f64`
139#[derive(Debug, Copy, Clone)]
140pub struct RuntimeTypeF64;
141/// Implementation for `i32`
142#[derive(Debug, Copy, Clone)]
143pub struct RuntimeTypeI32;
144/// Implementation for `f32`
145#[derive(Debug, Copy, Clone)]
146pub struct RuntimeTypeI64;
147/// Implementation for `u32`
148#[derive(Debug, Copy, Clone)]
149pub struct RuntimeTypeU32;
150/// Implementation for `u64`
151#[derive(Debug, Copy, Clone)]
152pub struct RuntimeTypeU64;
153/// Implementation for `bool`
154#[derive(Debug, Copy, Clone)]
155pub struct RuntimeTypeBool;
156/// Implementation for `String`
157#[derive(Debug, Copy, Clone)]
158pub struct RuntimeTypeString;
159/// Implementation for `Vec<u8>`
160#[derive(Debug, Copy, Clone)]
161pub struct RuntimeTypeVecU8;
162
163/// Implementation for [`Bytes`].
164#[cfg(feature = "bytes")]
165#[derive(Debug, Copy, Clone)]
166pub struct RuntimeTypeTokioBytes;
167/// Implementation for [`Chars`].
168#[cfg(feature = "bytes")]
169#[derive(Debug, Copy, Clone)]
170pub struct RuntimeTypeTokioChars;
171
172/// Implementation for enum.
173#[derive(Debug, Copy, Clone)]
174pub struct RuntimeTypeEnumOrUnknown<E: EnumFull>(marker::PhantomData<E>);
175/// Implementation for [`MessageFull`].
176#[derive(Debug, Copy, Clone)]
177pub struct RuntimeTypeMessage<M: MessageFull>(marker::PhantomData<M>);
178
179impl RuntimeTypeTrait for RuntimeTypeF32 {
180    type Value = f32;
181
182    fn runtime_type_box() -> RuntimeType
183    where
184        Self: Sized,
185    {
186        RuntimeType::F32
187    }
188
189    fn default_value_ref() -> ReflectValueRef<'static> {
190        ReflectValueRef::F32(0.0)
191    }
192
193    fn from_value_box(value_box: ReflectValueBox) -> Result<f32, ReflectValueBox> {
194        match value_box {
195            ReflectValueBox::F32(v) => Ok(v),
196            b => Err(b),
197        }
198    }
199    fn into_value_box(value: f32) -> ReflectValueBox {
200        ReflectValueBox::F32(value)
201    }
202
203    fn into_static_value_ref(value: f32) -> ReflectValueRef<'static> {
204        ReflectValueRef::F32(value)
205    }
206    fn as_ref(value: &f32) -> ReflectValueRef {
207        ReflectValueRef::F32(*value)
208    }
209
210    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {
211        unimplemented!()
212    }
213
214    fn is_non_zero(value: &f32) -> bool {
215        *value != 0.0
216    }
217
218    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {
219        assert_eq!(field_type, Type::TYPE_FLOAT);
220        ProtobufTypeFloat::get_from_unknown(unknown)
221    }
222}
223
224impl RuntimeTypeTrait for RuntimeTypeF64 {
225    type Value = f64;
226
227    fn default_value_ref() -> ReflectValueRef<'static> {
228        ReflectValueRef::F64(0.0)
229    }
230
231    fn runtime_type_box() -> RuntimeType
232    where
233        Self: Sized,
234    {
235        RuntimeType::F64
236    }
237
238    fn from_value_box(value_box: ReflectValueBox) -> Result<f64, ReflectValueBox> {
239        match value_box {
240            ReflectValueBox::F64(v) => Ok(v),
241            b => Err(b),
242        }
243    }
244
245    fn into_value_box(value: f64) -> ReflectValueBox {
246        ReflectValueBox::F64(value)
247    }
248
249    fn into_static_value_ref(value: f64) -> ReflectValueRef<'static> {
250        ReflectValueRef::F64(value)
251    }
252
253    fn as_ref(value: &f64) -> ReflectValueRef {
254        ReflectValueRef::F64(*value)
255    }
256
257    fn is_non_zero(value: &f64) -> bool {
258        *value != 0.0
259    }
260
261    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {
262        unimplemented!()
263    }
264
265    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {
266        assert_eq!(field_type, Type::TYPE_DOUBLE);
267        ProtobufTypeDouble::get_from_unknown(unknown)
268    }
269}
270
271impl RuntimeTypeTrait for RuntimeTypeI32 {
272    type Value = i32;
273
274    fn default_value_ref() -> ReflectValueRef<'static> {
275        ReflectValueRef::I32(0)
276    }
277
278    fn runtime_type_box() -> RuntimeType
279    where
280        Self: Sized,
281    {
282        RuntimeType::I32
283    }
284
285    fn from_value_box(value_box: ReflectValueBox) -> Result<i32, ReflectValueBox> {
286        match value_box {
287            ReflectValueBox::I32(v) => Ok(v),
288            b => Err(b),
289        }
290    }
291
292    fn into_value_box(value: i32) -> ReflectValueBox {
293        ReflectValueBox::I32(value)
294    }
295
296    fn into_static_value_ref(value: i32) -> ReflectValueRef<'static> {
297        ReflectValueRef::I32(value)
298    }
299
300    fn as_ref(value: &i32) -> ReflectValueRef {
301        ReflectValueRef::I32(*value)
302    }
303
304    fn is_non_zero(value: &i32) -> bool {
305        *value != 0
306    }
307
308    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {
309        unimplemented!()
310    }
311
312    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {
313        match field_type {
314            Type::TYPE_INT32 => ProtobufTypeInt32::get_from_unknown(unknown),
315            Type::TYPE_SINT32 => ProtobufTypeSint32::get_from_unknown(unknown),
316            Type::TYPE_SFIXED32 => ProtobufTypeSfixed32::get_from_unknown(unknown),
317            _ => panic!("wrong type: {:?}", field_type),
318        }
319    }
320}
321impl RuntimeTypeMapKey for RuntimeTypeI32 {
322    fn hash_map_get<'a, V>(map: &'a HashMap<i32, V>, key: ReflectValueRef) -> Option<&'a V> {
323        match key {
324            ReflectValueRef::I32(i) => map.get(&i),
325            _ => None,
326        }
327    }
328
329    fn btree_map_get<'a, V>(
330        map: &'a BTreeMap<Self::Value, V>,
331        key: ReflectValueRef,
332    ) -> Option<&'a V> {
333        match key {
334            ReflectValueRef::I32(i) => map.get(&i),
335            _ => None,
336        }
337    }
338}
339
340impl RuntimeTypeTrait for RuntimeTypeI64 {
341    type Value = i64;
342
343    fn default_value_ref() -> ReflectValueRef<'static> {
344        ReflectValueRef::I64(0)
345    }
346
347    fn runtime_type_box() -> RuntimeType
348    where
349        Self: Sized,
350    {
351        RuntimeType::I64
352    }
353
354    fn from_value_box(value_box: ReflectValueBox) -> Result<i64, ReflectValueBox> {
355        match value_box {
356            ReflectValueBox::I64(v) => Ok(v),
357            b => Err(b),
358        }
359    }
360
361    fn into_value_box(value: i64) -> ReflectValueBox {
362        ReflectValueBox::I64(value)
363    }
364
365    fn into_static_value_ref(value: i64) -> ReflectValueRef<'static> {
366        ReflectValueRef::I64(value)
367    }
368
369    fn as_ref(value: &i64) -> ReflectValueRef {
370        ReflectValueRef::I64(*value)
371    }
372
373    fn is_non_zero(value: &i64) -> bool {
374        *value != 0
375    }
376
377    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {
378        unimplemented!()
379    }
380
381    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {
382        match field_type {
383            Type::TYPE_INT64 => ProtobufTypeInt64::get_from_unknown(unknown),
384            Type::TYPE_SINT64 => ProtobufTypeSint64::get_from_unknown(unknown),
385            Type::TYPE_SFIXED64 => ProtobufTypeSfixed64::get_from_unknown(unknown),
386            _ => panic!("wrong type: {:?}", field_type),
387        }
388    }
389}
390impl RuntimeTypeMapKey for RuntimeTypeI64 {
391    fn hash_map_get<'a, V>(
392        map: &'a HashMap<Self::Value, V>,
393        key: ReflectValueRef,
394    ) -> Option<&'a V> {
395        match key {
396            ReflectValueRef::I64(i) => map.get(&i),
397            _ => None,
398        }
399    }
400
401    fn btree_map_get<'a, V>(
402        map: &'a BTreeMap<Self::Value, V>,
403        key: ReflectValueRef,
404    ) -> Option<&'a V> {
405        match key {
406            ReflectValueRef::I64(i) => map.get(&i),
407            _ => None,
408        }
409    }
410}
411
412impl RuntimeTypeTrait for RuntimeTypeU32 {
413    type Value = u32;
414
415    fn runtime_type_box() -> RuntimeType
416    where
417        Self: Sized,
418    {
419        RuntimeType::U32
420    }
421
422    fn default_value_ref() -> ReflectValueRef<'static> {
423        ReflectValueRef::U32(0)
424    }
425
426    fn from_value_box(value_box: ReflectValueBox) -> Result<u32, ReflectValueBox> {
427        match value_box {
428            ReflectValueBox::U32(v) => Ok(v),
429            b => Err(b),
430        }
431    }
432
433    fn into_value_box(value: u32) -> ReflectValueBox {
434        ReflectValueBox::U32(value)
435    }
436
437    fn into_static_value_ref(value: u32) -> ReflectValueRef<'static> {
438        ReflectValueRef::U32(value)
439    }
440
441    fn as_ref(value: &u32) -> ReflectValueRef {
442        ReflectValueRef::U32(*value)
443    }
444
445    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {
446        unimplemented!()
447    }
448
449    fn is_non_zero(value: &u32) -> bool {
450        *value != 0
451    }
452
453    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {
454        match field_type {
455            Type::TYPE_UINT32 => ProtobufTypeUint32::get_from_unknown(unknown),
456            Type::TYPE_FIXED32 => ProtobufTypeFixed32::get_from_unknown(unknown),
457            _ => panic!("wrong type: {:?}", field_type),
458        }
459    }
460}
461impl RuntimeTypeMapKey for RuntimeTypeU32 {
462    fn hash_map_get<'a, V>(map: &'a HashMap<u32, V>, key: ReflectValueRef) -> Option<&'a V> {
463        match key {
464            ReflectValueRef::U32(i) => map.get(&i),
465            _ => None,
466        }
467    }
468
469    fn btree_map_get<'a, V>(map: &'a BTreeMap<u32, V>, key: ReflectValueRef) -> Option<&'a V> {
470        match key {
471            ReflectValueRef::U32(i) => map.get(&i),
472            _ => None,
473        }
474    }
475}
476
477impl RuntimeTypeTrait for RuntimeTypeU64 {
478    type Value = u64;
479
480    fn default_value_ref() -> ReflectValueRef<'static> {
481        ReflectValueRef::U64(0)
482    }
483
484    fn runtime_type_box() -> RuntimeType
485    where
486        Self: Sized,
487    {
488        RuntimeType::U64
489    }
490
491    fn from_value_box(value_box: ReflectValueBox) -> Result<u64, ReflectValueBox> {
492        match value_box {
493            ReflectValueBox::U64(v) => Ok(v),
494            b => Err(b),
495        }
496    }
497
498    fn into_value_box(value: u64) -> ReflectValueBox {
499        ReflectValueBox::U64(value)
500    }
501
502    fn into_static_value_ref(value: u64) -> ReflectValueRef<'static> {
503        ReflectValueRef::U64(value)
504    }
505
506    fn as_ref(value: &u64) -> ReflectValueRef {
507        ReflectValueRef::U64(*value)
508    }
509
510    fn is_non_zero(value: &u64) -> bool {
511        *value != 0
512    }
513
514    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {
515        unimplemented!()
516    }
517
518    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {
519        match field_type {
520            Type::TYPE_UINT64 => ProtobufTypeUint64::get_from_unknown(unknown),
521            Type::TYPE_FIXED64 => ProtobufTypeFixed64::get_from_unknown(unknown),
522            _ => panic!("wrong type: {:?}", field_type),
523        }
524    }
525}
526impl RuntimeTypeMapKey for RuntimeTypeU64 {
527    fn hash_map_get<'a, V>(map: &'a HashMap<u64, V>, key: ReflectValueRef) -> Option<&'a V> {
528        match key {
529            ReflectValueRef::U64(i) => map.get(&i),
530            _ => None,
531        }
532    }
533
534    fn btree_map_get<'a, V>(map: &'a BTreeMap<u64, V>, key: ReflectValueRef) -> Option<&'a V> {
535        match key {
536            ReflectValueRef::U64(i) => map.get(&i),
537            _ => None,
538        }
539    }
540}
541
542impl RuntimeTypeTrait for RuntimeTypeBool {
543    type Value = bool;
544
545    fn default_value_ref() -> ReflectValueRef<'static> {
546        ReflectValueRef::Bool(false)
547    }
548
549    fn runtime_type_box() -> RuntimeType
550    where
551        Self: Sized,
552    {
553        RuntimeType::Bool
554    }
555
556    fn from_value_box(value_box: ReflectValueBox) -> Result<bool, ReflectValueBox> {
557        match value_box {
558            ReflectValueBox::Bool(v) => Ok(v),
559            b => Err(b),
560        }
561    }
562
563    fn into_value_box(value: bool) -> ReflectValueBox {
564        ReflectValueBox::Bool(value)
565    }
566
567    fn into_static_value_ref(value: bool) -> ReflectValueRef<'static> {
568        ReflectValueRef::Bool(value)
569    }
570
571    fn as_ref(value: &bool) -> ReflectValueRef {
572        ReflectValueRef::Bool(*value)
573    }
574
575    fn is_non_zero(value: &bool) -> bool {
576        *value
577    }
578
579    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {
580        unimplemented!()
581    }
582
583    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {
584        assert_eq!(field_type, Type::TYPE_BOOL);
585        ProtobufTypeBool::get_from_unknown(unknown)
586    }
587}
588impl RuntimeTypeMapKey for RuntimeTypeBool {
589    fn hash_map_get<'a, V>(map: &'a HashMap<bool, V>, key: ReflectValueRef) -> Option<&'a V> {
590        match key {
591            ReflectValueRef::Bool(i) => map.get(&i),
592            _ => None,
593        }
594    }
595
596    fn btree_map_get<'a, V>(
597        map: &'a BTreeMap<Self::Value, V>,
598        key: ReflectValueRef,
599    ) -> Option<&'a V> {
600        match key {
601            ReflectValueRef::Bool(i) => map.get(&i),
602            _ => None,
603        }
604    }
605}
606
607impl RuntimeTypeTrait for RuntimeTypeString {
608    type Value = String;
609
610    fn runtime_type_box() -> RuntimeType
611    where
612        Self: Sized,
613    {
614        RuntimeType::String
615    }
616
617    fn default_value_ref() -> ReflectValueRef<'static> {
618        ReflectValueRef::String("")
619    }
620
621    fn from_value_box(value_box: ReflectValueBox) -> Result<String, ReflectValueBox> {
622        match value_box {
623            ReflectValueBox::String(v) => Ok(v),
624            b => Err(b),
625        }
626    }
627
628    fn into_value_box(value: String) -> ReflectValueBox {
629        ReflectValueBox::String(value)
630    }
631
632    fn as_ref(value: &String) -> ReflectValueRef {
633        ReflectValueRef::String(&*value)
634    }
635
636    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {
637        unimplemented!()
638    }
639
640    fn is_non_zero(value: &String) -> bool {
641        !value.is_empty()
642    }
643
644    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {
645        assert_eq!(field_type, Type::TYPE_STRING);
646        ProtobufTypeString::get_from_unknown(unknown)
647    }
648}
649impl RuntimeTypeWithDeref for RuntimeTypeString {
650    type DerefTarget = str;
651
652    fn deref_as_ref(value: &str) -> ReflectValueRef {
653        ReflectValueRef::String(value)
654    }
655}
656impl RuntimeTypeMapKey for RuntimeTypeString {
657    fn hash_map_get<'a, V>(map: &'a HashMap<String, V>, key: ReflectValueRef) -> Option<&'a V> {
658        match key {
659            ReflectValueRef::String(s) => map.get(*&s),
660            _ => None,
661        }
662    }
663
664    /// Query btree map with a given key.
665    fn btree_map_get<'a, V>(
666        map: &'a BTreeMap<Self::Value, V>,
667        key: ReflectValueRef,
668    ) -> Option<&'a V>
669    where
670        Self::Value: Ord,
671    {
672        match key {
673            ReflectValueRef::String(s) => map.get(s),
674            _ => None,
675        }
676    }
677}
678
679impl RuntimeTypeTrait for RuntimeTypeVecU8 {
680    type Value = Vec<u8>;
681
682    fn runtime_type_box() -> RuntimeType
683    where
684        Self: Sized,
685    {
686        RuntimeType::VecU8
687    }
688
689    fn default_value_ref() -> ReflectValueRef<'static> {
690        ReflectValueRef::Bytes(b"")
691    }
692
693    fn from_value_box(value_box: ReflectValueBox) -> Result<Vec<u8>, ReflectValueBox> {
694        match value_box {
695            ReflectValueBox::Bytes(v) => Ok(v),
696            b => Err(b),
697        }
698    }
699
700    fn into_value_box(value: Vec<u8>) -> ReflectValueBox {
701        ReflectValueBox::Bytes(value)
702    }
703
704    fn as_ref(value: &Vec<u8>) -> ReflectValueRef {
705        ReflectValueRef::Bytes(value.as_slice())
706    }
707
708    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {
709        unimplemented!()
710    }
711
712    fn is_non_zero(value: &Vec<u8>) -> bool {
713        !value.is_empty()
714    }
715
716    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {
717        assert_eq!(field_type, Type::TYPE_BYTES);
718        ProtobufTypeBytes::get_from_unknown(unknown)
719    }
720}
721impl RuntimeTypeWithDeref for RuntimeTypeVecU8 {
722    type DerefTarget = [u8];
723
724    fn deref_as_ref(value: &[u8]) -> ReflectValueRef {
725        ReflectValueRef::Bytes(value)
726    }
727}
728
729#[cfg(feature = "bytes")]
730impl RuntimeTypeTrait for RuntimeTypeTokioBytes {
731    type Value = Bytes;
732
733    fn default_value_ref() -> ReflectValueRef<'static> {
734        ReflectValueRef::Bytes(b"")
735    }
736
737    fn runtime_type_box() -> RuntimeType
738    where
739        Self: Sized,
740    {
741        RuntimeType::VecU8
742    }
743
744    fn from_value_box(value_box: ReflectValueBox) -> Result<Bytes, ReflectValueBox> {
745        match value_box {
746            ReflectValueBox::Bytes(v) => Ok(v.into()),
747            b => Err(b),
748        }
749    }
750
751    fn into_value_box(value: Bytes) -> ReflectValueBox {
752        // TODO: copies here
753        ReflectValueBox::Bytes(value.as_ref().to_owned())
754    }
755
756    fn as_ref(value: &Bytes) -> ReflectValueRef {
757        ReflectValueRef::Bytes(value.as_ref())
758    }
759
760    fn is_non_zero(value: &Bytes) -> bool {
761        !value.is_empty()
762    }
763
764    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {
765        unimplemented!()
766    }
767
768    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {
769        assert_eq!(field_type, Type::TYPE_BYTES);
770        ProtobufTypeTokioBytes::get_from_unknown(unknown)
771    }
772}
773#[cfg(feature = "bytes")]
774impl RuntimeTypeWithDeref for RuntimeTypeTokioBytes {
775    type DerefTarget = [u8];
776
777    fn deref_as_ref(value: &[u8]) -> ReflectValueRef {
778        ReflectValueRef::Bytes(value)
779    }
780}
781
782#[cfg(feature = "bytes")]
783impl RuntimeTypeTrait for RuntimeTypeTokioChars {
784    type Value = Chars;
785
786    fn default_value_ref() -> ReflectValueRef<'static> {
787        ReflectValueRef::String("")
788    }
789
790    fn runtime_type_box() -> RuntimeType
791    where
792        Self: Sized,
793    {
794        RuntimeType::String
795    }
796
797    fn from_value_box(value_box: ReflectValueBox) -> Result<Chars, ReflectValueBox> {
798        match value_box {
799            ReflectValueBox::String(v) => Ok(v.into()),
800            b => Err(b),
801        }
802    }
803
804    fn into_value_box(value: Chars) -> ReflectValueBox {
805        ReflectValueBox::String(value.into())
806    }
807
808    fn as_ref(value: &Chars) -> ReflectValueRef {
809        ReflectValueRef::String(value.as_ref())
810    }
811
812    fn is_non_zero(value: &Chars) -> bool {
813        !value.is_empty()
814    }
815
816    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {
817        unimplemented!()
818    }
819
820    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {
821        assert_eq!(field_type, Type::TYPE_STRING);
822        ProtobufTypeTokioChars::get_from_unknown(unknown)
823    }
824}
825#[cfg(feature = "bytes")]
826impl RuntimeTypeWithDeref for RuntimeTypeTokioChars {
827    type DerefTarget = str;
828
829    fn deref_as_ref(value: &str) -> ReflectValueRef {
830        ReflectValueRef::String(value)
831    }
832}
833#[cfg(feature = "bytes")]
834impl RuntimeTypeMapKey for RuntimeTypeTokioChars {
835    fn hash_map_get<'a, V>(map: &'a HashMap<Chars, V>, key: ReflectValueRef) -> Option<&'a V> {
836        match key {
837            ReflectValueRef::String(s) => map.get(&*s),
838            _ => None,
839        }
840    }
841    fn btree_map_get<'a, V>(map: &'a BTreeMap<Chars, V>, key: ReflectValueRef) -> Option<&'a V> {
842        match key {
843            ReflectValueRef::String(s) => map.get(&*s),
844            _ => None,
845        }
846    }
847}
848
849impl<E> RuntimeTypeTrait for RuntimeTypeEnumOrUnknown<E>
850where
851    E: EnumFull + fmt::Debug,
852{
853    type Value = EnumOrUnknown<E>;
854
855    fn runtime_type_box() -> RuntimeType
856    where
857        Self: Sized,
858    {
859        RuntimeType::Enum(E::enum_descriptor())
860    }
861
862    fn default_value_ref() -> ReflectValueRef<'static> {
863        ReflectValueRef::from(E::enum_descriptor().default_value())
864    }
865
866    fn from_value_box(value_box: ReflectValueBox) -> Result<EnumOrUnknown<E>, ReflectValueBox> {
867        match value_box {
868            ReflectValueBox::Enum(d, v) if d == E::enum_descriptor() => {
869                Ok(EnumOrUnknown::from_i32(v))
870            }
871            b => Err(b),
872        }
873    }
874
875    fn into_value_box(value: EnumOrUnknown<E>) -> ReflectValueBox {
876        ReflectValueBox::Enum(E::enum_descriptor(), value.value())
877    }
878
879    fn into_static_value_ref(value: EnumOrUnknown<E>) -> ReflectValueRef<'static> {
880        ReflectValueRef::Enum(E::enum_descriptor(), value.value())
881    }
882
883    fn as_ref(value: &EnumOrUnknown<E>) -> ReflectValueRef {
884        ReflectValueRef::Enum(E::enum_descriptor(), value.value())
885    }
886
887    fn as_mut(_value: &mut Self::Value) -> ReflectValueMut {
888        unimplemented!()
889    }
890
891    fn is_non_zero(value: &EnumOrUnknown<E>) -> bool {
892        value.value() != 0
893    }
894
895    fn cast_to_enum_values(values: &[EnumOrUnknown<E>]) -> &[i32] {
896        EnumOrUnknown::cast_to_values(values)
897    }
898
899    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {
900        assert_eq!(field_type, Type::TYPE_ENUM);
901        ProtobufTypeEnumOrUnknown::<E>::get_from_unknown(unknown)
902    }
903}
904
905impl<M> RuntimeTypeTrait for RuntimeTypeMessage<M>
906where
907    M: MessageFull + ProtobufValue + Clone + Default,
908{
909    type Value = M;
910
911    fn runtime_type_box() -> RuntimeType
912    where
913        Self: Sized,
914    {
915        RuntimeType::Message(M::descriptor())
916    }
917
918    fn default_value_ref() -> ReflectValueRef<'static> {
919        ReflectValueRef::Message(MessageRef::new(M::default_instance()))
920    }
921
922    fn from_value_box(value_box: ReflectValueBox) -> Result<M, ReflectValueBox> {
923        match value_box {
924            ReflectValueBox::Message(v) => v
925                .downcast_box()
926                .map(|v| *v)
927                .map_err(ReflectValueBox::Message),
928            b => Err(b),
929        }
930    }
931
932    fn into_value_box(value: M) -> ReflectValueBox {
933        ReflectValueBox::Message(Box::new(value))
934    }
935    fn as_ref(value: &M) -> ReflectValueRef {
936        ReflectValueRef::Message(MessageRef::new(value))
937    }
938
939    fn as_mut(value: &mut M) -> ReflectValueMut {
940        ReflectValueMut::Message(value)
941    }
942
943    fn is_non_zero(_value: &M) -> bool {
944        true
945    }
946
947    fn get_from_unknown(unknown: UnknownValueRef, field_type: Type) -> Option<Self::Value> {
948        assert_eq!(field_type, Type::TYPE_MESSAGE);
949        ProtobufTypeMessage::<M>::get_from_unknown(unknown)
950    }
951}