1 use crate::reflect::message::message_ref::MessageRef;
2 use crate::reflect::runtime_types::RuntimeTypeTrait;
3 use crate::reflect::value::value_ref::ReflectValueMut;
4 use crate::reflect::value::value_ref::ReflectValueRef;
5 use crate::reflect::EnumDescriptor;
6 use crate::reflect::EnumValueDescriptor;
7 use crate::reflect::ProtobufValue;
8 use crate::reflect::RuntimeType;
9 use crate::MessageDyn;
10
11 /// Owner value of any elementary type
12 #[derive(Debug, Clone)]
13 pub enum ReflectValueBox {
14 /// `u32`
15 U32(u32),
16 /// `u64`
17 U64(u64),
18 /// `i32`
19 I32(i32),
20 /// `i64`
21 I64(i64),
22 /// `f32`
23 F32(f32),
24 /// `f64`
25 F64(f64),
26 /// `bool`
27 Bool(bool),
28 /// `string`
29 String(String),
30 /// `bytes`
31 Bytes(Vec<u8>),
32 /// `enum`
33 Enum(EnumDescriptor, i32),
34 /// `message`
35 Message(Box<dyn MessageDyn>),
36 }
37
38 impl From<u32> for ReflectValueBox {
from(v: u32) -> Self39 fn from(v: u32) -> Self {
40 ReflectValueBox::U32(v)
41 }
42 }
43
44 impl From<u64> for ReflectValueBox {
from(v: u64) -> Self45 fn from(v: u64) -> Self {
46 ReflectValueBox::U64(v)
47 }
48 }
49
50 impl From<i32> for ReflectValueBox {
from(v: i32) -> Self51 fn from(v: i32) -> Self {
52 ReflectValueBox::I32(v)
53 }
54 }
55
56 impl From<i64> for ReflectValueBox {
from(v: i64) -> Self57 fn from(v: i64) -> Self {
58 ReflectValueBox::I64(v)
59 }
60 }
61
62 impl From<f32> for ReflectValueBox {
from(v: f32) -> Self63 fn from(v: f32) -> Self {
64 ReflectValueBox::F32(v)
65 }
66 }
67
68 impl From<f64> for ReflectValueBox {
from(v: f64) -> Self69 fn from(v: f64) -> Self {
70 ReflectValueBox::F64(v)
71 }
72 }
73
74 impl From<bool> for ReflectValueBox {
from(v: bool) -> Self75 fn from(v: bool) -> Self {
76 ReflectValueBox::Bool(v)
77 }
78 }
79
80 impl From<String> for ReflectValueBox {
from(v: String) -> Self81 fn from(v: String) -> Self {
82 ReflectValueBox::String(v)
83 }
84 }
85
86 impl From<Vec<u8>> for ReflectValueBox {
from(v: Vec<u8>) -> Self87 fn from(v: Vec<u8>) -> Self {
88 ReflectValueBox::Bytes(v)
89 }
90 }
91
92 impl<'a> From<&'a EnumValueDescriptor> for ReflectValueBox {
from(v: &'a EnumValueDescriptor) -> Self93 fn from(v: &'a EnumValueDescriptor) -> Self {
94 ReflectValueBox::from(v.clone())
95 }
96 }
97
98 impl From<EnumValueDescriptor> for ReflectValueBox {
from(v: EnumValueDescriptor) -> Self99 fn from(v: EnumValueDescriptor) -> Self {
100 let number = v.value();
101 ReflectValueBox::Enum(v.enum_descriptor, number)
102 }
103 }
104
105 impl From<Box<dyn MessageDyn>> for ReflectValueBox {
from(v: Box<dyn MessageDyn>) -> Self106 fn from(v: Box<dyn MessageDyn>) -> Self {
107 ReflectValueBox::Message(v)
108 }
109 }
110
_assert_value_box_send_sync()111 fn _assert_value_box_send_sync() {
112 fn _assert_send_sync<T: Send + Sync>() {}
113 _assert_send_sync::<ReflectValueBox>();
114 }
115
116 impl ReflectValueBox {
117 /// Type of this value.
get_type(&self) -> RuntimeType118 pub fn get_type(&self) -> RuntimeType {
119 self.as_value_ref().get_type()
120 }
121
122 /// As ref
as_value_ref(&self) -> ReflectValueRef123 pub fn as_value_ref(&self) -> ReflectValueRef {
124 match self {
125 ReflectValueBox::U32(v) => ReflectValueRef::U32(*v),
126 ReflectValueBox::U64(v) => ReflectValueRef::U64(*v),
127 ReflectValueBox::I32(v) => ReflectValueRef::I32(*v),
128 ReflectValueBox::I64(v) => ReflectValueRef::I64(*v),
129 ReflectValueBox::F32(v) => ReflectValueRef::F32(*v),
130 ReflectValueBox::F64(v) => ReflectValueRef::F64(*v),
131 ReflectValueBox::Bool(v) => ReflectValueRef::Bool(*v),
132 ReflectValueBox::String(ref v) => ReflectValueRef::String(v.as_str()),
133 ReflectValueBox::Bytes(ref v) => ReflectValueRef::Bytes(v.as_slice()),
134 ReflectValueBox::Enum(d, v) => ReflectValueRef::Enum(d.clone(), *v),
135 ReflectValueBox::Message(v) => ReflectValueRef::Message(MessageRef::from(&**v)),
136 }
137 }
138
as_value_mut(&mut self) -> ReflectValueMut139 pub(crate) fn as_value_mut(&mut self) -> ReflectValueMut {
140 match self {
141 ReflectValueBox::Message(m) => ReflectValueMut::Message(&mut **m),
142 _ => panic!(
143 "ReflectValueMut cannot be constructed from {:?}",
144 self.get_type()
145 ),
146 }
147 }
148
149 /// Downcast to real typed value.
150 ///
151 /// For `enum` `V` can be either `V: ProtobufEnum` or `V: ProtobufEnumOrUnknown<E>`.
downcast<V: ProtobufValue>(self) -> Result<V, Self>152 pub fn downcast<V: ProtobufValue>(self) -> Result<V, Self> {
153 V::RuntimeType::from_value_box(self)
154 }
155 }
156
157 impl<'a> PartialEq for ReflectValueBox {
eq(&self, other: &Self) -> bool158 fn eq(&self, other: &Self) -> bool {
159 self.as_value_ref() == other.as_value_ref()
160 }
161 }
162
163 impl<'a> PartialEq<ReflectValueBox> for ReflectValueRef<'a> {
eq(&self, other: &ReflectValueBox) -> bool164 fn eq(&self, other: &ReflectValueBox) -> bool {
165 *self == other.as_value_ref()
166 }
167 }
168
169 #[cfg(test)]
170 mod test {
171 use super::*;
172
173 #[test]
reflect_value_box_downcast_primitive()174 fn reflect_value_box_downcast_primitive() {
175 assert_eq!(Ok(10), ReflectValueBox::U32(10).downcast::<u32>());
176 assert_eq!(
177 Err(ReflectValueBox::I32(10)),
178 ReflectValueBox::I32(10).downcast::<u32>()
179 );
180 }
181
182 #[test]
reflect_value_box_downcast_string()183 fn reflect_value_box_downcast_string() {
184 assert_eq!(
185 Ok("aa".to_owned()),
186 ReflectValueBox::String("aa".to_owned()).downcast::<String>()
187 );
188 assert_eq!(
189 Err(ReflectValueBox::String("aa".to_owned())),
190 ReflectValueBox::String("aa".to_owned()).downcast::<u32>()
191 );
192 assert_eq!(
193 Err(ReflectValueBox::Bool(false)),
194 ReflectValueBox::Bool(false).downcast::<String>()
195 );
196 }
197 }
198