Skip to content

Commit

Permalink
Move message_down_cast* functions dyn Message
Browse files Browse the repository at this point in the history
  • Loading branch information
stepancheg committed Feb 3, 2019
1 parent 346fe60 commit a05a421
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 64 deletions.
32 changes: 16 additions & 16 deletions protobuf/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ pub trait Message: fmt::Debug + Clear + Send + Sync + ProtobufValue {
Self: Sized;
}

impl Message {
impl dyn Message {
pub fn downcast_box<T: Any>(self: Box<Self>) -> Result<Box<T>, Box<Message>> {
if self.as_any().is::<T>() {
unsafe {
Expand All @@ -177,6 +177,21 @@ impl Message {
Err(self)
}
}

pub fn downcast_ref<'a, M: Message + 'a>(&'a self) -> Option<&'a M> {
self.as_any().downcast_ref::<M>()
}

pub fn downcast_mut<'a, M: Message + 'a>(&'a mut self) -> Option<&'a mut M> {
if self.as_any().is::<M>() {
unsafe {
Some(&mut *(self as *mut dyn Message as *mut M))
}
} else {
None
}

}
}

impl Clone for Box<Message> {
Expand All @@ -194,21 +209,6 @@ impl PartialEq for Box<Message> {
}
}

pub fn message_down_cast_ref<'a, M: Message + 'a>(m: &'a Message) -> Option<&'a M> {
m.as_any().downcast_ref::<M>()
}

pub fn message_down_cast_mut<'a, M: Message + 'a>(m: &'a mut Message) -> Option<&'a mut M> {
if m.as_any().is::<M>() {
unsafe {
Some(&mut *(m as *mut dyn Message as *mut M))
}
} else {
None
}

}

/// Parse message from stream.
pub fn parse_from<M: Message>(is: &mut CodedInputStream) -> ProtobufResult<M> {
let mut r: M = Message::new();
Expand Down
34 changes: 17 additions & 17 deletions protobuf/src/json/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use well_known_types::UInt32Value;
use well_known_types::UInt64Value;
use well_known_types::Value;
use well_known_types::Value_oneof_kind;
use core::message_down_cast_mut;


#[derive(Debug)]
pub enum ParseError {
Expand Down Expand Up @@ -571,67 +571,67 @@ impl<'a> Parser<'a> {
}

fn merge_inner(&mut self, message: &mut Message) -> ParseResult<()> {
if let Some(duration) = message_down_cast_mut(message) {
if let Some(duration) = message.downcast_mut() {
return self.merge_wk_duration(duration);
}

if let Some(timestamp) = message_down_cast_mut(message) {
if let Some(timestamp) = message.downcast_mut() {
return self.merge_wk_timestamp(timestamp);
}

if let Some(field_mask) = message_down_cast_mut(message) {
if let Some(field_mask) = message.downcast_mut() {
return self.merge_wk_field_mask(field_mask);
}

if let Some(value) = message_down_cast_mut(message) {
if let Some(value) = message.downcast_mut() {
return self.merge_wk_value(value);
}

if let Some(value) = message_down_cast_mut(message) {
if let Some(value) = message.downcast_mut() {
return self.merge_wk_any(value);
}

if let Some(value) = message_down_cast_mut::<DoubleValue>(message) {
if let Some(value) = message.downcast_mut::<DoubleValue>() {
return self.merge_wrapper(value);
}

if let Some(value) = message_down_cast_mut::<FloatValue>(message) {
if let Some(value) = message.downcast_mut::<FloatValue>() {
return self.merge_wrapper(value);
}

if let Some(value) = message_down_cast_mut::<Int64Value>(message) {
if let Some(value) = message.downcast_mut::<Int64Value>() {
return self.merge_wrapper(value);
}

if let Some(value) = message_down_cast_mut::<UInt64Value>(message) {
if let Some(value) = message.downcast_mut::<UInt64Value>() {
return self.merge_wrapper(value);
}

if let Some(value) = message_down_cast_mut::<Int32Value>(message) {
if let Some(value) = message.downcast_mut::<Int32Value>() {
return self.merge_wrapper(value);
}

if let Some(value) = message_down_cast_mut::<UInt32Value>(message) {
if let Some(value) = message.downcast_mut::<UInt32Value>() {
return self.merge_wrapper(value);
}

if let Some(value) = message_down_cast_mut::<BoolValue>(message) {
if let Some(value) = message.downcast_mut::<BoolValue>() {
return self.merge_bool_value(value);
}

if let Some(value) = message_down_cast_mut::<StringValue>(message) {
if let Some(value) = message.downcast_mut::<StringValue>() {
return self.merge_string_value(value);
}

if let Some(value) = message_down_cast_mut::<BytesValue>(message) {
if let Some(value) = message.downcast_mut::<BytesValue>() {
return self.merge_bytes_value(value);
}

if let Some(value) = message_down_cast_mut::<ListValue>(message) {
if let Some(value) = message.downcast_mut::<ListValue>() {
return self.merge_wk_list_value(value);
}

if let Some(value) = message_down_cast_mut::<Struct>(message) {
if let Some(value) = message.downcast_mut::<Struct>() {
return self.merge_wk_struct(value);
}

Expand Down
34 changes: 17 additions & 17 deletions protobuf/src/json/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use reflect::ReflectValueRef;
use std::f32;
use std::f64;
use Message;
use core::message_down_cast_ref;


use well_known_types::Any;
use well_known_types::BoolValue;
Expand Down Expand Up @@ -401,37 +401,37 @@ impl Printer {
}

fn print_message(&mut self, message: &Message) -> PrintResult<()> {
if let Some(duration) = message_down_cast_ref::<Duration>(message) {
if let Some(duration) = message.downcast_ref::<Duration>() {
self.print_printable(duration)
} else if let Some(timestamp) = message_down_cast_ref::<Timestamp>(message) {
} else if let Some(timestamp) = message.downcast_ref::<Timestamp>() {
self.print_printable(timestamp)
} else if let Some(field_mask) = message_down_cast_ref::<FieldMask>(message) {
} else if let Some(field_mask) = message.downcast_ref::<FieldMask>() {
self.print_printable(field_mask)
} else if let Some(any) = message_down_cast_ref::<Any>(message) {
} else if let Some(any) = message.downcast_ref::<Any>() {
self.print_printable(any)
} else if let Some(value) = message_down_cast_ref::<Value>(message) {
} else if let Some(value) = message.downcast_ref::<Value>() {
self.print_printable(value)
} else if let Some(value) = message_down_cast_ref::<DoubleValue>(message) {
} else if let Some(value) = message.downcast_ref::<DoubleValue>() {
self.print_wrapper(value)
} else if let Some(value) = message_down_cast_ref::<FloatValue>(message) {
} else if let Some(value) = message.downcast_ref::<FloatValue>() {
self.print_wrapper(value)
} else if let Some(value) = message_down_cast_ref::<Int64Value>(message) {
} else if let Some(value) = message.downcast_ref::<Int64Value>() {
self.print_wrapper(value)
} else if let Some(value) = message_down_cast_ref::<UInt64Value>(message) {
} else if let Some(value) = message.downcast_ref::<UInt64Value>() {
self.print_wrapper(value)
} else if let Some(value) = message_down_cast_ref::<Int32Value>(message) {
} else if let Some(value) = message.downcast_ref::<Int32Value>() {
self.print_wrapper(value)
} else if let Some(value) = message_down_cast_ref::<UInt32Value>(message) {
} else if let Some(value) = message.downcast_ref::<UInt32Value>() {
self.print_wrapper(value)
} else if let Some(value) = message_down_cast_ref::<BoolValue>(message) {
} else if let Some(value) = message.downcast_ref::<BoolValue>() {
self.print_wrapper(value)
} else if let Some(value) = message_down_cast_ref::<StringValue>(message) {
} else if let Some(value) = message.downcast_ref::<StringValue>() {
self.print_wrapper(value)
} else if let Some(value) = message_down_cast_ref::<BytesValue>(message) {
} else if let Some(value) = message.downcast_ref::<BytesValue>() {
self.print_wrapper(value)
} else if let Some(value) = message_down_cast_ref::<ListValue>(message) {
} else if let Some(value) = message.downcast_ref::<ListValue>() {
self.print_printable(value)
} else if let Some(value) = message_down_cast_ref::<Struct>(message) {
} else if let Some(value) = message.downcast_ref::<Struct>() {
self.print_printable(value)
} else {
self.print_regular_message(message)
Expand Down
6 changes: 2 additions & 4 deletions protobuf/src/reflect/accessor/map.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::collections::HashMap;
use std::hash::Hash;

use core::message_down_cast_ref;
use Message;

use core::message_down_cast_mut;
use reflect::accessor::AccessorKind;
use reflect::accessor::FieldAccessor;
use reflect::map::ReflectMapMut;
Expand Down Expand Up @@ -47,7 +45,7 @@ where
<K::RuntimeType as RuntimeType>::Value: Eq + Hash,
{
fn get_reflect<'a>(&self, m: &'a Message) -> ReflectMapRef<'a> {
let m = message_down_cast_ref(m).unwrap();
let m = m.downcast_ref().unwrap();
let map = (self.get_field)(m);
ReflectMapRef {
map,
Expand All @@ -57,7 +55,7 @@ where
}

fn mut_reflect<'a>(&self, m: &'a mut Message) -> ReflectMapMut<'a> {
let m = message_down_cast_mut(m).unwrap();
let m = m.downcast_mut().unwrap();
let map = (self.mut_field)(m);
ReflectMapMut {
map,
Expand Down
6 changes: 2 additions & 4 deletions protobuf/src/reflect/accessor/repeated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use std::marker;
use Message;
use RepeatedField;

use core::message_down_cast_ref;
use core::message_down_cast_mut;
use reflect::accessor::AccessorKind;
use reflect::accessor::FieldAccessor;
use reflect::repeated::ReflectRepeated;
Expand Down Expand Up @@ -82,7 +80,7 @@ where
V: ProtobufType,
{
fn get_reflect<'a>(&self, m: &'a Message) -> ReflectRepeatedRef<'a> {
let m = message_down_cast_ref(m).unwrap();
let m = m.downcast_ref().unwrap();
let repeated = self.fns.get_field(m);
ReflectRepeatedRef {
repeated,
Expand All @@ -91,7 +89,7 @@ where
}

fn mut_reflect<'a>(&self, m: &'a mut Message) -> ReflectRepeatedMut<'a> {
let m = message_down_cast_mut(m).unwrap();
let m = m.downcast_mut().unwrap();
let repeated = self.fns.mut_field(m);
ReflectRepeatedMut {
repeated,
Expand Down
9 changes: 3 additions & 6 deletions protobuf/src/reflect/accessor/singular.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use core::message_down_cast_ref;
use core::message_down_cast_mut;
use reflect::accessor::AccessorKind;
use reflect::accessor::FieldAccessor;
use reflect::runtime_types::RuntimeType;
Expand Down Expand Up @@ -73,18 +71,18 @@ where
}

fn get_reflect<'a>(&self, m: &'a Message) -> Option<ReflectValueRef<'a>> {
let m = message_down_cast_ref(m).unwrap();
let m = m.downcast_ref().unwrap();
self.get_option_impl.get_reflect_impl(m)
}

fn get_singular_field_or_default<'a>(&self, m: &'a Message) -> ReflectValueRef<'a> {
let m = message_down_cast_ref(m).unwrap();
let m = m.downcast_ref().unwrap();
self.get_or_default_impl
.get_singular_field_or_default_impl(m)
}

fn set_singular_field(&self, m: &mut Message, value: ReflectValueBox) {
let m = message_down_cast_mut(m).unwrap();
let m = m.downcast_mut().unwrap();
self.set_impl.set_singular_field(m, value)
}
}
Expand Down Expand Up @@ -156,7 +154,6 @@ where
O: OptionLike<V::Value> + Sync + Send + 'static,
{
fn get_reflect_impl<'a>(&self, m: &'a M) -> Option<ReflectValueRef<'a>> {
let m = message_down_cast_ref(m).unwrap();
(self.get_field)(m).as_option_ref().map(V::as_ref)
}
}
Expand Down

0 comments on commit a05a421

Please sign in to comment.