Chih-Hung Hsieh | cfc3a23 | 2020-06-10 20:13:05 -0700 | [diff] [blame] | 1 | use std::mem; |
| 2 | |
| 3 | use super::value::ProtobufValue; |
| 4 | |
Haibo Huang | d32e6ee | 2020-08-12 13:52:04 -0700 | [diff] [blame] | 5 | use crate::singular::*; |
Chih-Hung Hsieh | cfc3a23 | 2020-06-10 20:13:05 -0700 | [diff] [blame] | 6 | |
| 7 | pub trait ReflectOptional: 'static { |
Haibo Huang | 4bf8b46 | 2020-11-24 20:53:50 -0800 | [diff] [blame^] | 8 | fn to_option(&self) -> Option<&dyn ProtobufValue>; |
Chih-Hung Hsieh | cfc3a23 | 2020-06-10 20:13:05 -0700 | [diff] [blame] | 9 | |
Haibo Huang | 4bf8b46 | 2020-11-24 20:53:50 -0800 | [diff] [blame^] | 10 | fn set_value(&mut self, value: &dyn ProtobufValue); |
Chih-Hung Hsieh | cfc3a23 | 2020-06-10 20:13:05 -0700 | [diff] [blame] | 11 | } |
| 12 | |
| 13 | impl<V: ProtobufValue + Clone + 'static> ReflectOptional for Option<V> { |
Haibo Huang | 4bf8b46 | 2020-11-24 20:53:50 -0800 | [diff] [blame^] | 14 | fn to_option(&self) -> Option<&dyn ProtobufValue> { |
| 15 | self.as_ref().map(|v| v as &dyn ProtobufValue) |
Chih-Hung Hsieh | cfc3a23 | 2020-06-10 20:13:05 -0700 | [diff] [blame] | 16 | } |
| 17 | |
Haibo Huang | 4bf8b46 | 2020-11-24 20:53:50 -0800 | [diff] [blame^] | 18 | fn set_value(&mut self, value: &dyn ProtobufValue) { |
Chih-Hung Hsieh | cfc3a23 | 2020-06-10 20:13:05 -0700 | [diff] [blame] | 19 | match value.as_any().downcast_ref::<V>() { |
| 20 | Some(v) => mem::replace(self, Some(v.clone())), |
| 21 | None => panic!(), |
| 22 | }; |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | impl<V: ProtobufValue + Clone + 'static> ReflectOptional for SingularField<V> { |
Haibo Huang | 4bf8b46 | 2020-11-24 20:53:50 -0800 | [diff] [blame^] | 27 | fn to_option(&self) -> Option<&dyn ProtobufValue> { |
| 28 | self.as_ref().map(|v| v as &dyn ProtobufValue) |
Chih-Hung Hsieh | cfc3a23 | 2020-06-10 20:13:05 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Haibo Huang | 4bf8b46 | 2020-11-24 20:53:50 -0800 | [diff] [blame^] | 31 | fn set_value(&mut self, value: &dyn ProtobufValue) { |
Chih-Hung Hsieh | cfc3a23 | 2020-06-10 20:13:05 -0700 | [diff] [blame] | 32 | match value.as_any().downcast_ref::<V>() { |
| 33 | Some(v) => mem::replace(self, SingularField::some(v.clone())), |
| 34 | None => panic!(), |
| 35 | }; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | impl<V: ProtobufValue + Clone + 'static> ReflectOptional for SingularPtrField<V> { |
Haibo Huang | 4bf8b46 | 2020-11-24 20:53:50 -0800 | [diff] [blame^] | 40 | fn to_option(&self) -> Option<&dyn ProtobufValue> { |
| 41 | self.as_ref().map(|v| v as &dyn ProtobufValue) |
Chih-Hung Hsieh | cfc3a23 | 2020-06-10 20:13:05 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Haibo Huang | 4bf8b46 | 2020-11-24 20:53:50 -0800 | [diff] [blame^] | 44 | fn set_value(&mut self, value: &dyn ProtobufValue) { |
Chih-Hung Hsieh | cfc3a23 | 2020-06-10 20:13:05 -0700 | [diff] [blame] | 45 | match value.as_any().downcast_ref::<V>() { |
| 46 | Some(v) => mem::replace(self, SingularPtrField::some(v.clone())), |
| 47 | None => panic!(), |
| 48 | }; |
| 49 | } |
| 50 | } |