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