protobuf/reflect/value/
value_box.rs

1use crate::reflect::message::message_ref::MessageRef;
2use crate::reflect::runtime_types::RuntimeTypeTrait;
3use crate::reflect::value::value_ref::ReflectValueMut;
4use crate::reflect::value::value_ref::ReflectValueRef;
5use crate::reflect::EnumDescriptor;
6use crate::reflect::EnumValueDescriptor;
7use crate::reflect::ProtobufValue;
8use crate::reflect::RuntimeType;
9use crate::MessageDyn;
10
11/// Owner value of any elementary type
12#[derive(Debug, Clone)]
13pub 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
38impl From<u32> for ReflectValueBox {
39    fn from(v: u32) -> Self {
40        ReflectValueBox::U32(v)
41    }
42}
43
44impl From<u64> for ReflectValueBox {
45    fn from(v: u64) -> Self {
46        ReflectValueBox::U64(v)
47    }
48}
49
50impl From<i32> for ReflectValueBox {
51    fn from(v: i32) -> Self {
52        ReflectValueBox::I32(v)
53    }
54}
55
56impl From<i64> for ReflectValueBox {
57    fn from(v: i64) -> Self {
58        ReflectValueBox::I64(v)
59    }
60}
61
62impl From<f32> for ReflectValueBox {
63    fn from(v: f32) -> Self {
64        ReflectValueBox::F32(v)
65    }
66}
67
68impl From<f64> for ReflectValueBox {
69    fn from(v: f64) -> Self {
70        ReflectValueBox::F64(v)
71    }
72}
73
74impl From<bool> for ReflectValueBox {
75    fn from(v: bool) -> Self {
76        ReflectValueBox::Bool(v)
77    }
78}
79
80impl From<String> for ReflectValueBox {
81    fn from(v: String) -> Self {
82        ReflectValueBox::String(v)
83    }
84}
85
86impl From<Vec<u8>> for ReflectValueBox {
87    fn from(v: Vec<u8>) -> Self {
88        ReflectValueBox::Bytes(v)
89    }
90}
91
92impl<'a> From<&'a EnumValueDescriptor> for ReflectValueBox {
93    fn from(v: &'a EnumValueDescriptor) -> Self {
94        ReflectValueBox::from(v.clone())
95    }
96}
97
98impl From<EnumValueDescriptor> for ReflectValueBox {
99    fn from(v: EnumValueDescriptor) -> Self {
100        let number = v.value();
101        ReflectValueBox::Enum(v.enum_descriptor, number)
102    }
103}
104
105impl From<Box<dyn MessageDyn>> for ReflectValueBox {
106    fn from(v: Box<dyn MessageDyn>) -> Self {
107        ReflectValueBox::Message(v)
108    }
109}
110
111fn _assert_value_box_send_sync() {
112    fn _assert_send_sync<T: Send + Sync>() {}
113    _assert_send_sync::<ReflectValueBox>();
114}
115
116impl ReflectValueBox {
117    /// Type of this value.
118    pub fn get_type(&self) -> RuntimeType {
119        self.as_value_ref().get_type()
120    }
121
122    /// As ref
123    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
139    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>`.
152    pub fn downcast<V: ProtobufValue>(self) -> Result<V, Self> {
153        V::RuntimeType::from_value_box(self)
154    }
155}
156
157impl<'a> PartialEq for ReflectValueBox {
158    fn eq(&self, other: &Self) -> bool {
159        self.as_value_ref() == other.as_value_ref()
160    }
161}
162
163impl<'a> PartialEq<ReflectValueBox> for ReflectValueRef<'a> {
164    fn eq(&self, other: &ReflectValueBox) -> bool {
165        *self == other.as_value_ref()
166    }
167}
168
169#[cfg(test)]
170mod test {
171    use super::*;
172
173    #[test]
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]
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}