rune/runtime/value/
macros.rs

1macro_rules! inline_into {
2    (
3        $(#[$($meta:meta)*])*
4        $kind:ident($ty:ty),
5        $as:ident,
6        $as_mut:ident,
7    ) => {
8        $(#[$($meta)*])*
9        ///
10        /// This gets a copy of the value.
11        #[inline]
12        pub fn $as(&self) -> Result<$ty, RuntimeError> {
13            match &self.repr {
14                Repr::Inline(Inline::$kind(value)) => {
15                    Ok(*value)
16                }
17                Repr::Inline(value) => {
18                    Err(RuntimeError::expected::<$ty>(value.type_info()))
19                }
20                Repr::Dynamic(value) => {
21                    Err(RuntimeError::expected::<$ty>(value.type_info()))
22                }
23                Repr::Any(value) => {
24                    Err(RuntimeError::expected::<$ty>(value.type_info()))
25                }
26            }
27        }
28
29        $(#[$($meta)*])*
30        ///
31        /// This gets the value by mutable reference.
32        #[inline]
33        pub fn $as_mut(&mut self) -> Result<&mut $ty, RuntimeError> {
34            match &mut self.repr {
35                Repr::Inline(Inline::$kind(value)) => {
36                    Ok(value)
37                }
38                Repr::Inline(value) => {
39                    Err(RuntimeError::expected::<$ty>(value.type_info()))
40                }
41                Repr::Dynamic(value) => {
42                    Err(RuntimeError::expected::<$ty>(value.type_info()))
43                }
44                Repr::Any(value) => {
45                    Err(RuntimeError::expected::<$ty>(value.type_info()))
46                }
47            }
48        }
49    }
50}
51
52macro_rules! any_from {
53    ($($ty:ty),* $(,)*) => {
54        $(
55            impl TryFrom<$ty> for Value {
56                type Error = alloc::Error;
57
58                #[inline]
59                fn try_from(value: $ty) -> Result<Self, Self::Error> {
60                    Value::new(value)
61                }
62            }
63
64            impl IntoOutput for $ty {
65                #[inline]
66                fn into_output(self) -> Result<$crate::runtime::Value, $crate::runtime::RuntimeError> {
67                    Ok(Value::new(self)?)
68                }
69            }
70        )*
71    };
72}
73
74macro_rules! inline_from {
75    ($($variant:ident => $ty:ty),* $(,)*) => {
76        $(
77            impl From<$ty> for $crate::runtime::Value {
78                #[inline]
79                fn from(value: $ty) -> Self {
80                    $crate::runtime::Value::from($crate::runtime::Inline::$variant(value))
81                }
82            }
83
84            impl From<$ty> for $crate::runtime::ConstValue {
85                #[inline]
86                fn from(value: $ty) -> Self {
87                    $crate::runtime::ConstValue::from($crate::runtime::Inline::$variant(value))
88                }
89            }
90
91            impl $crate::runtime::IntoOutput for $ty {
92                #[inline]
93                fn into_output(self) -> Result<$crate::runtime::Value, $crate::runtime::RuntimeError> {
94                    Ok($crate::runtime::Value::from(self))
95                }
96            }
97
98            impl $crate::runtime::ToValue for $ty {
99                #[inline]
100                fn to_value(self) -> Result<Value, $crate::runtime::RuntimeError> {
101                    Ok($crate::runtime::Value::from(self))
102                }
103            }
104
105            impl $crate::runtime::ToConstValue for $ty {
106                #[inline]
107                fn to_const_value(self) -> Result<$crate::runtime::ConstValue, $crate::runtime::RuntimeError> {
108                    Ok($crate::runtime::ConstValue::from(self))
109                }
110            }
111        )*
112    };
113}
114
115macro_rules! signed_value_trait {
116    ($($ty:ty),* $(,)?) => {
117        $(
118            #[allow(clippy::needless_question_mark)]
119            impl $crate::runtime::ToValue for $ty {
120                #[inline]
121                fn to_value(self) -> Result<Value, $crate::runtime::RuntimeError> {
122                    Ok($crate::runtime::Value::try_from(self)?)
123                }
124            }
125
126            #[allow(clippy::needless_question_mark)]
127            impl $crate::runtime::ToConstValue for $ty {
128                #[inline]
129                fn to_const_value(self) -> Result<$crate::runtime::ConstValue, $crate::runtime::RuntimeError> {
130                    Ok($crate::runtime::ConstValue::try_from(self)?)
131                }
132            }
133        )*
134    };
135}
136
137macro_rules! signed_value_from {
138    ($($ty:ty),* $(,)?) => {
139        $(
140            impl From<$ty> for $crate::runtime::Value {
141                #[inline]
142                fn from(number: $ty) -> Self {
143                    $crate::runtime::Value::from(number as i64)
144                }
145            }
146
147            impl From<$ty> for $crate::runtime::ConstValue {
148                #[inline]
149                fn from(number: $ty) -> Self {
150                    $crate::runtime::ConstValue::from(number as i64)
151                }
152            }
153        )*
154    }
155}
156
157macro_rules! signed_value_try_from {
158    ($($ty:ty),* $(,)?) => {
159        $(
160            impl TryFrom<$ty> for Value {
161                type Error = $crate::runtime::RuntimeError;
162
163                #[inline]
164                fn try_from(value: $ty) -> Result<Self, $crate::runtime::RuntimeError> {
165                    match <i64>::try_from(value) {
166                        Ok(number) => Ok(Value::from(number)),
167                        #[allow(unreachable_patterns)]
168                        Err(..) => Err($crate::runtime::RuntimeError::from(VmErrorKind::IntegerToValueCoercionError {
169                            from: VmIntegerRepr::from(value),
170                            to: any::type_name::<i64>(),
171                        })),
172                    }
173                }
174            }
175
176            impl TryFrom<$ty> for ConstValue {
177                type Error = $crate::runtime::RuntimeError;
178
179                #[inline]
180                fn try_from(value: $ty) -> Result<Self, $crate::runtime::RuntimeError> {
181                    match <i64>::try_from(value) {
182                        Ok(number) => Ok($crate::runtime::ConstValue::from(number)),
183                        #[allow(unreachable_patterns)]
184                        Err(..) => Err($crate::runtime::RuntimeError::from(VmErrorKind::IntegerToValueCoercionError {
185                            from: VmIntegerRepr::from(value),
186                            to: any::type_name::<i64>(),
187                        })),
188                    }
189                }
190            }
191        )*
192    }
193}
194
195macro_rules! unsigned_value_trait {
196    ($($ty:ty),* $(,)?) => {
197        $(
198            #[allow(clippy::needless_question_mark)]
199            impl $crate::runtime::ToValue for $ty {
200                #[inline]
201                fn to_value(self) -> Result<Value, $crate::runtime::RuntimeError> {
202                    Ok($crate::runtime::Value::try_from(self)?)
203                }
204            }
205
206            #[allow(clippy::needless_question_mark)]
207            impl $crate::runtime::ToConstValue for $ty {
208                #[inline]
209                fn to_const_value(self) -> Result<$crate::runtime::ConstValue, $crate::runtime::RuntimeError> {
210                    Ok($crate::runtime::ConstValue::try_from(self)?)
211                }
212            }
213        )*
214    };
215}
216
217macro_rules! unsigned_value_from {
218    ($($ty:ty),* $(,)?) => {
219        $(
220            impl From<$ty> for Value {
221                #[inline]
222                fn from(number: $ty) -> Self {
223                    Value::from(number as u64)
224                }
225            }
226
227            impl From<$ty> for ConstValue {
228                #[inline]
229                fn from(number: $ty) -> Self {
230                    ConstValue::from(number as u64)
231                }
232            }
233        )*
234    }
235}
236
237macro_rules! unsigned_value_try_from {
238    ($($ty:ty),* $(,)?) => {
239        $(
240            impl TryFrom<$ty> for Value {
241                type Error = $crate::runtime::RuntimeError;
242
243                #[inline]
244                fn try_from(value: $ty) -> Result<Self, $crate::runtime::RuntimeError> {
245                    match <u64>::try_from(value) {
246                        Ok(number) => Ok(Value::from(number)),
247                        #[allow(unreachable_patterns)]
248                        Err(..) => Err($crate::runtime::RuntimeError::from(VmErrorKind::IntegerToValueCoercionError {
249                            from: VmIntegerRepr::from(value),
250                            to: any::type_name::<u64>(),
251                        })),
252                    }
253                }
254            }
255
256            impl TryFrom<$ty> for ConstValue {
257                type Error = $crate::runtime::RuntimeError;
258
259                #[inline]
260                fn try_from(value: $ty) -> Result<Self, $crate::runtime::RuntimeError> {
261                    match <u64>::try_from(value) {
262                        Ok(number) => Ok($crate::runtime::ConstValue::from(number)),
263                        #[allow(unreachable_patterns)]
264                        Err(..) => Err($crate::runtime::RuntimeError::from(VmErrorKind::IntegerToValueCoercionError {
265                            from: VmIntegerRepr::from(value),
266                            to: any::type_name::<u64>(),
267                        })),
268                    }
269                }
270            }
271        )*
272    }
273}
274
275macro_rules! float_value_trait {
276    ($($ty:ty),* $(,)?) => {
277        $(
278            impl $crate::runtime::ToValue for $ty {
279                #[inline]
280                fn to_value(self) -> Result<Value, $crate::runtime::RuntimeError> {
281                    Ok($crate::runtime::Value::from(self as f64))
282                }
283            }
284
285            impl From<$ty> for $crate::runtime::Value {
286                #[inline]
287                fn from(value: $ty) -> Value {
288                    $crate::runtime::Value::from(value as f64)
289                }
290            }
291
292            impl $crate::runtime::ToConstValue for $ty {
293                #[inline]
294                fn to_const_value(self) -> Result<$crate::runtime::ConstValue, $crate::runtime::RuntimeError> {
295                    Ok($crate::runtime::ConstValue::from(self as f64))
296                }
297            }
298
299            impl From<$ty> for $crate::runtime::ConstValue {
300                #[inline]
301                fn from(value: $ty) -> $crate::runtime::ConstValue {
302                    $crate::runtime::ConstValue::from(value as f64)
303                }
304            }
305        )*
306    }
307}