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