melodium_common/executive/value/
traits.rs

1use super::Value;
2use crate::executive::DataTrait;
3use core::{convert::TryInto, fmt::Debug, hash::Hash};
4use erased_serde::Serialize;
5use serde::ser::SerializeSeq;
6
7impl DataTrait for Value {
8    fn to_i8(&self) -> i8 {
9        match self {
10            Value::I8(val) => *val,
11            Value::Data(obj) => obj.to_i8(),
12            other => panic!("ToI8 not supported for {}", other.datatype()),
13        }
14    }
15
16    fn to_i16(&self) -> i16 {
17        match self {
18            Value::I8(val) => *val as i16,
19            Value::I16(val) => *val,
20            Value::U8(val) => *val as i16,
21            Value::Data(obj) => obj.to_i16(),
22            other => panic!("ToI16 not supported for {}", other.datatype()),
23        }
24    }
25
26    fn to_i32(&self) -> i32 {
27        match self {
28            Value::I8(val) => *val as i32,
29            Value::I16(val) => *val as i32,
30            Value::I32(val) => *val,
31            Value::U8(val) => *val as i32,
32            Value::U16(val) => *val as i32,
33            Value::Data(obj) => obj.to_i32(),
34            other => panic!("ToI32 not supported for {}", other.datatype()),
35        }
36    }
37
38    fn to_i64(&self) -> i64 {
39        match self {
40            Value::I8(val) => *val as i64,
41            Value::I16(val) => *val as i64,
42            Value::I32(val) => *val as i64,
43            Value::I64(val) => *val,
44            Value::U8(val) => *val as i64,
45            Value::U16(val) => *val as i64,
46            Value::U32(val) => *val as i64,
47            Value::Data(obj) => obj.to_i64(),
48            other => panic!("ToI64 not supported for {}", other.datatype()),
49        }
50    }
51
52    fn to_i128(&self) -> i128 {
53        match self {
54            Value::I8(val) => *val as i128,
55            Value::I16(val) => *val as i128,
56            Value::I32(val) => *val as i128,
57            Value::I64(val) => *val as i128,
58            Value::U8(val) => *val as i128,
59            Value::U16(val) => *val as i128,
60            Value::U32(val) => *val as i128,
61            Value::U64(val) => *val as i128,
62            Value::I128(val) => *val,
63            Value::Data(obj) => obj.to_i128(),
64            other => panic!("ToI128 not supported for {}", other.datatype()),
65        }
66    }
67
68    fn to_u8(&self) -> u8 {
69        match self {
70            Value::U8(val) => *val,
71            Value::Byte(val) => *val,
72            Value::Data(obj) => obj.to_u8(),
73            other => panic!("ToU8 not supported for {}", other.datatype()),
74        }
75    }
76
77    fn to_u16(&self) -> u16 {
78        match self {
79            Value::U8(val) => *val as u16,
80            Value::U16(val) => *val,
81            Value::Data(obj) => obj.to_u16(),
82            other => panic!("ToU16 not supported for {}", other.datatype()),
83        }
84    }
85
86    fn to_u32(&self) -> u32 {
87        match self {
88            Value::U8(val) => *val as u32,
89            Value::U16(val) => *val as u32,
90            Value::U32(val) => *val,
91            Value::Data(obj) => obj.to_u32(),
92            other => panic!("ToU32 not supported for {}", other.datatype()),
93        }
94    }
95
96    fn to_u64(&self) -> u64 {
97        match self {
98            Value::U8(val) => *val as u64,
99            Value::U16(val) => *val as u64,
100            Value::U32(val) => *val as u64,
101            Value::U64(val) => *val,
102            Value::Data(obj) => obj.to_u64(),
103            other => panic!("ToU64 not supported for {}", other.datatype()),
104        }
105    }
106
107    fn to_u128(&self) -> u128 {
108        match self {
109            Value::U8(val) => *val as u128,
110            Value::U16(val) => *val as u128,
111            Value::U32(val) => *val as u128,
112            Value::U64(val) => *val as u128,
113            Value::U128(val) => *val,
114            Value::Data(obj) => obj.to_u128(),
115            other => panic!("ToU128 not supported for {}", other.datatype()),
116        }
117    }
118
119    fn to_f32(&self) -> f32 {
120        match self {
121            Value::I8(val) => *val as f32,
122            Value::I16(val) => *val as f32,
123            Value::I32(val) => *val as f32,
124            Value::I64(val) => *val as f32,
125            Value::I128(val) => *val as f32,
126            Value::U8(val) => *val as f32,
127            Value::U16(val) => *val as f32,
128            Value::U32(val) => *val as f32,
129            Value::U64(val) => *val as f32,
130            Value::U128(val) => *val as f32,
131            Value::F32(val) => *val,
132            Value::F64(val) => *val as f32,
133            Value::Data(obj) => obj.to_f32(),
134            other => panic!("ToF32 not supported for {}", other.datatype()),
135        }
136    }
137
138    fn to_f64(&self) -> f64 {
139        match self {
140            Value::I8(val) => *val as f64,
141            Value::I16(val) => *val as f64,
142            Value::I32(val) => *val as f64,
143            Value::I64(val) => *val as f64,
144            Value::I128(val) => *val as f64,
145            Value::U8(val) => *val as f64,
146            Value::U16(val) => *val as f64,
147            Value::U32(val) => *val as f64,
148            Value::U64(val) => *val as f64,
149            Value::U128(val) => *val as f64,
150            Value::F32(val) => *val as f64,
151            Value::F64(val) => *val,
152            Value::Data(obj) => obj.to_f64(),
153            other => panic!("ToF64 not supported for {}", other.datatype()),
154        }
155    }
156
157    fn to_bool(&self) -> bool {
158        match self {
159            Value::I8(val) => *val != 0,
160            Value::I16(val) => *val != 0,
161            Value::I32(val) => *val != 0,
162            Value::I64(val) => *val != 0,
163            Value::I128(val) => *val != 0,
164            Value::U8(val) => *val != 0,
165            Value::U16(val) => *val != 0,
166            Value::U32(val) => *val != 0,
167            Value::U64(val) => *val != 0,
168            Value::U128(val) => *val != 0,
169            Value::Bool(val) => *val,
170            Value::Byte(val) => *val != 0,
171            Value::Data(obj) => obj.to_bool(),
172            other => panic!("ToBool not supported for {}", other.datatype()),
173        }
174    }
175
176    fn to_byte(&self) -> u8 {
177        match self {
178            Value::U8(val) => *val,
179            Value::Bool(val) => *val as u8,
180            Value::Byte(val) => *val,
181            Value::Data(obj) => obj.to_byte(),
182            other => panic!("ToByte not supported for {}", other.datatype()),
183        }
184    }
185
186    fn to_char(&self) -> char {
187        match self {
188            Value::Char(val) => *val,
189            Value::Data(obj) => obj.to_char(),
190            other => panic!("ToChar not supported for {}", other.datatype()),
191        }
192    }
193
194    fn to_string(&self) -> String {
195        match self {
196            Value::Void(_val) => String::new(),
197            Value::I8(val) => val.to_string(),
198            Value::I16(val) => val.to_string(),
199            Value::I32(val) => val.to_string(),
200            Value::I64(val) => val.to_string(),
201            Value::I128(val) => val.to_string(),
202            Value::U8(val) => val.to_string(),
203            Value::U16(val) => val.to_string(),
204            Value::U32(val) => val.to_string(),
205            Value::U64(val) => val.to_string(),
206            Value::U128(val) => val.to_string(),
207            Value::F32(val) => val.to_string(),
208            Value::F64(val) => val.to_string(),
209            Value::Bool(val) => val.to_string(),
210            Value::Byte(val) => hex::encode([*val]),
211            Value::Char(val) => val.to_string(),
212            Value::String(val) => val.clone(),
213            Value::Data(obj) => obj.to_string(),
214            other => panic!("ToString not supported for {}", other.datatype()),
215        }
216    }
217
218    fn try_to_i8(&self) -> Option<i8> {
219        match self {
220            Value::I8(val) => Some(*val),
221            Value::I16(val) => TryInto::<i8>::try_into(*val).ok(),
222
223            Value::I32(val) => TryInto::<i8>::try_into(*val).ok(),
224            Value::I64(val) => TryInto::<i8>::try_into(*val).ok(),
225            Value::I128(val) => TryInto::<i8>::try_into(*val).ok(),
226            Value::U8(val) => TryInto::<i8>::try_into(*val).ok(),
227            Value::U16(val) => TryInto::<i8>::try_into(*val).ok(),
228            Value::U32(val) => TryInto::<i8>::try_into(*val).ok(),
229            Value::U64(val) => TryInto::<i8>::try_into(*val).ok(),
230            Value::U128(val) => TryInto::<i8>::try_into(*val).ok(),
231            Value::F32(val) => {
232                if val.is_finite() && *val <= i8::MAX as f32 && *val >= i8::MIN as f32 {
233                    Some(*val as i8)
234                } else {
235                    None
236                }
237            }
238
239            Value::F64(val) => {
240                if val.is_finite() && *val <= i8::MAX as f64 && *val >= i8::MIN as f64 {
241                    Some(*val as i8)
242                } else {
243                    None
244                }
245            }
246
247            Value::Data(obj) => obj.try_to_i8(),
248
249            other => panic!("TryToI8 not supported for {}", other.datatype()),
250        }
251    }
252
253    fn try_to_i16(&self) -> Option<i16> {
254        match self {
255            Value::I8(val) => Some(*val as i16),
256            Value::I16(val) => Some(*val),
257            Value::I32(val) => TryInto::<i16>::try_into(*val).ok(),
258            Value::I64(val) => TryInto::<i16>::try_into(*val).ok(),
259            Value::I128(val) => TryInto::<i16>::try_into(*val).ok(),
260            Value::U8(val) => TryInto::<i16>::try_into(*val).ok(),
261            Value::U16(val) => TryInto::<i16>::try_into(*val).ok(),
262            Value::U32(val) => TryInto::<i16>::try_into(*val).ok(),
263            Value::U64(val) => TryInto::<i16>::try_into(*val).ok(),
264            Value::U128(val) => TryInto::<i16>::try_into(*val).ok(),
265            Value::F32(val) => {
266                if val.is_finite() && *val <= i16::MAX as f32 && *val >= i16::MIN as f32 {
267                    Some(*val as i16)
268                } else {
269                    None
270                }
271            }
272
273            Value::F64(val) => {
274                if val.is_finite() && *val <= i16::MAX as f64 && *val >= i16::MIN as f64 {
275                    Some(*val as i16)
276                } else {
277                    None
278                }
279            }
280
281            Value::Data(obj) => obj.try_to_i16(),
282
283            other => panic!("TryToI16 not supported for {}", other.datatype()),
284        }
285    }
286
287    fn try_to_i32(&self) -> Option<i32> {
288        match self {
289            Value::I8(val) => Some(*val as i32),
290            Value::I16(val) => Some(*val as i32),
291            Value::I32(val) => Some(*val),
292            Value::I64(val) => TryInto::<i32>::try_into(*val).ok(),
293            Value::I128(val) => TryInto::<i32>::try_into(*val).ok(),
294            Value::U8(val) => TryInto::<i32>::try_into(*val).ok(),
295            Value::U16(val) => TryInto::<i32>::try_into(*val).ok(),
296            Value::U32(val) => TryInto::<i32>::try_into(*val).ok(),
297            Value::U64(val) => TryInto::<i32>::try_into(*val).ok(),
298            Value::U128(val) => TryInto::<i32>::try_into(*val).ok(),
299            Value::F32(val) => {
300                if val.is_finite() && *val <= i32::MAX as f32 && *val >= i32::MIN as f32 {
301                    Some(*val as i32)
302                } else {
303                    None
304                }
305            }
306
307            Value::F64(val) => {
308                if val.is_finite() && *val <= i32::MAX as f64 && *val >= i32::MIN as f64 {
309                    Some(*val as i32)
310                } else {
311                    None
312                }
313            }
314
315            Value::Data(obj) => obj.try_to_i32(),
316
317            other => panic!("TryToI32 not supported for {}", other.datatype()),
318        }
319    }
320
321    fn try_to_i64(&self) -> Option<i64> {
322        match self {
323            Value::I8(val) => Some(*val as i64),
324            Value::I16(val) => Some(*val as i64),
325            Value::I32(val) => Some(*val as i64),
326            Value::I64(val) => Some(*val),
327            Value::I128(val) => TryInto::<i64>::try_into(*val).ok(),
328            Value::U8(val) => TryInto::<i64>::try_into(*val).ok(),
329            Value::U16(val) => TryInto::<i64>::try_into(*val).ok(),
330            Value::U32(val) => TryInto::<i64>::try_into(*val).ok(),
331            Value::U64(val) => TryInto::<i64>::try_into(*val).ok(),
332            Value::U128(val) => TryInto::<i64>::try_into(*val).ok(),
333            Value::F32(val) => {
334                if val.is_finite() && *val <= i64::MAX as f32 && *val >= i64::MIN as f32 {
335                    Some(*val as i64)
336                } else {
337                    None
338                }
339            }
340
341            Value::F64(val) => {
342                if val.is_finite() && *val <= i64::MAX as f64 && *val >= i64::MIN as f64 {
343                    Some(*val as i64)
344                } else {
345                    None
346                }
347            }
348
349            Value::Data(obj) => obj.try_to_i64(),
350
351            other => panic!("TryToI64 not supported for {}", other.datatype()),
352        }
353    }
354
355    fn try_to_i128(&self) -> Option<i128> {
356        match self {
357            Value::I8(val) => Some(*val as i128),
358            Value::I16(val) => Some(*val as i128),
359            Value::I32(val) => Some(*val as i128),
360            Value::I64(val) => Some(*val as i128),
361            Value::I128(val) => Some(*val),
362            Value::U8(val) => TryInto::<i128>::try_into(*val).ok(),
363            Value::U16(val) => TryInto::<i128>::try_into(*val).ok(),
364            Value::U32(val) => TryInto::<i128>::try_into(*val).ok(),
365            Value::U64(val) => TryInto::<i128>::try_into(*val).ok(),
366            Value::U128(val) => TryInto::<i128>::try_into(*val).ok(),
367            Value::F32(val) => {
368                if val.is_finite() && *val <= i128::MAX as f32 && *val >= i128::MIN as f32 {
369                    Some(*val as i128)
370                } else {
371                    None
372                }
373            }
374
375            Value::F64(val) => {
376                if val.is_finite() && *val <= i128::MAX as f64 && *val >= i128::MIN as f64 {
377                    Some(*val as i128)
378                } else {
379                    None
380                }
381            }
382
383            Value::Data(obj) => obj.try_to_i128(),
384
385            other => panic!("TryToI128 not supported for {}", other.datatype()),
386        }
387    }
388
389    fn try_to_u8(&self) -> Option<u8> {
390        match self {
391            Value::I8(val) => TryInto::<u8>::try_into(*val).ok(),
392            Value::I16(val) => TryInto::<u8>::try_into(*val).ok(),
393            Value::I32(val) => TryInto::<u8>::try_into(*val).ok(),
394            Value::I64(val) => TryInto::<u8>::try_into(*val).ok(),
395            Value::I128(val) => TryInto::<u8>::try_into(*val).ok(),
396            Value::U8(val) => Some(*val),
397            Value::U16(val) => TryInto::<u8>::try_into(*val).ok(),
398            Value::U32(val) => TryInto::<u8>::try_into(*val).ok(),
399            Value::U64(val) => TryInto::<u8>::try_into(*val).ok(),
400            Value::U128(val) => TryInto::<u8>::try_into(*val).ok(),
401            Value::F32(val) => {
402                if val.is_finite() && *val <= u8::MAX as f32 && *val >= u8::MIN as f32 {
403                    Some(*val as u8)
404                } else {
405                    None
406                }
407            }
408
409            Value::F64(val) => {
410                if val.is_finite() && *val <= u8::MAX as f64 && *val >= u8::MIN as f64 {
411                    Some(*val as u8)
412                } else {
413                    None
414                }
415            }
416
417            Value::Data(obj) => obj.try_to_u8(),
418
419            other => panic!("TryToU8 not supported for {}", other.datatype()),
420        }
421    }
422
423    fn try_to_u16(&self) -> Option<u16> {
424        match self {
425            Value::I8(val) => TryInto::<u16>::try_into(*val).ok(),
426            Value::I16(val) => TryInto::<u16>::try_into(*val).ok(),
427            Value::I32(val) => TryInto::<u16>::try_into(*val).ok(),
428            Value::I64(val) => TryInto::<u16>::try_into(*val).ok(),
429            Value::I128(val) => TryInto::<u16>::try_into(*val).ok(),
430            Value::U8(val) => Some(*val as u16),
431            Value::U16(val) => Some(*val),
432            Value::U32(val) => TryInto::<u16>::try_into(*val).ok(),
433            Value::U64(val) => TryInto::<u16>::try_into(*val).ok(),
434            Value::U128(val) => TryInto::<u16>::try_into(*val).ok(),
435            Value::F32(val) => {
436                if val.is_finite() && *val <= u16::MAX as f32 && *val >= u16::MIN as f32 {
437                    Some(*val as u16)
438                } else {
439                    None
440                }
441            }
442
443            Value::F64(val) => {
444                if val.is_finite() && *val <= u16::MAX as f64 && *val >= u16::MIN as f64 {
445                    Some(*val as u16)
446                } else {
447                    None
448                }
449            }
450
451            Value::Data(obj) => obj.try_to_u16(),
452
453            other => panic!("TryToU16 not supported for {}", other.datatype()),
454        }
455    }
456
457    fn try_to_u32(&self) -> Option<u32> {
458        match self {
459            Value::I8(val) => TryInto::<u32>::try_into(*val).ok(),
460            Value::I16(val) => TryInto::<u32>::try_into(*val).ok(),
461            Value::I32(val) => TryInto::<u32>::try_into(*val).ok(),
462            Value::I64(val) => TryInto::<u32>::try_into(*val).ok(),
463            Value::I128(val) => TryInto::<u32>::try_into(*val).ok(),
464            Value::U8(val) => Some(*val as u32),
465            Value::U16(val) => Some(*val as u32),
466            Value::U32(val) => Some(*val),
467            Value::U64(val) => TryInto::<u32>::try_into(*val).ok(),
468            Value::U128(val) => TryInto::<u32>::try_into(*val).ok(),
469            Value::F32(val) => {
470                if val.is_finite() && *val <= u32::MAX as f32 && *val >= u32::MIN as f32 {
471                    Some(*val as u32)
472                } else {
473                    None
474                }
475            }
476
477            Value::F64(val) => {
478                if val.is_finite() && *val <= u32::MAX as f64 && *val >= u32::MIN as f64 {
479                    Some(*val as u32)
480                } else {
481                    None
482                }
483            }
484
485            Value::Data(obj) => obj.try_to_u32(),
486
487            other => panic!("TryToU32 not supported for {}", other.datatype()),
488        }
489    }
490
491    fn try_to_u64(&self) -> Option<u64> {
492        match self {
493            Value::I8(val) => TryInto::<u64>::try_into(*val).ok(),
494            Value::I16(val) => TryInto::<u64>::try_into(*val).ok(),
495            Value::I32(val) => TryInto::<u64>::try_into(*val).ok(),
496            Value::I64(val) => TryInto::<u64>::try_into(*val).ok(),
497            Value::I128(val) => TryInto::<u64>::try_into(*val).ok(),
498            Value::U8(val) => Some(*val as u64),
499            Value::U16(val) => Some(*val as u64),
500            Value::U32(val) => Some(*val as u64),
501            Value::U64(val) => Some(*val),
502            Value::U128(val) => TryInto::<u64>::try_into(*val).ok(),
503            Value::F32(val) => {
504                if val.is_finite() && *val <= u64::MAX as f32 && *val >= u64::MIN as f32 {
505                    Some(*val as u64)
506                } else {
507                    None
508                }
509            }
510
511            Value::F64(val) => {
512                if val.is_finite() && *val <= u64::MAX as f64 && *val >= u64::MIN as f64 {
513                    Some(*val as u64)
514                } else {
515                    None
516                }
517            }
518
519            Value::Data(obj) => obj.try_to_u64(),
520
521            other => panic!("TryToU64 not supported for {}", other.datatype()),
522        }
523    }
524
525    fn try_to_u128(&self) -> Option<u128> {
526        match self {
527            Value::I8(val) => TryInto::<u128>::try_into(*val).ok(),
528            Value::I16(val) => TryInto::<u128>::try_into(*val).ok(),
529            Value::I32(val) => TryInto::<u128>::try_into(*val).ok(),
530            Value::I64(val) => TryInto::<u128>::try_into(*val).ok(),
531            Value::I128(val) => TryInto::<u128>::try_into(*val).ok(),
532            Value::U8(val) => Some(*val as u128),
533            Value::U16(val) => Some(*val as u128),
534            Value::U32(val) => Some(*val as u128),
535            Value::U64(val) => Some(*val as u128),
536            Value::U128(val) => Some(*val),
537            Value::F32(val) => {
538                if val.is_finite() && *val <= u128::MAX as f32 && *val >= u128::MIN as f32 {
539                    Some(*val as u128)
540                } else {
541                    None
542                }
543            }
544
545            Value::F64(val) => {
546                if val.is_finite() && *val <= u128::MAX as f64 && *val >= u128::MIN as f64 {
547                    Some(*val as u128)
548                } else {
549                    None
550                }
551            }
552
553            Value::Data(obj) => obj.try_to_u128(),
554
555            other => panic!("TryToU128 not supported for {}", other.datatype()),
556        }
557    }
558
559    fn try_to_f32(&self) -> Option<f32> {
560        match self {
561            Value::I8(val) => Some(*val as f32),
562            Value::I16(val) => Some(*val as f32),
563            Value::I32(val) => Some(*val as f32),
564            Value::I64(val) => Some(*val as f32),
565            Value::I128(val) => Some(*val as f32),
566            Value::U8(val) => Some(*val as f32),
567            Value::U16(val) => Some(*val as f32),
568            Value::U32(val) => Some(*val as f32),
569            Value::U64(val) => Some(*val as f32),
570            Value::U128(val) => Some(*val as f32),
571            Value::F32(val) => Some(*val),
572            Value::F64(val) => Some(*val as f32),
573            Value::Data(obj) => obj.try_to_f32(),
574            other => panic!("TryToF32 not supported for {}", other.datatype()),
575        }
576    }
577
578    fn try_to_f64(&self) -> Option<f64> {
579        match self {
580            Value::I8(val) => Some(*val as f64),
581            Value::I16(val) => Some(*val as f64),
582            Value::I32(val) => Some(*val as f64),
583            Value::I64(val) => Some(*val as f64),
584            Value::I128(val) => Some(*val as f64),
585            Value::U8(val) => Some(*val as f64),
586            Value::U16(val) => Some(*val as f64),
587            Value::U32(val) => Some(*val as f64),
588            Value::U64(val) => Some(*val as f64),
589            Value::U128(val) => Some(*val as f64),
590            Value::F32(val) => Some(*val as f64),
591            Value::F64(val) => Some(*val),
592            Value::Data(obj) => obj.try_to_f64(),
593            other => panic!("TryToF32 not supported for {}", other.datatype()),
594        }
595    }
596
597    fn try_to_bool(&self) -> Option<bool> {
598        match self {
599            Value::I8(val) => Some(*val != 0),
600            Value::I16(val) => Some(*val != 0),
601            Value::I32(val) => Some(*val != 0),
602            Value::I64(val) => Some(*val != 0),
603            Value::I128(val) => Some(*val != 0),
604            Value::U8(val) => Some(*val != 0),
605            Value::U16(val) => Some(*val != 0),
606            Value::U32(val) => Some(*val != 0),
607            Value::U64(val) => Some(*val != 0),
608            Value::U128(val) => Some(*val != 0),
609            Value::Bool(val) => Some(*val),
610            Value::Byte(val) => Some(*val != 0),
611            Value::Data(obj) => obj.try_to_bool(),
612            other => panic!("TryToBool not supported for {}", other.datatype()),
613        }
614    }
615
616    fn try_to_byte(&self) -> Option<u8> {
617        match self {
618            Value::U8(val) => Some(*val),
619            Value::Bool(val) => Some(*val as u8),
620            Value::Byte(val) => Some(*val),
621            Value::Data(obj) => obj.try_to_byte(),
622            other => panic!("TryToByte not supported for {}", other.datatype()),
623        }
624    }
625
626    fn try_to_char(&self) -> Option<char> {
627        match self {
628            Value::Char(val) => Some(*val),
629            Value::Data(obj) => obj.try_to_char(),
630            other => panic!("TryToChar not supported for {}", other.datatype()),
631        }
632    }
633
634    fn try_to_string(&self) -> Option<String> {
635        match self {
636            Value::Void(_val) => Some(String::new()),
637            Value::I8(val) => Some(val.to_string()),
638            Value::I16(val) => Some(val.to_string()),
639            Value::I32(val) => Some(val.to_string()),
640            Value::I64(val) => Some(val.to_string()),
641            Value::I128(val) => Some(val.to_string()),
642            Value::U8(val) => Some(val.to_string()),
643            Value::U16(val) => Some(val.to_string()),
644            Value::U32(val) => Some(val.to_string()),
645            Value::U64(val) => Some(val.to_string()),
646            Value::U128(val) => Some(val.to_string()),
647            Value::F32(val) => Some(val.to_string()),
648            Value::F64(val) => Some(val.to_string()),
649            Value::Bool(val) => Some(val.to_string()),
650            Value::Byte(val) => Some(hex::encode([*val])),
651            Value::Char(val) => Some(val.to_string()),
652            Value::String(val) => Some(val.clone()),
653            Value::Data(obj) => obj.try_to_string(),
654            other => panic!("TryToString not supported for {}", other.datatype()),
655        }
656    }
657
658    fn saturating_to_i8(&self) -> i8 {
659        match self {
660            Value::I8(val) => *val,
661            Value::I16(val) => match *val {
662                val if val < i8::MIN as i16 => i8::MIN,
663                val if val > i8::MAX as i16 => i8::MAX,
664                val => val as i8,
665            },
666            Value::I32(val) => match *val {
667                val if val < i8::MIN as i32 => i8::MIN,
668                val if val > i8::MAX as i32 => i8::MAX,
669                val => val as i8,
670            },
671            Value::I64(val) => match *val {
672                val if val < i8::MIN as i64 => i8::MIN,
673                val if val > i8::MAX as i64 => i8::MAX,
674                val => val as i8,
675            },
676            Value::I128(val) => match *val {
677                val if val < i8::MIN as i128 => i8::MIN,
678                val if val > i8::MAX as i128 => i8::MAX,
679                val => val as i8,
680            },
681            Value::U8(val) => match *val {
682                val if val > i8::MAX as u8 => i8::MAX,
683                val => val as i8,
684            },
685            Value::U16(val) => match *val {
686                val if val > i8::MAX as u16 => i8::MAX,
687                val => val as i8,
688            },
689            Value::U32(val) => match *val {
690                val if val > i8::MAX as u32 => i8::MAX,
691                val => val as i8,
692            },
693            Value::U64(val) => match *val {
694                val if val > i8::MAX as u64 => i8::MAX,
695                val => val as i8,
696            },
697            Value::U128(val) => match *val {
698                val if val > i8::MAX as u128 => i8::MAX,
699                val => val as i8,
700            },
701            Value::F32(val) => {
702                if val.is_nan() {
703                    0
704                } else if *val < i8::MIN as f32 {
705                    i8::MIN
706                } else if *val > i8::MAX as f32 {
707                    i8::MAX
708                } else {
709                    *val as i8
710                }
711            }
712            Value::F64(val) => {
713                if val.is_nan() {
714                    0
715                } else if *val < i8::MIN as f64 {
716                    i8::MIN
717                } else if *val > i8::MAX as f64 {
718                    i8::MAX
719                } else {
720                    *val as i8
721                }
722            }
723            Value::Data(obj) => obj.saturating_to_i8(),
724            other => panic!("SaturatingToI8 not supported for {}", other.datatype()),
725        }
726    }
727
728    fn saturating_to_i16(&self) -> i16 {
729        match self {
730            Value::I8(val) => *val as i16,
731            Value::I16(val) => *val,
732            Value::I32(val) => match *val {
733                val if val < i16::MIN as i32 => i16::MIN,
734                val if val > i16::MAX as i32 => i16::MAX,
735                val => val as i16,
736            },
737            Value::I64(val) => match *val {
738                val if val < i16::MIN as i64 => i16::MIN,
739                val if val > i16::MAX as i64 => i16::MAX,
740                val => val as i16,
741            },
742            Value::I128(val) => match *val {
743                val if val < i16::MIN as i128 => i16::MIN,
744                val if val > i16::MAX as i128 => i16::MAX,
745                val => val as i16,
746            },
747            Value::U8(val) => *val as i16,
748            Value::U16(val) => match *val {
749                val if val > i16::MAX as u16 => i16::MAX,
750                val => val as i16,
751            },
752            Value::U32(val) => match *val {
753                val if val > i16::MAX as u32 => i16::MAX,
754                val => val as i16,
755            },
756            Value::U64(val) => match *val {
757                val if val > i16::MAX as u64 => i16::MAX,
758                val => val as i16,
759            },
760            Value::U128(val) => match *val {
761                val if val > i16::MAX as u128 => i16::MAX,
762                val => val as i16,
763            },
764            Value::F32(val) => {
765                if val.is_nan() {
766                    0
767                } else if *val < i16::MIN as f32 {
768                    i16::MIN
769                } else if *val > i16::MAX as f32 {
770                    i16::MAX
771                } else {
772                    *val as i16
773                }
774            }
775            Value::F64(val) => {
776                if val.is_nan() {
777                    0
778                } else if *val < i16::MIN as f64 {
779                    i16::MIN
780                } else if *val > i16::MAX as f64 {
781                    i16::MAX
782                } else {
783                    *val as i16
784                }
785            }
786            Value::Data(obj) => obj.saturating_to_i16(),
787            other => panic!("SaturatingToI16 not supported for {}", other.datatype()),
788        }
789    }
790
791    fn saturating_to_i32(&self) -> i32 {
792        match self {
793            Value::I8(val) => *val as i32,
794            Value::I16(val) => *val as i32,
795            Value::I32(val) => *val,
796            Value::I64(val) => match *val {
797                val if val < i32::MIN as i64 => i32::MIN,
798                val if val > i32::MAX as i64 => i32::MAX,
799                val => val as i32,
800            },
801            Value::I128(val) => match *val {
802                val if val < i32::MIN as i128 => i32::MIN,
803                val if val > i32::MAX as i128 => i32::MAX,
804                val => val as i32,
805            },
806            Value::U8(val) => *val as i32,
807            Value::U16(val) => *val as i32,
808            Value::U32(val) => match *val {
809                val if val > i32::MAX as u32 => i32::MAX,
810                val => val as i32,
811            },
812            Value::U64(val) => match *val {
813                val if val > i32::MAX as u64 => i32::MAX,
814                val => val as i32,
815            },
816            Value::U128(val) => match *val {
817                val if val > i32::MAX as u128 => i32::MAX,
818                val => val as i32,
819            },
820            Value::F32(val) => {
821                if val.is_nan() {
822                    0
823                } else if *val < i32::MIN as f32 {
824                    i32::MIN
825                } else if *val > i32::MAX as f32 {
826                    i32::MAX
827                } else {
828                    *val as i32
829                }
830            }
831            Value::F64(val) => {
832                if val.is_nan() {
833                    0
834                } else if *val < i32::MIN as f64 {
835                    i32::MIN
836                } else if *val > i32::MAX as f64 {
837                    i32::MAX
838                } else {
839                    *val as i32
840                }
841            }
842            Value::Data(obj) => obj.saturating_to_i32(),
843            other => panic!("SaturatingToI32 not supported for {}", other.datatype()),
844        }
845    }
846
847    fn saturating_to_i64(&self) -> i64 {
848        match self {
849            Value::I8(val) => *val as i64,
850            Value::I16(val) => *val as i64,
851            Value::I32(val) => *val as i64,
852            Value::I64(val) => *val,
853            Value::I128(val) => match *val {
854                val if val < i64::MIN as i128 => i64::MIN,
855                val if val > i64::MAX as i128 => i64::MAX,
856                val => val as i64,
857            },
858            Value::U8(val) => *val as i64,
859            Value::U16(val) => *val as i64,
860            Value::U32(val) => *val as i64,
861            Value::U64(val) => match *val {
862                val if val > i64::MAX as u64 => i64::MAX,
863                val => val as i64,
864            },
865            Value::U128(val) => match *val {
866                val if val > i64::MAX as u128 => i64::MAX,
867                val => val as i64,
868            },
869            Value::F32(val) => {
870                if val.is_nan() {
871                    0
872                } else if *val < i64::MIN as f32 {
873                    i64::MIN
874                } else if *val > i64::MAX as f32 {
875                    i64::MAX
876                } else {
877                    *val as i64
878                }
879            }
880            Value::F64(val) => {
881                if val.is_nan() {
882                    0
883                } else if *val < i64::MIN as f64 {
884                    i64::MIN
885                } else if *val > i64::MAX as f64 {
886                    i64::MAX
887                } else {
888                    *val as i64
889                }
890            }
891            Value::Data(obj) => obj.saturating_to_i64(),
892            other => panic!("SaturatingToI64 not supported for {}", other.datatype()),
893        }
894    }
895
896    fn saturating_to_i128(&self) -> i128 {
897        match self {
898            Value::I8(val) => *val as i128,
899            Value::I16(val) => *val as i128,
900            Value::I32(val) => *val as i128,
901            Value::I64(val) => *val as i128,
902            Value::I128(val) => *val,
903            Value::U8(val) => *val as i128,
904            Value::U16(val) => *val as i128,
905            Value::U32(val) => *val as i128,
906            Value::U64(val) => *val as i128,
907            Value::U128(val) => match *val {
908                val if val > i128::MAX as u128 => i128::MAX,
909                val => val as i128,
910            },
911            Value::F32(val) => {
912                if val.is_nan() {
913                    0
914                } else if *val < i128::MIN as f32 {
915                    i128::MIN
916                } else if *val > i128::MAX as f32 {
917                    i128::MAX
918                } else {
919                    *val as i128
920                }
921            }
922            Value::F64(val) => {
923                if val.is_nan() {
924                    0
925                } else if *val < i128::MIN as f64 {
926                    i128::MIN
927                } else if *val > i128::MAX as f64 {
928                    i128::MAX
929                } else {
930                    *val as i128
931                }
932            }
933            Value::Data(obj) => obj.saturating_to_i128(),
934            other => panic!("SaturatingToI128 not supported for {}", other.datatype()),
935        }
936    }
937
938    fn saturating_to_u8(&self) -> u8 {
939        match self {
940            Value::I8(val) => match *val {
941                val if val < u8::MIN as i8 => u8::MIN,
942                val => val as u8,
943            },
944            Value::I16(val) => match *val {
945                val if val < u8::MIN as i16 => u8::MIN,
946                val if val > u8::MAX as i16 => u8::MAX,
947                val => val as u8,
948            },
949            Value::I32(val) => match *val {
950                val if val < u8::MIN as i32 => u8::MIN,
951                val if val > u8::MAX as i32 => u8::MAX,
952                val => val as u8,
953            },
954            Value::I64(val) => match *val {
955                val if val < u8::MIN as i64 => u8::MIN,
956                val if val > u8::MAX as i64 => u8::MAX,
957                val => val as u8,
958            },
959            Value::I128(val) => match *val {
960                val if val < u8::MIN as i128 => u8::MIN,
961                val if val > u8::MAX as i128 => u8::MAX,
962                val => val as u8,
963            },
964            Value::U8(val) => *val,
965            Value::U16(val) => match *val {
966                val if val > u8::MAX as u16 => u8::MAX,
967                val => val as u8,
968            },
969            Value::U32(val) => match *val {
970                val if val > u8::MAX as u32 => u8::MAX,
971                val => val as u8,
972            },
973            Value::U64(val) => match *val {
974                val if val > u8::MAX as u64 => u8::MAX,
975                val => val as u8,
976            },
977            Value::U128(val) => match *val {
978                val if val > u8::MAX as u128 => u8::MAX,
979                val => val as u8,
980            },
981            Value::F32(val) => {
982                if val.is_nan() {
983                    0
984                } else if *val < u8::MIN as f32 {
985                    u8::MIN
986                } else if *val > u8::MAX as f32 {
987                    u8::MAX
988                } else {
989                    *val as u8
990                }
991            }
992            Value::F64(val) => {
993                if val.is_nan() {
994                    0
995                } else if *val < u8::MIN as f64 {
996                    u8::MIN
997                } else if *val > u8::MAX as f64 {
998                    u8::MAX
999                } else {
1000                    *val as u8
1001                }
1002            }
1003            Value::Data(obj) => obj.saturating_to_u8(),
1004            other => panic!("SaturatingToU8 not supported for {}", other.datatype()),
1005        }
1006    }
1007
1008    fn saturating_to_u16(&self) -> u16 {
1009        match self {
1010            Value::I8(val) => match *val {
1011                val if val < u16::MIN as i8 => u16::MIN,
1012                val => val as u16,
1013            },
1014            Value::I16(val) => match *val {
1015                val if val < u16::MIN as i16 => u16::MIN,
1016                val => val as u16,
1017            },
1018            Value::I32(val) => match *val {
1019                val if val < u16::MIN as i32 => u16::MIN,
1020                val if val > u16::MAX as i32 => u16::MAX,
1021                val => val as u16,
1022            },
1023            Value::I64(val) => match *val {
1024                val if val < u16::MIN as i64 => u16::MIN,
1025                val if val > u16::MAX as i64 => u16::MAX,
1026                val => val as u16,
1027            },
1028            Value::I128(val) => match *val {
1029                val if val < u16::MIN as i128 => u16::MIN,
1030                val if val > u16::MAX as i128 => u16::MAX,
1031                val => val as u16,
1032            },
1033            Value::U8(val) => *val as u16,
1034            Value::U16(val) => *val,
1035            Value::U32(val) => match *val {
1036                val if val > u16::MAX as u32 => u16::MAX,
1037                val => val as u16,
1038            },
1039            Value::U64(val) => match *val {
1040                val if val > u16::MAX as u64 => u16::MAX,
1041                val => val as u16,
1042            },
1043            Value::U128(val) => match *val {
1044                val if val > u16::MAX as u128 => u16::MAX,
1045                val => val as u16,
1046            },
1047            Value::F32(val) => {
1048                if val.is_nan() {
1049                    0
1050                } else if *val < u16::MIN as f32 {
1051                    u16::MIN
1052                } else if *val > u16::MAX as f32 {
1053                    u16::MAX
1054                } else {
1055                    *val as u16
1056                }
1057            }
1058            Value::F64(val) => {
1059                if val.is_nan() {
1060                    0
1061                } else if *val < u16::MIN as f64 {
1062                    u16::MIN
1063                } else if *val > u16::MAX as f64 {
1064                    u16::MAX
1065                } else {
1066                    *val as u16
1067                }
1068            }
1069            Value::Data(obj) => obj.saturating_to_u16(),
1070            other => panic!("SaturatingToU16 not supported for {}", other.datatype()),
1071        }
1072    }
1073
1074    fn saturating_to_u32(&self) -> u32 {
1075        match self {
1076            Value::I8(val) => match *val {
1077                val if val < u32::MIN as i8 => u32::MIN,
1078                val => val as u32,
1079            },
1080            Value::I16(val) => match *val {
1081                val if val < u32::MIN as i16 => u32::MIN,
1082                val => val as u32,
1083            },
1084            Value::I32(val) => match *val {
1085                val if val < u32::MIN as i32 => u32::MIN,
1086                val => val as u32,
1087            },
1088            Value::I64(val) => match *val {
1089                val if val < u32::MIN as i64 => u32::MIN,
1090                val if val > u32::MAX as i64 => u32::MAX,
1091                val => val as u32,
1092            },
1093            Value::I128(val) => match *val {
1094                val if val < u32::MIN as i128 => u32::MIN,
1095                val if val > u32::MAX as i128 => u32::MAX,
1096                val => val as u32,
1097            },
1098            Value::U8(val) => *val as u32,
1099            Value::U16(val) => *val as u32,
1100            Value::U32(val) => *val,
1101            Value::U64(val) => match *val {
1102                val if val > u32::MAX as u64 => u32::MAX,
1103                val => val as u32,
1104            },
1105            Value::U128(val) => match *val {
1106                val if val > u32::MAX as u128 => u32::MAX,
1107                val => val as u32,
1108            },
1109            Value::F32(val) => {
1110                if val.is_nan() {
1111                    0
1112                } else if *val < u32::MIN as f32 {
1113                    u32::MIN
1114                } else if *val > u32::MAX as f32 {
1115                    u32::MAX
1116                } else {
1117                    *val as u32
1118                }
1119            }
1120            Value::F64(val) => {
1121                if val.is_nan() {
1122                    0
1123                } else if *val < u32::MIN as f64 {
1124                    u32::MIN
1125                } else if *val > u32::MAX as f64 {
1126                    u32::MAX
1127                } else {
1128                    *val as u32
1129                }
1130            }
1131            Value::Data(obj) => obj.saturating_to_u32(),
1132            other => panic!("SaturatingToU32 not supported for {}", other.datatype()),
1133        }
1134    }
1135
1136    fn saturating_to_u64(&self) -> u64 {
1137        match self {
1138            Value::I8(val) => match *val {
1139                val if val < u64::MIN as i8 => u64::MIN,
1140                val => val as u64,
1141            },
1142            Value::I16(val) => match *val {
1143                val if val < u64::MIN as i16 => u64::MIN,
1144                val => val as u64,
1145            },
1146            Value::I32(val) => match *val {
1147                val if val < u64::MIN as i32 => u64::MIN,
1148                val => val as u64,
1149            },
1150            Value::I64(val) => match *val {
1151                val if val < u64::MIN as i64 => u64::MIN,
1152                val => val as u64,
1153            },
1154            Value::I128(val) => match *val {
1155                val if val < u64::MIN as i128 => u64::MIN,
1156                val if val > u64::MAX as i128 => u64::MAX,
1157                val => val as u64,
1158            },
1159            Value::U8(val) => *val as u64,
1160            Value::U16(val) => *val as u64,
1161            Value::U32(val) => *val as u64,
1162            Value::U64(val) => *val,
1163            Value::U128(val) => match *val {
1164                val if val > u64::MAX as u128 => u64::MAX,
1165                val => val as u64,
1166            },
1167            Value::F32(val) => {
1168                if val.is_nan() {
1169                    0
1170                } else if *val < u64::MIN as f32 {
1171                    u64::MIN
1172                } else if *val > u64::MAX as f32 {
1173                    u64::MAX
1174                } else {
1175                    *val as u64
1176                }
1177            }
1178            Value::F64(val) => {
1179                if val.is_nan() {
1180                    0
1181                } else if *val < u64::MIN as f64 {
1182                    u64::MIN
1183                } else if *val > u64::MAX as f64 {
1184                    u64::MAX
1185                } else {
1186                    *val as u64
1187                }
1188            }
1189            Value::Data(obj) => obj.saturating_to_u64(),
1190            other => panic!("SaturatingToU64 not supported for {}", other.datatype()),
1191        }
1192    }
1193
1194    fn saturating_to_u128(&self) -> u128 {
1195        match self {
1196            Value::I8(val) => match *val {
1197                val if val < u128::MIN as i8 => u128::MIN,
1198                val => val as u128,
1199            },
1200            Value::I16(val) => match *val {
1201                val if val < u128::MIN as i16 => u128::MIN,
1202                val => val as u128,
1203            },
1204            Value::I32(val) => match *val {
1205                val if val < u128::MIN as i32 => u128::MIN,
1206                val => val as u128,
1207            },
1208            Value::I64(val) => match *val {
1209                val if val < u128::MIN as i64 => u128::MIN,
1210                val => val as u128,
1211            },
1212            Value::I128(val) => match *val {
1213                val if val < u128::MIN as i128 => u128::MIN,
1214                val => val as u128,
1215            },
1216            Value::U8(val) => *val as u128,
1217            Value::U16(val) => *val as u128,
1218            Value::U32(val) => *val as u128,
1219            Value::U64(val) => *val as u128,
1220            Value::U128(val) => *val,
1221            Value::F32(val) => {
1222                if val.is_nan() {
1223                    0
1224                } else if *val < u128::MIN as f32 {
1225                    u128::MIN
1226                } else if *val > u128::MAX as f32 {
1227                    u128::MAX
1228                } else {
1229                    *val as u128
1230                }
1231            }
1232            Value::F64(val) => {
1233                if val.is_nan() {
1234                    0
1235                } else if *val < u128::MIN as f64 {
1236                    u128::MIN
1237                } else if *val > u128::MAX as f64 {
1238                    u128::MAX
1239                } else {
1240                    *val as u128
1241                }
1242            }
1243            Value::Data(obj) => obj.saturating_to_u128(),
1244            other => panic!("SaturatingToU128 not supported for {}", other.datatype()),
1245        }
1246    }
1247
1248    fn saturating_to_f32(&self) -> f32 {
1249        match self {
1250            Value::I8(val) => *val as f32,
1251            Value::I16(val) => *val as f32,
1252            Value::I32(val) => *val as f32,
1253            Value::I64(val) => *val as f32,
1254            Value::I128(val) => *val as f32,
1255            Value::U8(val) => *val as f32,
1256            Value::U16(val) => *val as f32,
1257            Value::U32(val) => *val as f32,
1258            Value::U64(val) => *val as f32,
1259            Value::U128(val) => *val as f32,
1260            Value::F32(val) => *val,
1261            Value::F64(val) => *val as f32,
1262            Value::Data(obj) => obj.saturating_to_f32(),
1263            other => panic!("SaturatingToF32 not supported for {}", other.datatype()),
1264        }
1265    }
1266
1267    fn saturating_to_f64(&self) -> f64 {
1268        match self {
1269            Value::I8(val) => *val as f64,
1270            Value::I16(val) => *val as f64,
1271            Value::I32(val) => *val as f64,
1272            Value::I64(val) => *val as f64,
1273            Value::I128(val) => *val as f64,
1274            Value::U8(val) => *val as f64,
1275            Value::U16(val) => *val as f64,
1276            Value::U32(val) => *val as f64,
1277            Value::U64(val) => *val as f64,
1278            Value::U128(val) => *val as f64,
1279            Value::F32(val) => *val as f64,
1280            Value::F64(val) => *val,
1281            Value::Data(obj) => obj.saturating_to_f64(),
1282            other => panic!("SaturatingToF64 not supported for {}", other.datatype()),
1283        }
1284    }
1285
1286    fn binary_and(&self, other: &Value) -> Value {
1287        match (self, other) {
1288            (Value::Bool(val), Value::Bool(other)) => Value::Bool(val & other),
1289            (Value::Byte(val), Value::Byte(other)) => Value::Byte(val & other),
1290            (Value::Data(obj), Value::Data(_)) => obj.binary_and(other),
1291            (a, b) if a.datatype() != b.datatype() => {
1292                panic!("Unsupported operation, values involved must have same type")
1293            }
1294            (other, _) => panic!("Binary not supported for {}", other.datatype()),
1295        }
1296    }
1297
1298    fn binary_or(&self, other: &Value) -> Value {
1299        match (self, other) {
1300            (Value::Bool(val), Value::Bool(other)) => Value::Bool(val | other),
1301            (Value::Byte(val), Value::Byte(other)) => Value::Byte(val | other),
1302            (Value::Data(obj), Value::Data(_)) => obj.binary_or(other),
1303            (a, b) if a.datatype() != b.datatype() => {
1304                panic!("Unsupported operation, values involved must have same type")
1305            }
1306            (other, _) => panic!("Binary not supported for {}", other.datatype()),
1307        }
1308    }
1309
1310    fn binary_xor(&self, other: &Value) -> Value {
1311        match (self, other) {
1312            (Value::Bool(val), Value::Bool(other)) => Value::Bool(val ^ other),
1313            (Value::Byte(val), Value::Byte(other)) => Value::Byte(val ^ other),
1314            (Value::Data(obj), Value::Data(_)) => obj.binary_xor(other),
1315            (a, b) if a.datatype() != b.datatype() => {
1316                panic!("Unsupported operation, values involved must have same type")
1317            }
1318            (other, _) => panic!("Binary not supported for {}", other.datatype()),
1319        }
1320    }
1321
1322    fn binary_not(&self) -> Value {
1323        match self {
1324            Value::Bool(val) => Value::Bool(!val),
1325            Value::Byte(val) => Value::Byte(!val),
1326            Value::Data(obj) => obj.binary_not(),
1327            other => panic!("Binary not supported for {}", other.datatype()),
1328        }
1329    }
1330
1331    fn signed_abs(&self) -> Option<Value> {
1332        match self {
1333            Value::I8(val) => {
1334                if *val == i8::MIN {
1335                    None
1336                } else {
1337                    Some(Value::I8(val.abs()))
1338                }
1339            }
1340            Value::I16(val) => {
1341                if *val == i16::MIN {
1342                    None
1343                } else {
1344                    Some(Value::I16(val.abs()))
1345                }
1346            }
1347            Value::I32(val) => {
1348                if *val == i32::MIN {
1349                    None
1350                } else {
1351                    Some(Value::I32(val.abs()))
1352                }
1353            }
1354            Value::I64(val) => {
1355                if *val == i64::MIN {
1356                    None
1357                } else {
1358                    Some(Value::I64(val.abs()))
1359                }
1360            }
1361            Value::I128(val) => {
1362                if *val == i128::MIN {
1363                    None
1364                } else {
1365                    Some(Value::I128(val.abs()))
1366                }
1367            }
1368            Value::F32(val) => Some(Value::F32(val.abs())),
1369            Value::F64(val) => Some(Value::F64(val.abs())),
1370            Value::Data(obj) => obj.signed_abs(),
1371            other => panic!("Signed not supported for {}", other.datatype()),
1372        }
1373    }
1374
1375    fn signed_signum(&self) -> Value {
1376        match self {
1377            Value::I8(val) => Value::I8(val.signum()),
1378            Value::I16(val) => Value::I16(val.signum()),
1379            Value::I32(val) => Value::I32(val.signum()),
1380            Value::I64(val) => Value::I64(val.signum()),
1381            Value::I128(val) => Value::I128(val.signum()),
1382            Value::F32(val) => Value::F32(val.signum()),
1383            Value::F64(val) => Value::F64(val.signum()),
1384            Value::Data(obj) => obj.signed_signum(),
1385            other => panic!("Signed not supported for {}", other.datatype()),
1386        }
1387    }
1388
1389    fn signed_is_positive(&self) -> bool {
1390        match self {
1391            Value::I8(val) => val.is_positive(),
1392            Value::I16(val) => val.is_positive(),
1393            Value::I32(val) => val.is_positive(),
1394            Value::I64(val) => val.is_positive(),
1395            Value::I128(val) => val.is_positive(),
1396            Value::F32(val) => val.is_sign_positive(),
1397            Value::F64(val) => val.is_sign_positive(),
1398            Value::Data(obj) => obj.signed_is_positive(),
1399            other => panic!("Signed not supported for {}", other.datatype()),
1400        }
1401    }
1402
1403    fn signed_is_negative(&self) -> bool {
1404        match self {
1405            Value::I8(val) => val.is_negative(),
1406            Value::I16(val) => val.is_negative(),
1407            Value::I32(val) => val.is_negative(),
1408            Value::I64(val) => val.is_negative(),
1409            Value::I128(val) => val.is_negative(),
1410            Value::F32(val) => val.is_sign_negative(),
1411            Value::F64(val) => val.is_sign_negative(),
1412            Value::Data(obj) => obj.signed_is_negative(),
1413            other => panic!("Signed not supported for {}", other.datatype()),
1414        }
1415    }
1416
1417    fn float_is_nan(&self) -> bool {
1418        match self {
1419            Value::F32(val) => val.is_nan(),
1420            Value::F64(val) => val.is_nan(),
1421            Value::Data(obj) => obj.float_is_nan(),
1422            other => panic!("Float not supported for {}", other.datatype()),
1423        }
1424    }
1425
1426    fn float_is_infinite(&self) -> bool {
1427        match self {
1428            Value::F32(val) => val.is_infinite(),
1429            Value::F64(val) => val.is_infinite(),
1430            Value::Data(obj) => obj.float_is_infinite(),
1431            other => panic!("Float not supported for {}", other.datatype()),
1432        }
1433    }
1434
1435    fn float_is_finite(&self) -> bool {
1436        match self {
1437            Value::F32(val) => val.is_finite(),
1438            Value::F64(val) => val.is_finite(),
1439            Value::Data(obj) => obj.float_is_finite(),
1440            other => panic!("Float not supported for {}", other.datatype()),
1441        }
1442    }
1443
1444    fn float_is_normal(&self) -> bool {
1445        match self {
1446            Value::F32(val) => val.is_normal(),
1447            Value::F64(val) => val.is_normal(),
1448            Value::Data(obj) => obj.float_is_normal(),
1449            other => panic!("Float not supported for {}", other.datatype()),
1450        }
1451    }
1452
1453    fn float_is_subnormal(&self) -> bool {
1454        match self {
1455            Value::F32(val) => val.is_subnormal(),
1456            Value::F64(val) => val.is_subnormal(),
1457            Value::Data(obj) => obj.float_is_subnormal(),
1458            other => panic!("Float not supported for {}", other.datatype()),
1459        }
1460    }
1461
1462    fn float_floor(&self) -> Value {
1463        match self {
1464            Value::F32(val) => Value::F32(val.floor()),
1465            Value::F64(val) => Value::F64(val.floor()),
1466            Value::Data(obj) => obj.float_floor(),
1467            other => panic!("Float not supported for {}", other.datatype()),
1468        }
1469    }
1470
1471    fn float_ceil(&self) -> Value {
1472        match self {
1473            Value::F32(val) => Value::F32(val.ceil()),
1474            Value::F64(val) => Value::F64(val.ceil()),
1475            Value::Data(obj) => obj.float_ceil(),
1476            other => panic!("Float not supported for {}", other.datatype()),
1477        }
1478    }
1479
1480    fn float_round(&self) -> Value {
1481        match self {
1482            Value::F32(val) => Value::F32(val.round()),
1483            Value::F64(val) => Value::F64(val.round()),
1484            Value::Data(obj) => obj.float_round(),
1485            other => panic!("Float not supported for {}", other.datatype()),
1486        }
1487    }
1488
1489    fn float_trunc(&self) -> Value {
1490        match self {
1491            Value::F32(val) => Value::F32(val.trunc()),
1492            Value::F64(val) => Value::F64(val.trunc()),
1493            Value::Data(obj) => obj.float_trunc(),
1494            other => panic!("Float not supported for {}", other.datatype()),
1495        }
1496    }
1497
1498    fn float_fract(&self) -> Value {
1499        match self {
1500            Value::F32(val) => Value::F32(val.fract()),
1501            Value::F64(val) => Value::F64(val.fract()),
1502            Value::Data(obj) => obj.float_fract(),
1503            other => panic!("Float not supported for {}", other.datatype()),
1504        }
1505    }
1506
1507    fn float_recip(&self) -> Value {
1508        match self {
1509            Value::F32(val) => Value::F32(val.recip()),
1510            Value::F64(val) => Value::F64(val.recip()),
1511            Value::Data(obj) => obj.float_recip(),
1512            other => panic!("Float not supported for {}", other.datatype()),
1513        }
1514    }
1515
1516    fn float_pow(&self, n: &Value) -> Value {
1517        match (self, n) {
1518            (Value::F32(val), Value::F32(n)) => Value::F32(val.powf(*n)),
1519            (Value::F64(val), Value::F64(n)) => Value::F64(val.powf(*n)),
1520            (Value::Data(obj), Value::Data(_)) => obj.float_pow(n),
1521            (a, b) if a.datatype() != b.datatype() => {
1522                panic!("Unsupported operation, values involved must have same type")
1523            }
1524            (other, _) => panic!("Float not supported for {}", other.datatype()),
1525        }
1526    }
1527
1528    fn float_sqrt(&self) -> Value {
1529        match self {
1530            Value::F32(val) => Value::F32(val.sqrt()),
1531            Value::F64(val) => Value::F64(val.sqrt()),
1532            Value::Data(obj) => obj.float_sqrt(),
1533            other => panic!("Float not supported for {}", other.datatype()),
1534        }
1535    }
1536
1537    fn float_exp(&self) -> Value {
1538        match self {
1539            Value::F32(val) => Value::F32(val.exp()),
1540            Value::F64(val) => Value::F64(val.exp()),
1541            Value::Data(obj) => obj.float_exp(),
1542            other => panic!("Float not supported for {}", other.datatype()),
1543        }
1544    }
1545
1546    fn float_exp2(&self) -> Value {
1547        match self {
1548            Value::F32(val) => Value::F32(val.exp2()),
1549            Value::F64(val) => Value::F64(val.exp2()),
1550            Value::Data(obj) => obj.float_exp2(),
1551            other => panic!("Float not supported for {}", other.datatype()),
1552        }
1553    }
1554
1555    fn float_ln(&self) -> Value {
1556        match self {
1557            Value::F32(val) => Value::F32(val.ln()),
1558            Value::F64(val) => Value::F64(val.ln()),
1559            Value::Data(obj) => obj.float_ln(),
1560            other => panic!("Float not supported for {}", other.datatype()),
1561        }
1562    }
1563
1564    fn float_log(&self, base: &Value) -> Value {
1565        match (self, base) {
1566            (Value::F32(val), Value::F32(n)) => Value::F32(val.log(*n)),
1567            (Value::F64(val), Value::F64(n)) => Value::F64(val.log(*n)),
1568            (Value::Data(obj), Value::Data(_)) => obj.float_log(base),
1569            (a, b) if a.datatype() != b.datatype() => {
1570                panic!("Unsupported operation, values involved must have same type")
1571            }
1572            (other, _) => panic!("Float not supported for {}", other.datatype()),
1573        }
1574    }
1575
1576    fn float_log2(&self) -> Value {
1577        match self {
1578            Value::F32(val) => Value::F32(val.log2()),
1579            Value::F64(val) => Value::F64(val.log2()),
1580            Value::Data(obj) => obj.float_log2(),
1581            other => panic!("Float not supported for {}", other.datatype()),
1582        }
1583    }
1584
1585    fn float_log10(&self) -> Value {
1586        match self {
1587            Value::F32(val) => Value::F32(val.log10()),
1588            Value::F64(val) => Value::F64(val.log10()),
1589            Value::Data(obj) => obj.float_log10(),
1590            other => panic!("Float not supported for {}", other.datatype()),
1591        }
1592    }
1593
1594    fn float_cbrt(&self) -> Value {
1595        match self {
1596            Value::F32(val) => Value::F32(val.cbrt()),
1597            Value::F64(val) => Value::F64(val.cbrt()),
1598            Value::Data(obj) => obj.float_cbrt(),
1599            other => panic!("Float not supported for {}", other.datatype()),
1600        }
1601    }
1602
1603    fn float_hypot(&self, n: &Value) -> Value {
1604        match (self, n) {
1605            (Value::F32(val), Value::F32(n)) => Value::F32(val.hypot(*n)),
1606            (Value::F64(val), Value::F64(n)) => Value::F64(val.hypot(*n)),
1607            (Value::Data(obj), Value::Data(_)) => obj.float_hypot(n),
1608            (a, b) if a.datatype() != b.datatype() => {
1609                panic!("Unsupported operation, values involved must have same type")
1610            }
1611            (other, _) => panic!("Float not supported for {}", other.datatype()),
1612        }
1613    }
1614
1615    fn float_sin(&self) -> Value {
1616        match self {
1617            Value::F32(val) => Value::F32(val.sin()),
1618            Value::F64(val) => Value::F64(val.sin()),
1619            Value::Data(obj) => obj.float_sin(),
1620            other => panic!("Float not supported for {}", other.datatype()),
1621        }
1622    }
1623
1624    fn float_cos(&self) -> Value {
1625        match self {
1626            Value::F32(val) => Value::F32(val.cos()),
1627            Value::F64(val) => Value::F64(val.cos()),
1628            Value::Data(obj) => obj.float_cos(),
1629            other => panic!("Float not supported for {}", other.datatype()),
1630        }
1631    }
1632
1633    fn float_tan(&self) -> Value {
1634        match self {
1635            Value::F32(val) => Value::F32(val.tan()),
1636            Value::F64(val) => Value::F64(val.tan()),
1637            Value::Data(obj) => obj.float_tan(),
1638            other => panic!("Float not supported for {}", other.datatype()),
1639        }
1640    }
1641
1642    fn float_asin(&self) -> Value {
1643        match self {
1644            Value::F32(val) => Value::F32(val.asin()),
1645            Value::F64(val) => Value::F64(val.asin()),
1646            Value::Data(obj) => obj.float_asin(),
1647            other => panic!("Float not supported for {}", other.datatype()),
1648        }
1649    }
1650
1651    fn float_acos(&self) -> Value {
1652        match self {
1653            Value::F32(val) => Value::F32(val.acos()),
1654            Value::F64(val) => Value::F64(val.acos()),
1655            Value::Data(obj) => obj.float_acos(),
1656            other => panic!("Float not supported for {}", other.datatype()),
1657        }
1658    }
1659
1660    fn float_atan(&self) -> Value {
1661        match self {
1662            Value::F32(val) => Value::F32(val.atan()),
1663            Value::F64(val) => Value::F64(val.atan()),
1664            Value::Data(obj) => obj.float_atan(),
1665            other => panic!("Float not supported for {}", other.datatype()),
1666        }
1667    }
1668
1669    fn float_atan2(&self, n: &Value) -> Value {
1670        match (self, n) {
1671            (Value::F32(val), Value::F32(n)) => Value::F32(val.atan2(*n)),
1672            (Value::F64(val), Value::F64(n)) => Value::F64(val.atan2(*n)),
1673            (Value::Data(obj), Value::Data(_)) => obj.binary_and(n),
1674            (a, b) if a.datatype() != b.datatype() => {
1675                panic!("Unsupported operation, values involved must have same type")
1676            }
1677            (other, _) => panic!("Float not supported for {}", other.datatype()),
1678        }
1679    }
1680
1681    fn float_sinh(&self) -> Value {
1682        match self {
1683            Value::F32(val) => Value::F32(val.sinh()),
1684            Value::F64(val) => Value::F64(val.sinh()),
1685            Value::Data(obj) => obj.float_sinh(),
1686            other => panic!("Float not supported for {}", other.datatype()),
1687        }
1688    }
1689
1690    fn float_cosh(&self) -> Value {
1691        match self {
1692            Value::F32(val) => Value::F32(val.cosh()),
1693            Value::F64(val) => Value::F64(val.cosh()),
1694            Value::Data(obj) => obj.float_cosh(),
1695            other => panic!("Float not supported for {}", other.datatype()),
1696        }
1697    }
1698
1699    fn float_tanh(&self) -> Value {
1700        match self {
1701            Value::F32(val) => Value::F32(val.tanh()),
1702            Value::F64(val) => Value::F64(val.tanh()),
1703            Value::Data(obj) => obj.float_tanh(),
1704            other => panic!("Float not supported for {}", other.datatype()),
1705        }
1706    }
1707
1708    fn float_asinh(&self) -> Value {
1709        match self {
1710            Value::F32(val) => Value::F32(val.asinh()),
1711            Value::F64(val) => Value::F64(val.asinh()),
1712            Value::Data(obj) => obj.float_asinh(),
1713            other => panic!("Float not supported for {}", other.datatype()),
1714        }
1715    }
1716
1717    fn float_acosh(&self) -> Value {
1718        match self {
1719            Value::F32(val) => Value::F32(val.acosh()),
1720            Value::F64(val) => Value::F64(val.acosh()),
1721            Value::Data(obj) => obj.float_acosh(),
1722            other => panic!("Float not supported for {}", other.datatype()),
1723        }
1724    }
1725
1726    fn float_atanh(&self) -> Value {
1727        match self {
1728            Value::F32(val) => Value::F32(val.atanh()),
1729            Value::F64(val) => Value::F64(val.atanh()),
1730            Value::Data(obj) => obj.float_atanh(),
1731            other => panic!("Float not supported for {}", other.datatype()),
1732        }
1733    }
1734
1735    fn float_to_degrees(&self) -> Value {
1736        match self {
1737            Value::F32(val) => Value::F32(val.to_degrees()),
1738            Value::F64(val) => Value::F64(val.to_degrees()),
1739            Value::Data(obj) => obj.float_to_degrees(),
1740            other => panic!("Float not supported for {}", other.datatype()),
1741        }
1742    }
1743
1744    fn float_to_radians(&self) -> Value {
1745        match self {
1746            Value::F32(val) => Value::F32(val.to_radians()),
1747            Value::F64(val) => Value::F64(val.to_radians()),
1748            Value::Data(obj) => obj.float_to_radians(),
1749            other => panic!("Float not supported for {}", other.datatype()),
1750        }
1751    }
1752
1753    fn partial_equality_eq(&self, other: &Value) -> bool {
1754        match (self, other) {
1755            (Value::I8(me), Value::I8(other)) => me == other,
1756            (Value::I16(me), Value::I16(other)) => me == other,
1757            (Value::I32(me), Value::I32(other)) => me == other,
1758            (Value::I64(me), Value::I64(other)) => me == other,
1759            (Value::I128(me), Value::I128(other)) => me == other,
1760            (Value::U8(me), Value::U8(other)) => me == other,
1761            (Value::U16(me), Value::U16(other)) => me == other,
1762            (Value::U32(me), Value::U32(other)) => me == other,
1763            (Value::U64(me), Value::U64(other)) => me == other,
1764            (Value::U128(me), Value::U128(other)) => me == other,
1765            (Value::F32(me), Value::F32(other)) => me == other,
1766            (Value::F64(me), Value::F64(other)) => me == other,
1767            (Value::Bool(me), Value::Bool(other)) => me == other,
1768            (Value::Byte(me), Value::Byte(other)) => me == other,
1769            (Value::Char(me), Value::Char(other)) => me == other,
1770            (Value::String(me), Value::String(other)) => me == other,
1771            (Value::Data(obj), Value::Data(_)) => obj.partial_equality_eq(other),
1772            (a, b) if a.datatype() != b.datatype() => {
1773                panic!("Unsupported operation, values involved must have same type")
1774            }
1775            (other, _) => panic!("PartialEq not supported for {}", other.datatype()),
1776        }
1777    }
1778
1779    fn partial_equality_ne(&self, other: &Value) -> bool {
1780        match (self, other) {
1781            (Value::I8(me), Value::I8(other)) => me != other,
1782            (Value::I16(me), Value::I16(other)) => me != other,
1783            (Value::I32(me), Value::I32(other)) => me != other,
1784            (Value::I64(me), Value::I64(other)) => me != other,
1785            (Value::I128(me), Value::I128(other)) => me != other,
1786            (Value::U8(me), Value::U8(other)) => me != other,
1787            (Value::U16(me), Value::U16(other)) => me != other,
1788            (Value::U32(me), Value::U32(other)) => me != other,
1789            (Value::U64(me), Value::U64(other)) => me != other,
1790            (Value::U128(me), Value::U128(other)) => me != other,
1791            (Value::F32(me), Value::F32(other)) => me != other,
1792            (Value::F64(me), Value::F64(other)) => me != other,
1793            (Value::Bool(me), Value::Bool(other)) => me != other,
1794            (Value::Byte(me), Value::Byte(other)) => me != other,
1795            (Value::Char(me), Value::Char(other)) => me != other,
1796            (Value::String(me), Value::String(other)) => me != other,
1797            (Value::Data(obj), Value::Data(_)) => obj.partial_equality_ne(other),
1798            (a, b) if a.datatype() != b.datatype() => {
1799                panic!("Unsupported operation, values involved must have same type")
1800            }
1801            (other, _) => panic!("PartialEq not supported for {}", other.datatype()),
1802        }
1803    }
1804
1805    fn partial_order_lt(&self, other: &Value) -> bool {
1806        match (self, other) {
1807            (Value::I8(me), Value::I8(other)) => me < other,
1808            (Value::I16(me), Value::I16(other)) => me < other,
1809            (Value::I32(me), Value::I32(other)) => me < other,
1810            (Value::I64(me), Value::I64(other)) => me < other,
1811            (Value::I128(me), Value::I128(other)) => me < other,
1812            (Value::U8(me), Value::U8(other)) => me < other,
1813            (Value::U16(me), Value::U16(other)) => me < other,
1814            (Value::U32(me), Value::U32(other)) => me < other,
1815            (Value::U64(me), Value::U64(other)) => me < other,
1816            (Value::U128(me), Value::U128(other)) => me < other,
1817            (Value::F32(me), Value::F32(other)) => me < other,
1818            (Value::F64(me), Value::F64(other)) => me < other,
1819            (Value::Bool(me), Value::Bool(other)) => me < other,
1820            (Value::Byte(me), Value::Byte(other)) => me < other,
1821            (Value::Char(me), Value::Char(other)) => me < other,
1822            (Value::String(me), Value::String(other)) => me < other,
1823            (Value::Data(obj), Value::Data(_)) => obj.partial_order_lt(other),
1824            (a, b) if a.datatype() != b.datatype() => {
1825                panic!("Unsupported operation, values involved must have same type")
1826            }
1827            (other, _) => panic!("PartialOrd not supported for {}", other.datatype()),
1828        }
1829    }
1830
1831    fn partial_order_le(&self, other: &Value) -> bool {
1832        match (self, other) {
1833            (Value::I8(me), Value::I8(other)) => me <= other,
1834            (Value::I16(me), Value::I16(other)) => me <= other,
1835            (Value::I32(me), Value::I32(other)) => me <= other,
1836            (Value::I64(me), Value::I64(other)) => me <= other,
1837            (Value::I128(me), Value::I128(other)) => me <= other,
1838            (Value::U8(me), Value::U8(other)) => me <= other,
1839            (Value::U16(me), Value::U16(other)) => me <= other,
1840            (Value::U32(me), Value::U32(other)) => me <= other,
1841            (Value::U64(me), Value::U64(other)) => me <= other,
1842            (Value::U128(me), Value::U128(other)) => me <= other,
1843            (Value::F32(me), Value::F32(other)) => me <= other,
1844            (Value::F64(me), Value::F64(other)) => me <= other,
1845            (Value::Bool(me), Value::Bool(other)) => me <= other,
1846            (Value::Byte(me), Value::Byte(other)) => me <= other,
1847            (Value::Char(me), Value::Char(other)) => me <= other,
1848            (Value::String(me), Value::String(other)) => me <= other,
1849            (Value::Data(obj), Value::Data(_)) => obj.partial_order_le(other),
1850            (a, b) if a.datatype() != b.datatype() => {
1851                panic!("Unsupported operation, values involved must have same type")
1852            }
1853            (other, _) => panic!("PartialOrd not supported for {}", other.datatype()),
1854        }
1855    }
1856
1857    fn partial_order_gt(&self, other: &Value) -> bool {
1858        match (self, other) {
1859            (Value::I8(me), Value::I8(other)) => me > other,
1860            (Value::I16(me), Value::I16(other)) => me > other,
1861            (Value::I32(me), Value::I32(other)) => me > other,
1862            (Value::I64(me), Value::I64(other)) => me > other,
1863            (Value::I128(me), Value::I128(other)) => me > other,
1864            (Value::U8(me), Value::U8(other)) => me > other,
1865            (Value::U16(me), Value::U16(other)) => me > other,
1866            (Value::U32(me), Value::U32(other)) => me > other,
1867            (Value::U64(me), Value::U64(other)) => me > other,
1868            (Value::U128(me), Value::U128(other)) => me > other,
1869            (Value::F32(me), Value::F32(other)) => me > other,
1870            (Value::F64(me), Value::F64(other)) => me > other,
1871            (Value::Bool(me), Value::Bool(other)) => me > other,
1872            (Value::Byte(me), Value::Byte(other)) => me > other,
1873            (Value::Char(me), Value::Char(other)) => me > other,
1874            (Value::String(me), Value::String(other)) => me > other,
1875            (Value::Data(obj), Value::Data(_)) => obj.partial_order_gt(other),
1876            (a, b) if a.datatype() != b.datatype() => {
1877                panic!("Unsupported operation, values involved must have same type")
1878            }
1879            (other, _) => panic!("PartialOrd not supported for {}", other.datatype()),
1880        }
1881    }
1882
1883    fn partial_order_ge(&self, other: &Value) -> bool {
1884        match (self, other) {
1885            (Value::I8(me), Value::I8(other)) => me >= other,
1886            (Value::I16(me), Value::I16(other)) => me >= other,
1887            (Value::I32(me), Value::I32(other)) => me >= other,
1888            (Value::I64(me), Value::I64(other)) => me >= other,
1889            (Value::I128(me), Value::I128(other)) => me >= other,
1890            (Value::U8(me), Value::U8(other)) => me >= other,
1891            (Value::U16(me), Value::U16(other)) => me >= other,
1892            (Value::U32(me), Value::U32(other)) => me >= other,
1893            (Value::U64(me), Value::U64(other)) => me >= other,
1894            (Value::U128(me), Value::U128(other)) => me >= other,
1895            (Value::F32(me), Value::F32(other)) => me >= other,
1896            (Value::F64(me), Value::F64(other)) => me >= other,
1897            (Value::Bool(me), Value::Bool(other)) => me >= other,
1898            (Value::Byte(me), Value::Byte(other)) => me >= other,
1899            (Value::Char(me), Value::Char(other)) => me >= other,
1900            (Value::String(me), Value::String(other)) => me >= other,
1901            (Value::Data(obj), Value::Data(_)) => obj.partial_order_ge(other),
1902            (a, b) if a.datatype() != b.datatype() => {
1903                panic!("Unsupported operation, values involved must have same type")
1904            }
1905            (other, _) => panic!("PartialOrd not supported for {}", other.datatype()),
1906        }
1907    }
1908
1909    fn order_max(&self, other: &Value) -> Value {
1910        match (self, other) {
1911            (Value::I8(me), Value::I8(other)) => Value::I8((*me).max(*other)),
1912            (Value::I16(me), Value::I16(other)) => Value::I16((*me).max(*other)),
1913            (Value::I32(me), Value::I32(other)) => Value::I32((*me).max(*other)),
1914            (Value::I64(me), Value::I64(other)) => Value::I64((*me).max(*other)),
1915            (Value::I128(me), Value::I128(other)) => Value::I128((*me).max(*other)),
1916            (Value::U8(me), Value::U8(other)) => Value::U8((*me).max(*other)),
1917            (Value::U16(me), Value::U16(other)) => Value::U16((*me).max(*other)),
1918            (Value::U32(me), Value::U32(other)) => Value::U32((*me).max(*other)),
1919            (Value::U64(me), Value::U64(other)) => Value::U64((*me).max(*other)),
1920            (Value::U128(me), Value::U128(other)) => Value::U128((*me).max(*other)),
1921            (Value::F32(me), Value::F32(other)) => Value::F32((*me).max(*other)),
1922            (Value::F64(me), Value::F64(other)) => Value::F64((*me).max(*other)),
1923            (Value::Bool(me), Value::Bool(other)) => Value::Bool((*me).max(*other)),
1924            (Value::Byte(me), Value::Byte(other)) => Value::Byte((*me).max(*other)),
1925            (Value::Char(me), Value::Char(other)) => Value::Char((*me).max(*other)),
1926            (Value::String(me), Value::String(other)) => Value::String(me.max(other).clone()),
1927            (Value::Data(obj), Value::Data(_)) => obj.order_max(other),
1928            (a, b) if a.datatype() != b.datatype() => {
1929                panic!("Unsupported operation, values involved must have same type")
1930            }
1931            (other, _) => panic!("Ord not supported for {}", other.datatype()),
1932        }
1933    }
1934
1935    fn order_min(&self, other: &Value) -> Value {
1936        match (self, other) {
1937            (Value::I8(me), Value::I8(other)) => Value::I8((*me).min(*other)),
1938            (Value::I16(me), Value::I16(other)) => Value::I16((*me).min(*other)),
1939            (Value::I32(me), Value::I32(other)) => Value::I32((*me).min(*other)),
1940            (Value::I64(me), Value::I64(other)) => Value::I64((*me).min(*other)),
1941            (Value::I128(me), Value::I128(other)) => Value::I128((*me).min(*other)),
1942            (Value::U8(me), Value::U8(other)) => Value::U8((*me).min(*other)),
1943            (Value::U16(me), Value::U16(other)) => Value::U16((*me).min(*other)),
1944            (Value::U32(me), Value::U32(other)) => Value::U32((*me).min(*other)),
1945            (Value::U64(me), Value::U64(other)) => Value::U64((*me).min(*other)),
1946            (Value::U128(me), Value::U128(other)) => Value::U128((*me).min(*other)),
1947            (Value::F32(me), Value::F32(other)) => Value::F32((*me).min(*other)),
1948            (Value::F64(me), Value::F64(other)) => Value::F64((*me).min(*other)),
1949            (Value::Bool(me), Value::Bool(other)) => Value::Bool((*me).min(*other)),
1950            (Value::Byte(me), Value::Byte(other)) => Value::Byte((*me).min(*other)),
1951            (Value::Char(me), Value::Char(other)) => Value::Char((*me).min(*other)),
1952            (Value::String(me), Value::String(other)) => Value::String(me.min(other).clone()),
1953            (Value::Data(obj), Value::Data(_)) => obj.order_min(other),
1954            (a, b) if a.datatype() != b.datatype() => {
1955                panic!("Unsupported operation, values involved must have same type")
1956            }
1957            (other, _) => panic!("Ord not supported for {}", other.datatype()),
1958        }
1959    }
1960
1961    fn order_clamp(&self, min: &Value, max: &Value) -> Value {
1962        match (self, min, max) {
1963            (Value::I8(me), Value::I8(min), Value::I8(max)) => Value::I8((*me).clamp(*min, *max)),
1964            (Value::I16(me), Value::I16(min), Value::I16(max)) => {
1965                Value::I16((*me).clamp(*min, *max))
1966            }
1967            (Value::I32(me), Value::I32(min), Value::I32(max)) => {
1968                Value::I32((*me).clamp(*min, *max))
1969            }
1970            (Value::I64(me), Value::I64(min), Value::I64(max)) => {
1971                Value::I64((*me).clamp(*min, *max))
1972            }
1973            (Value::I128(me), Value::I128(min), Value::I128(max)) => {
1974                Value::I128((*me).clamp(*min, *max))
1975            }
1976            (Value::U8(me), Value::U8(min), Value::U8(max)) => Value::U8((*me).clamp(*min, *max)),
1977            (Value::U16(me), Value::U16(min), Value::U16(max)) => {
1978                Value::U16((*me).clamp(*min, *max))
1979            }
1980            (Value::U32(me), Value::U32(min), Value::U32(max)) => {
1981                Value::U32((*me).clamp(*min, *max))
1982            }
1983            (Value::U64(me), Value::U64(min), Value::U64(max)) => {
1984                Value::U64((*me).clamp(*min, *max))
1985            }
1986            (Value::U128(me), Value::U128(min), Value::U128(max)) => {
1987                Value::U128((*me).clamp(*min, *max))
1988            }
1989            (Value::F32(me), Value::F32(min), Value::F32(max)) => {
1990                Value::F32((*me).clamp(*min, *max))
1991            }
1992            (Value::F64(me), Value::F64(min), Value::F64(max)) => {
1993                Value::F64((*me).clamp(*min, *max))
1994            }
1995            (Value::Bool(me), Value::Bool(min), Value::Bool(max)) => {
1996                Value::Bool((*me).clamp(*min, *max))
1997            }
1998            (Value::Byte(me), Value::Byte(min), Value::Byte(max)) => {
1999                Value::Byte((*me).clamp(*min, *max))
2000            }
2001            (Value::Char(me), Value::Char(min), Value::Char(max)) => {
2002                Value::Char((*me).clamp(*min, *max))
2003            }
2004            (Value::String(me), Value::String(min), Value::String(max)) => {
2005                Value::String((me).clamp(min, max).clone())
2006            }
2007            (Value::Data(obj), Value::Data(_), Value::Data(_)) => obj.order_clamp(min, max),
2008            (a, b, c) if a.datatype() != b.datatype() || a.datatype() != c.datatype() => {
2009                panic!("Unsupported operation, values involved must have same type")
2010            }
2011            (other, _, _) => panic!("Ord not supported for {}", other.datatype()),
2012        }
2013    }
2014
2015    fn add(&self, other: &Value) -> Value {
2016        match (self, other) {
2017            (Value::I8(me), Value::I8(other)) => Value::I8(*me + *other),
2018            (Value::I16(me), Value::I16(other)) => Value::I16(*me + *other),
2019            (Value::I32(me), Value::I32(other)) => Value::I32(*me + *other),
2020            (Value::I64(me), Value::I64(other)) => Value::I64(*me + *other),
2021            (Value::I128(me), Value::I128(other)) => Value::I128(*me + *other),
2022            (Value::U8(me), Value::U8(other)) => Value::U8(*me + *other),
2023            (Value::U16(me), Value::U16(other)) => Value::U16(*me + *other),
2024            (Value::U32(me), Value::U32(other)) => Value::U32(*me + *other),
2025            (Value::U64(me), Value::U64(other)) => Value::U64(*me + *other),
2026            (Value::U128(me), Value::U128(other)) => Value::U128(*me + *other),
2027            (Value::F32(me), Value::F32(other)) => Value::F32(*me + *other),
2028            (Value::F64(me), Value::F64(other)) => Value::F64(*me + *other),
2029            (Value::Data(obj), Value::Data(_)) => obj.add(other),
2030            (a, b) if a.datatype() != b.datatype() => {
2031                panic!("Unsupported operation, values involved must have same type")
2032            }
2033            (other, _) => panic!("Add not supported for {}", other.datatype()),
2034        }
2035    }
2036
2037    fn checked_add(&self, other: &Value) -> Option<Value> {
2038        match (self, other) {
2039            (Value::I8(me), Value::I8(other)) => me.checked_add(*other).map(|val| Value::I8(val)),
2040            (Value::I16(me), Value::I16(other)) => {
2041                me.checked_add(*other).map(|val| Value::I16(val))
2042            }
2043            (Value::I32(me), Value::I32(other)) => {
2044                me.checked_add(*other).map(|val| Value::I32(val))
2045            }
2046            (Value::I64(me), Value::I64(other)) => {
2047                me.checked_add(*other).map(|val| Value::I64(val))
2048            }
2049            (Value::I128(me), Value::I128(other)) => {
2050                me.checked_add(*other).map(|val| Value::I128(val))
2051            }
2052            (Value::U8(me), Value::U8(other)) => me.checked_add(*other).map(|val| Value::U8(val)),
2053            (Value::U16(me), Value::U16(other)) => {
2054                me.checked_add(*other).map(|val| Value::U16(val))
2055            }
2056            (Value::U32(me), Value::U32(other)) => {
2057                me.checked_add(*other).map(|val| Value::U32(val))
2058            }
2059            (Value::U64(me), Value::U64(other)) => {
2060                me.checked_add(*other).map(|val| Value::U64(val))
2061            }
2062            (Value::U128(me), Value::U128(other)) => {
2063                me.checked_add(*other).map(|val| Value::U128(val))
2064            }
2065            (Value::Data(obj), Value::Data(_)) => obj.checked_add(other),
2066            (a, b) if a.datatype() != b.datatype() => {
2067                panic!("Unsupported operation, values involved must have same type")
2068            }
2069            (other, _) => panic!("CheckedAdd not supported for {}", other.datatype()),
2070        }
2071    }
2072
2073    fn saturating_add(&self, other: &Value) -> Value {
2074        match (self, other) {
2075            (Value::I8(me), Value::I8(other)) => Value::I8(me.saturating_add(*other)),
2076            (Value::I16(me), Value::I16(other)) => Value::I16(me.saturating_add(*other)),
2077            (Value::I32(me), Value::I32(other)) => Value::I32(me.saturating_add(*other)),
2078            (Value::I64(me), Value::I64(other)) => Value::I64(me.saturating_add(*other)),
2079            (Value::I128(me), Value::I128(other)) => Value::I128(me.saturating_add(*other)),
2080            (Value::U8(me), Value::U8(other)) => Value::U8(me.saturating_add(*other)),
2081            (Value::U16(me), Value::U16(other)) => Value::U16(me.saturating_add(*other)),
2082            (Value::U32(me), Value::U32(other)) => Value::U32(me.saturating_add(*other)),
2083            (Value::U64(me), Value::U64(other)) => Value::U64(me.saturating_add(*other)),
2084            (Value::U128(me), Value::U128(other)) => Value::U128(me.saturating_add(*other)),
2085            (Value::Data(obj), Value::Data(_)) => obj.saturating_add(other),
2086            (a, b) if a.datatype() != b.datatype() => {
2087                panic!("Unsupported operation, values involved must have same type")
2088            }
2089            (other, _) => panic!("SaturatingAdd not supported for {}", other.datatype()),
2090        }
2091    }
2092
2093    fn wrapping_add(&self, other: &Value) -> Value {
2094        match (self, other) {
2095            (Value::I8(me), Value::I8(other)) => Value::I8(me.wrapping_add(*other)),
2096            (Value::I16(me), Value::I16(other)) => Value::I16(me.wrapping_add(*other)),
2097            (Value::I32(me), Value::I32(other)) => Value::I32(me.wrapping_add(*other)),
2098            (Value::I64(me), Value::I64(other)) => Value::I64(me.wrapping_add(*other)),
2099            (Value::I128(me), Value::I128(other)) => Value::I128(me.wrapping_add(*other)),
2100            (Value::U8(me), Value::U8(other)) => Value::U8(me.wrapping_add(*other)),
2101            (Value::U16(me), Value::U16(other)) => Value::U16(me.wrapping_add(*other)),
2102            (Value::U32(me), Value::U32(other)) => Value::U32(me.wrapping_add(*other)),
2103            (Value::U64(me), Value::U64(other)) => Value::U64(me.wrapping_add(*other)),
2104            (Value::U128(me), Value::U128(other)) => Value::U128(me.wrapping_add(*other)),
2105            (Value::Data(obj), Value::Data(_)) => obj.wrapping_add(other),
2106            (a, b) if a.datatype() != b.datatype() => {
2107                panic!("Unsupported operation, values involved must have same type")
2108            }
2109            (other, _) => panic!("WrappingAdd not supported for {}", other.datatype()),
2110        }
2111    }
2112
2113    fn sub(&self, other: &Value) -> Value {
2114        match (self, other) {
2115            (Value::I8(me), Value::I8(other)) => Value::I8(*me - *other),
2116            (Value::I16(me), Value::I16(other)) => Value::I16(*me - *other),
2117            (Value::I32(me), Value::I32(other)) => Value::I32(*me - *other),
2118            (Value::I64(me), Value::I64(other)) => Value::I64(*me - *other),
2119            (Value::I128(me), Value::I128(other)) => Value::I128(*me - *other),
2120            (Value::U8(me), Value::U8(other)) => Value::U8(*me - *other),
2121            (Value::U16(me), Value::U16(other)) => Value::U16(*me - *other),
2122            (Value::U32(me), Value::U32(other)) => Value::U32(*me - *other),
2123            (Value::U64(me), Value::U64(other)) => Value::U64(*me - *other),
2124            (Value::U128(me), Value::U128(other)) => Value::U128(*me - *other),
2125            (Value::F32(me), Value::F32(other)) => Value::F32(*me - *other),
2126            (Value::F64(me), Value::F64(other)) => Value::F64(*me - *other),
2127            (Value::Data(obj), Value::Data(_)) => obj.sub(other),
2128            (a, b) if a.datatype() != b.datatype() => {
2129                panic!("Unsupported operation, values involved must have same type")
2130            }
2131            (other, _) => panic!("Sub not supported for {}", other.datatype()),
2132        }
2133    }
2134
2135    fn checked_sub(&self, other: &Value) -> Option<Value> {
2136        match (self, other) {
2137            (Value::I8(me), Value::I8(other)) => me.checked_sub(*other).map(|val| Value::I8(val)),
2138            (Value::I16(me), Value::I16(other)) => {
2139                me.checked_sub(*other).map(|val| Value::I16(val))
2140            }
2141            (Value::I32(me), Value::I32(other)) => {
2142                me.checked_sub(*other).map(|val| Value::I32(val))
2143            }
2144            (Value::I64(me), Value::I64(other)) => {
2145                me.checked_sub(*other).map(|val| Value::I64(val))
2146            }
2147            (Value::I128(me), Value::I128(other)) => {
2148                me.checked_sub(*other).map(|val| Value::I128(val))
2149            }
2150            (Value::U8(me), Value::U8(other)) => me.checked_sub(*other).map(|val| Value::U8(val)),
2151            (Value::U16(me), Value::U16(other)) => {
2152                me.checked_sub(*other).map(|val| Value::U16(val))
2153            }
2154            (Value::U32(me), Value::U32(other)) => {
2155                me.checked_sub(*other).map(|val| Value::U32(val))
2156            }
2157            (Value::U64(me), Value::U64(other)) => {
2158                me.checked_sub(*other).map(|val| Value::U64(val))
2159            }
2160            (Value::U128(me), Value::U128(other)) => {
2161                me.checked_sub(*other).map(|val| Value::U128(val))
2162            }
2163            (Value::Data(obj), Value::Data(_)) => obj.checked_sub(other),
2164            (a, b) if a.datatype() != b.datatype() => {
2165                panic!("Unsupported operation, values involved must have same type")
2166            }
2167            (other, _) => panic!("CheckedSub not supported for {}", other.datatype()),
2168        }
2169    }
2170
2171    fn saturating_sub(&self, other: &Value) -> Value {
2172        match (self, other) {
2173            (Value::I8(me), Value::I8(other)) => Value::I8(me.saturating_sub(*other)),
2174            (Value::I16(me), Value::I16(other)) => Value::I16(me.saturating_sub(*other)),
2175            (Value::I32(me), Value::I32(other)) => Value::I32(me.saturating_sub(*other)),
2176            (Value::I64(me), Value::I64(other)) => Value::I64(me.saturating_sub(*other)),
2177            (Value::I128(me), Value::I128(other)) => Value::I128(me.saturating_sub(*other)),
2178            (Value::U8(me), Value::U8(other)) => Value::U8(me.saturating_sub(*other)),
2179            (Value::U16(me), Value::U16(other)) => Value::U16(me.saturating_sub(*other)),
2180            (Value::U32(me), Value::U32(other)) => Value::U32(me.saturating_sub(*other)),
2181            (Value::U64(me), Value::U64(other)) => Value::U64(me.saturating_sub(*other)),
2182            (Value::U128(me), Value::U128(other)) => Value::U128(me.saturating_sub(*other)),
2183            (Value::Data(obj), Value::Data(_)) => obj.saturating_sub(other),
2184            (a, b) if a.datatype() != b.datatype() => {
2185                panic!("Unsupported operation, values involved must have same type")
2186            }
2187            (other, _) => panic!("SaturatingSub not supported for {}", other.datatype()),
2188        }
2189    }
2190
2191    fn wrapping_sub(&self, other: &Value) -> Value {
2192        match (self, other) {
2193            (Value::I8(me), Value::I8(other)) => Value::I8(me.wrapping_sub(*other)),
2194            (Value::I16(me), Value::I16(other)) => Value::I16(me.wrapping_sub(*other)),
2195            (Value::I32(me), Value::I32(other)) => Value::I32(me.wrapping_sub(*other)),
2196            (Value::I64(me), Value::I64(other)) => Value::I64(me.wrapping_sub(*other)),
2197            (Value::I128(me), Value::I128(other)) => Value::I128(me.wrapping_sub(*other)),
2198            (Value::U8(me), Value::U8(other)) => Value::U8(me.wrapping_sub(*other)),
2199            (Value::U16(me), Value::U16(other)) => Value::U16(me.wrapping_sub(*other)),
2200            (Value::U32(me), Value::U32(other)) => Value::U32(me.wrapping_sub(*other)),
2201            (Value::U64(me), Value::U64(other)) => Value::U64(me.wrapping_sub(*other)),
2202            (Value::U128(me), Value::U128(other)) => Value::U128(me.wrapping_sub(*other)),
2203            (Value::Data(obj), Value::Data(_)) => obj.wrapping_sub(other),
2204            (a, b) if a.datatype() != b.datatype() => {
2205                panic!("Unsupported operation, values involved must have same type")
2206            }
2207            (other, _) => panic!("WrappingSub not supported for {}", other.datatype()),
2208        }
2209    }
2210
2211    fn mul(&self, other: &Value) -> Value {
2212        match (self, other) {
2213            (Value::I8(me), Value::I8(other)) => Value::I8(*me * *other),
2214            (Value::I16(me), Value::I16(other)) => Value::I16(*me * *other),
2215            (Value::I32(me), Value::I32(other)) => Value::I32(*me * *other),
2216            (Value::I64(me), Value::I64(other)) => Value::I64(*me * *other),
2217            (Value::I128(me), Value::I128(other)) => Value::I128(*me * *other),
2218            (Value::U8(me), Value::U8(other)) => Value::U8(*me * *other),
2219            (Value::U16(me), Value::U16(other)) => Value::U16(*me * *other),
2220            (Value::U32(me), Value::U32(other)) => Value::U32(*me * *other),
2221            (Value::U64(me), Value::U64(other)) => Value::U64(*me * *other),
2222            (Value::U128(me), Value::U128(other)) => Value::U128(*me * *other),
2223            (Value::F32(me), Value::F32(other)) => Value::F32(*me * *other),
2224            (Value::F64(me), Value::F64(other)) => Value::F64(*me * *other),
2225            (Value::Data(obj), Value::Data(_)) => obj.mul(other),
2226            (a, b) if a.datatype() != b.datatype() => {
2227                panic!("Unsupported operation, values involved must have same type")
2228            }
2229            (other, _) => panic!("Mul not supported for {}", other.datatype()),
2230        }
2231    }
2232
2233    fn checked_mul(&self, other: &Value) -> Option<Value> {
2234        match (self, other) {
2235            (Value::I8(me), Value::I8(other)) => me.checked_mul(*other).map(|val| Value::I8(val)),
2236            (Value::I16(me), Value::I16(other)) => {
2237                me.checked_mul(*other).map(|val| Value::I16(val))
2238            }
2239            (Value::I32(me), Value::I32(other)) => {
2240                me.checked_mul(*other).map(|val| Value::I32(val))
2241            }
2242            (Value::I64(me), Value::I64(other)) => {
2243                me.checked_mul(*other).map(|val| Value::I64(val))
2244            }
2245            (Value::I128(me), Value::I128(other)) => {
2246                me.checked_mul(*other).map(|val| Value::I128(val))
2247            }
2248            (Value::U8(me), Value::U8(other)) => me.checked_mul(*other).map(|val| Value::U8(val)),
2249            (Value::U16(me), Value::U16(other)) => {
2250                me.checked_mul(*other).map(|val| Value::U16(val))
2251            }
2252            (Value::U32(me), Value::U32(other)) => {
2253                me.checked_mul(*other).map(|val| Value::U32(val))
2254            }
2255            (Value::U64(me), Value::U64(other)) => {
2256                me.checked_mul(*other).map(|val| Value::U64(val))
2257            }
2258            (Value::U128(me), Value::U128(other)) => {
2259                me.checked_mul(*other).map(|val| Value::U128(val))
2260            }
2261            (Value::Data(obj), Value::Data(_)) => obj.checked_mul(other),
2262            (a, b) if a.datatype() != b.datatype() => {
2263                panic!("Unsupported operation, values involved must have same type")
2264            }
2265            (other, _) => panic!("CheckedMul not supported for {}", other.datatype()),
2266        }
2267    }
2268
2269    fn saturating_mul(&self, other: &Value) -> Value {
2270        match (self, other) {
2271            (Value::I8(me), Value::I8(other)) => Value::I8(me.saturating_mul(*other)),
2272            (Value::I16(me), Value::I16(other)) => Value::I16(me.saturating_mul(*other)),
2273            (Value::I32(me), Value::I32(other)) => Value::I32(me.saturating_mul(*other)),
2274            (Value::I64(me), Value::I64(other)) => Value::I64(me.saturating_mul(*other)),
2275            (Value::I128(me), Value::I128(other)) => Value::I128(me.saturating_mul(*other)),
2276            (Value::U8(me), Value::U8(other)) => Value::U8(me.saturating_mul(*other)),
2277            (Value::U16(me), Value::U16(other)) => Value::U16(me.saturating_mul(*other)),
2278            (Value::U32(me), Value::U32(other)) => Value::U32(me.saturating_mul(*other)),
2279            (Value::U64(me), Value::U64(other)) => Value::U64(me.saturating_mul(*other)),
2280            (Value::U128(me), Value::U128(other)) => Value::U128(me.saturating_mul(*other)),
2281            (Value::Data(obj), Value::Data(_)) => obj.saturating_mul(other),
2282            (a, b) if a.datatype() != b.datatype() => {
2283                panic!("Unsupported operation, values involved must have same type")
2284            }
2285            (other, _) => panic!("SaturatingMul not supported for {}", other.datatype()),
2286        }
2287    }
2288
2289    fn wrapping_mul(&self, other: &Value) -> Value {
2290        match (self, other) {
2291            (Value::I8(me), Value::I8(other)) => Value::I8(me.wrapping_mul(*other)),
2292            (Value::I16(me), Value::I16(other)) => Value::I16(me.wrapping_mul(*other)),
2293            (Value::I32(me), Value::I32(other)) => Value::I32(me.wrapping_mul(*other)),
2294            (Value::I64(me), Value::I64(other)) => Value::I64(me.wrapping_mul(*other)),
2295            (Value::I128(me), Value::I128(other)) => Value::I128(me.wrapping_mul(*other)),
2296            (Value::U8(me), Value::U8(other)) => Value::U8(me.wrapping_mul(*other)),
2297            (Value::U16(me), Value::U16(other)) => Value::U16(me.wrapping_mul(*other)),
2298            (Value::U32(me), Value::U32(other)) => Value::U32(me.wrapping_mul(*other)),
2299            (Value::U64(me), Value::U64(other)) => Value::U64(me.wrapping_mul(*other)),
2300            (Value::U128(me), Value::U128(other)) => Value::U128(me.wrapping_mul(*other)),
2301            (Value::Data(obj), Value::Data(_)) => obj.wrapping_mul(other),
2302            (a, b) if a.datatype() != b.datatype() => {
2303                panic!("Unsupported operation, values involved must have same type")
2304            }
2305            (other, _) => panic!("WrappingMul not supported for {}", other.datatype()),
2306        }
2307    }
2308
2309    fn div(&self, other: &Value) -> Value {
2310        match (self, other) {
2311            (Value::I8(me), Value::I8(other)) => {
2312                Value::I8(if *other != 0 { *me / *other } else { 0 })
2313            }
2314            (Value::I16(me), Value::I16(other)) => {
2315                Value::I16(if *other != 0 { *me / *other } else { 0 })
2316            }
2317            (Value::I32(me), Value::I32(other)) => {
2318                Value::I32(if *other != 0 { *me / *other } else { 0 })
2319            }
2320            (Value::I64(me), Value::I64(other)) => {
2321                Value::I64(if *other != 0 { *me / *other } else { 0 })
2322            }
2323            (Value::I128(me), Value::I128(other)) => {
2324                Value::I128(if *other != 0 { *me / *other } else { 0 })
2325            }
2326            (Value::U8(me), Value::U8(other)) => {
2327                Value::U8(if *other != 0 { *me / *other } else { 0 })
2328            }
2329            (Value::U16(me), Value::U16(other)) => {
2330                Value::U16(if *other != 0 { *me / *other } else { 0 })
2331            }
2332            (Value::U32(me), Value::U32(other)) => {
2333                Value::U32(if *other != 0 { *me / *other } else { 0 })
2334            }
2335            (Value::U64(me), Value::U64(other)) => {
2336                Value::U64(if *other != 0 { *me / *other } else { 0 })
2337            }
2338            (Value::U128(me), Value::U128(other)) => {
2339                Value::U128(if *other != 0 { *me / *other } else { 0 })
2340            }
2341            (Value::F32(me), Value::F32(other)) => Value::F32(*me / *other),
2342            (Value::F64(me), Value::F64(other)) => Value::F64(*me / *other),
2343            (Value::Data(obj), Value::Data(_)) => obj.div(other),
2344            (a, b) if a.datatype() != b.datatype() => {
2345                panic!("Unsupported operation, values involved must have same type")
2346            }
2347            (other, _) => panic!("Div not supported for {}", other.datatype()),
2348        }
2349    }
2350
2351    fn checked_div(&self, other: &Value) -> Option<Value> {
2352        match (self, other) {
2353            (Value::I8(me), Value::I8(other)) => me.checked_div(*other).map(|val| Value::I8(val)),
2354            (Value::I16(me), Value::I16(other)) => {
2355                me.checked_div(*other).map(|val| Value::I16(val))
2356            }
2357            (Value::I32(me), Value::I32(other)) => {
2358                me.checked_div(*other).map(|val| Value::I32(val))
2359            }
2360            (Value::I64(me), Value::I64(other)) => {
2361                me.checked_div(*other).map(|val| Value::I64(val))
2362            }
2363            (Value::I128(me), Value::I128(other)) => {
2364                me.checked_div(*other).map(|val| Value::I128(val))
2365            }
2366            (Value::U8(me), Value::U8(other)) => me.checked_div(*other).map(|val| Value::U8(val)),
2367            (Value::U16(me), Value::U16(other)) => {
2368                me.checked_div(*other).map(|val| Value::U16(val))
2369            }
2370            (Value::U32(me), Value::U32(other)) => {
2371                me.checked_div(*other).map(|val| Value::U32(val))
2372            }
2373            (Value::U64(me), Value::U64(other)) => {
2374                me.checked_div(*other).map(|val| Value::U64(val))
2375            }
2376            (Value::U128(me), Value::U128(other)) => {
2377                me.checked_div(*other).map(|val| Value::U128(val))
2378            }
2379            (Value::Data(obj), Value::Data(_)) => obj.checked_div(other),
2380            (a, b) if a.datatype() != b.datatype() => {
2381                panic!("Unsupported operation, values involved must have same type")
2382            }
2383            (other, _) => panic!("CheckedDiv not supported for {}", other.datatype()),
2384        }
2385    }
2386
2387    fn rem(&self, other: &Value) -> Value {
2388        match (self, other) {
2389            (Value::I8(me), Value::I8(other)) => {
2390                Value::I8(if *other != 0 { *me % *other } else { 0 })
2391            }
2392            (Value::I16(me), Value::I16(other)) => {
2393                Value::I16(if *other != 0 { *me % *other } else { 0 })
2394            }
2395            (Value::I32(me), Value::I32(other)) => {
2396                Value::I32(if *other != 0 { *me % *other } else { 0 })
2397            }
2398            (Value::I64(me), Value::I64(other)) => {
2399                Value::I64(if *other != 0 { *me % *other } else { 0 })
2400            }
2401            (Value::I128(me), Value::I128(other)) => {
2402                Value::I128(if *other != 0 { *me % *other } else { 0 })
2403            }
2404            (Value::U8(me), Value::U8(other)) => {
2405                Value::U8(if *other != 0 { *me % *other } else { 0 })
2406            }
2407            (Value::U16(me), Value::U16(other)) => {
2408                Value::U16(if *other != 0 { *me % *other } else { 0 })
2409            }
2410            (Value::U32(me), Value::U32(other)) => {
2411                Value::U32(if *other != 0 { *me % *other } else { 0 })
2412            }
2413            (Value::U64(me), Value::U64(other)) => {
2414                Value::U64(if *other != 0 { *me % *other } else { 0 })
2415            }
2416            (Value::U128(me), Value::U128(other)) => {
2417                Value::U128(if *other != 0 { *me % *other } else { 0 })
2418            }
2419            (Value::F32(me), Value::F32(other)) => Value::F32(*me % *other),
2420            (Value::F64(me), Value::F64(other)) => Value::F64(*me % *other),
2421            (Value::Data(obj), Value::Data(_)) => obj.rem(other),
2422            (a, b) if a.datatype() != b.datatype() => {
2423                panic!("Unsupported operation, values involved must have same type")
2424            }
2425            (other, _) => panic!("Rem not supported for {}", other.datatype()),
2426        }
2427    }
2428
2429    fn checked_rem(&self, other: &Value) -> Option<Value> {
2430        match (self, other) {
2431            (Value::I8(me), Value::I8(other)) => me.checked_rem(*other).map(|val| Value::I8(val)),
2432            (Value::I16(me), Value::I16(other)) => {
2433                me.checked_rem(*other).map(|val| Value::I16(val))
2434            }
2435            (Value::I32(me), Value::I32(other)) => {
2436                me.checked_rem(*other).map(|val| Value::I32(val))
2437            }
2438            (Value::I64(me), Value::I64(other)) => {
2439                me.checked_rem(*other).map(|val| Value::I64(val))
2440            }
2441            (Value::I128(me), Value::I128(other)) => {
2442                me.checked_rem(*other).map(|val| Value::I128(val))
2443            }
2444            (Value::U8(me), Value::U8(other)) => me.checked_rem(*other).map(|val| Value::U8(val)),
2445            (Value::U16(me), Value::U16(other)) => {
2446                me.checked_rem(*other).map(|val| Value::U16(val))
2447            }
2448            (Value::U32(me), Value::U32(other)) => {
2449                me.checked_rem(*other).map(|val| Value::U32(val))
2450            }
2451            (Value::U64(me), Value::U64(other)) => {
2452                me.checked_rem(*other).map(|val| Value::U64(val))
2453            }
2454            (Value::U128(me), Value::U128(other)) => {
2455                me.checked_rem(*other).map(|val| Value::U128(val))
2456            }
2457            (Value::Data(obj), Value::Data(_)) => obj.checked_rem(other),
2458            (a, b) if a.datatype() != b.datatype() => {
2459                panic!("Unsupported operation, values involved must have same type")
2460            }
2461            (other, _) => panic!("CheckedRem not supported for {}", other.datatype()),
2462        }
2463    }
2464
2465    fn neg(&self) -> Value {
2466        match self {
2467            Value::I8(me) => Value::I8(-*me),
2468            Value::I16(me) => Value::I16(-*me),
2469            Value::I32(me) => Value::I32(-*me),
2470            Value::I64(me) => Value::I64(-*me),
2471            Value::I128(me) => Value::I128(-*me),
2472            Value::F32(me) => Value::F32(-*me),
2473            Value::F64(me) => Value::F64(-*me),
2474            Value::Data(obj) => obj.neg(),
2475            other => panic!("Neg not supported for {}", other.datatype()),
2476        }
2477    }
2478
2479    fn checked_neg(&self) -> Option<Value> {
2480        match self {
2481            Value::I8(me) => me.checked_neg().map(|val| Value::I8(val)),
2482            Value::I16(me) => me.checked_neg().map(|val| Value::I16(val)),
2483            Value::I32(me) => me.checked_neg().map(|val| Value::I32(val)),
2484            Value::I64(me) => me.checked_neg().map(|val| Value::I64(val)),
2485            Value::I128(me) => me.checked_neg().map(|val| Value::I128(val)),
2486            Value::Data(obj) => obj.checked_neg(),
2487            other => panic!("CheckedNeg not supported for {}", other.datatype()),
2488        }
2489    }
2490
2491    fn wrapping_neg(&self) -> Value {
2492        match self {
2493            Value::I8(me) => Value::I8(me.wrapping_neg()),
2494            Value::I16(me) => Value::I16(me.wrapping_neg()),
2495            Value::I32(me) => Value::I32(me.wrapping_neg()),
2496            Value::I64(me) => Value::I64(me.wrapping_neg()),
2497            Value::I128(me) => Value::I128(me.wrapping_neg()),
2498            Value::Data(obj) => obj.wrapping_neg(),
2499            other => panic!("WrappingNeg not supported for {}", other.datatype()),
2500        }
2501    }
2502
2503    fn pow(&self, exp: &u32) -> Value {
2504        match self {
2505            Value::I8(me) => Value::I8(me.pow(*exp)),
2506            Value::I16(me) => Value::I16(me.pow(*exp)),
2507            Value::I32(me) => Value::I32(me.pow(*exp)),
2508            Value::I64(me) => Value::I64(me.pow(*exp)),
2509            Value::I128(me) => Value::I128(me.pow(*exp)),
2510            Value::U8(me) => Value::U8(me.pow(*exp)),
2511            Value::U16(me) => Value::U16(me.pow(*exp)),
2512            Value::U32(me) => Value::U32(me.pow(*exp)),
2513            Value::U64(me) => Value::U64(me.pow(*exp)),
2514            Value::U128(me) => Value::U128(me.pow(*exp)),
2515            Value::Data(obj) => obj.pow(exp),
2516            other => panic!("Pow not supported for {}", other.datatype()),
2517        }
2518    }
2519
2520    fn checked_pow(&self, exp: &u32) -> Option<Value> {
2521        match self {
2522            Value::I8(me) => me.checked_pow(*exp).map(|val| Value::I8(val)),
2523            Value::I16(me) => me.checked_pow(*exp).map(|val| Value::I16(val)),
2524            Value::I32(me) => me.checked_pow(*exp).map(|val| Value::I32(val)),
2525            Value::I64(me) => me.checked_pow(*exp).map(|val| Value::I64(val)),
2526            Value::I128(me) => me.checked_pow(*exp).map(|val| Value::I128(val)),
2527            Value::U8(me) => me.checked_pow(*exp).map(|val| Value::U8(val)),
2528            Value::U16(me) => me.checked_pow(*exp).map(|val| Value::U16(val)),
2529            Value::U32(me) => me.checked_pow(*exp).map(|val| Value::U32(val)),
2530            Value::U64(me) => me.checked_pow(*exp).map(|val| Value::U64(val)),
2531            Value::U128(me) => me.checked_pow(*exp).map(|val| Value::U128(val)),
2532            Value::Data(obj) => obj.checked_pow(exp),
2533            other => panic!("CheckedPow not supported for {}", other.datatype()),
2534        }
2535    }
2536
2537    fn euclid_div(&self, other: &Value) -> Value {
2538        match (self, other) {
2539            (Value::I8(me), Value::I8(other)) => Value::I8(if *other != 0 {
2540                me.overflowing_div_euclid(*other).0
2541            } else {
2542                0
2543            }),
2544            (Value::I16(me), Value::I16(other)) => Value::I16(if *other != 0 {
2545                me.overflowing_div_euclid(*other).0
2546            } else {
2547                0
2548            }),
2549            (Value::I32(me), Value::I32(other)) => Value::I32(if *other != 0 {
2550                me.overflowing_div_euclid(*other).0
2551            } else {
2552                0
2553            }),
2554            (Value::I64(me), Value::I64(other)) => Value::I64(if *other != 0 {
2555                me.overflowing_div_euclid(*other).0
2556            } else {
2557                0
2558            }),
2559            (Value::I128(me), Value::I128(other)) => Value::I128(if *other != 0 {
2560                me.overflowing_div_euclid(*other).0
2561            } else {
2562                0
2563            }),
2564            (Value::U8(me), Value::U8(other)) => Value::U8(if *other != 0 {
2565                me.overflowing_div_euclid(*other).0
2566            } else {
2567                0
2568            }),
2569            (Value::U16(me), Value::U16(other)) => Value::U16(if *other != 0 {
2570                me.overflowing_div_euclid(*other).0
2571            } else {
2572                0
2573            }),
2574            (Value::U32(me), Value::U32(other)) => Value::U32(if *other != 0 {
2575                me.overflowing_div_euclid(*other).0
2576            } else {
2577                0
2578            }),
2579            (Value::U64(me), Value::U64(other)) => Value::U64(if *other != 0 {
2580                me.overflowing_div_euclid(*other).0
2581            } else {
2582                0
2583            }),
2584            (Value::U128(me), Value::U128(other)) => Value::U128(if *other != 0 {
2585                me.overflowing_div_euclid(*other).0
2586            } else {
2587                0
2588            }),
2589            (Value::F32(me), Value::F32(other)) => Value::F32(me.div_euclid(*other)),
2590            (Value::F64(me), Value::F64(other)) => Value::F64(me.div_euclid(*other)),
2591            (Value::Data(obj), Value::Data(_)) => obj.euclid_div(other),
2592            (a, b) if a.datatype() != b.datatype() => {
2593                panic!("Unsupported operation, values involved must have same type")
2594            }
2595            (other, _) => panic!("Euclid not supported for {}", other.datatype()),
2596        }
2597    }
2598
2599    fn euclid_rem(&self, other: &Value) -> Value {
2600        match (self, other) {
2601            (Value::I8(me), Value::I8(other)) => Value::I8(if *other != 0 {
2602                me.overflowing_rem_euclid(*other).0
2603            } else {
2604                0
2605            }),
2606            (Value::I16(me), Value::I16(other)) => Value::I16(if *other != 0 {
2607                me.overflowing_rem_euclid(*other).0
2608            } else {
2609                0
2610            }),
2611            (Value::I32(me), Value::I32(other)) => Value::I32(if *other != 0 {
2612                me.overflowing_rem_euclid(*other).0
2613            } else {
2614                0
2615            }),
2616            (Value::I64(me), Value::I64(other)) => Value::I64(if *other != 0 {
2617                me.overflowing_rem_euclid(*other).0
2618            } else {
2619                0
2620            }),
2621            (Value::I128(me), Value::I128(other)) => Value::I128(if *other != 0 {
2622                me.overflowing_rem_euclid(*other).0
2623            } else {
2624                0
2625            }),
2626            (Value::U8(me), Value::U8(other)) => Value::U8(if *other != 0 {
2627                me.overflowing_rem_euclid(*other).0
2628            } else {
2629                0
2630            }),
2631            (Value::U16(me), Value::U16(other)) => Value::U16(if *other != 0 {
2632                me.overflowing_rem_euclid(*other).0
2633            } else {
2634                0
2635            }),
2636            (Value::U32(me), Value::U32(other)) => Value::U32(if *other != 0 {
2637                me.overflowing_rem_euclid(*other).0
2638            } else {
2639                0
2640            }),
2641            (Value::U64(me), Value::U64(other)) => Value::U64(if *other != 0 {
2642                me.overflowing_rem_euclid(*other).0
2643            } else {
2644                0
2645            }),
2646            (Value::U128(me), Value::U128(other)) => Value::U128(if *other != 0 {
2647                me.overflowing_rem_euclid(*other).0
2648            } else {
2649                0
2650            }),
2651            (Value::F32(me), Value::F32(other)) => Value::F32(me.rem_euclid(*other)),
2652            (Value::F64(me), Value::F64(other)) => Value::F64(me.rem_euclid(*other)),
2653            (Value::Data(obj), Value::Data(_)) => obj.euclid_rem(other),
2654            (a, b) if a.datatype() != b.datatype() => {
2655                panic!("Unsupported operation, values involved must have same type")
2656            }
2657            (other, _) => panic!("Euclid not supported for {}", other.datatype()),
2658        }
2659    }
2660
2661    fn checked_euclid_div(&self, other: &Value) -> Option<Value> {
2662        match (self, other) {
2663            (Value::I8(me), Value::I8(other)) => {
2664                me.checked_div_euclid(*other).map(|val| Value::I8(val))
2665            }
2666            (Value::I16(me), Value::I16(other)) => {
2667                me.checked_div_euclid(*other).map(|val| Value::I16(val))
2668            }
2669            (Value::I32(me), Value::I32(other)) => {
2670                me.checked_div_euclid(*other).map(|val| Value::I32(val))
2671            }
2672            (Value::I64(me), Value::I64(other)) => {
2673                me.checked_div_euclid(*other).map(|val| Value::I64(val))
2674            }
2675            (Value::I128(me), Value::I128(other)) => {
2676                me.checked_div_euclid(*other).map(|val| Value::I128(val))
2677            }
2678            (Value::U8(me), Value::U8(other)) => {
2679                me.checked_div_euclid(*other).map(|val| Value::U8(val))
2680            }
2681            (Value::U16(me), Value::U16(other)) => {
2682                me.checked_div_euclid(*other).map(|val| Value::U16(val))
2683            }
2684            (Value::U32(me), Value::U32(other)) => {
2685                me.checked_div_euclid(*other).map(|val| Value::U32(val))
2686            }
2687            (Value::U64(me), Value::U64(other)) => {
2688                me.checked_div_euclid(*other).map(|val| Value::U64(val))
2689            }
2690            (Value::U128(me), Value::U128(other)) => {
2691                me.checked_div_euclid(*other).map(|val| Value::U128(val))
2692            }
2693            (Value::Data(obj), Value::Data(_)) => obj.checked_euclid_div(other),
2694            (a, b) if a.datatype() != b.datatype() => {
2695                panic!("Unsupported operation, values involved must have same type")
2696            }
2697            (other, _) => panic!("CheckedEuclid not supported for {}", other.datatype()),
2698        }
2699    }
2700
2701    fn checked_euclid_rem(&self, other: &Value) -> Option<Value> {
2702        match (self, other) {
2703            (Value::I8(me), Value::I8(other)) => {
2704                me.checked_rem_euclid(*other).map(|val| Value::I8(val))
2705            }
2706            (Value::I16(me), Value::I16(other)) => {
2707                me.checked_rem_euclid(*other).map(|val| Value::I16(val))
2708            }
2709            (Value::I32(me), Value::I32(other)) => {
2710                me.checked_rem_euclid(*other).map(|val| Value::I32(val))
2711            }
2712            (Value::I64(me), Value::I64(other)) => {
2713                me.checked_rem_euclid(*other).map(|val| Value::I64(val))
2714            }
2715            (Value::I128(me), Value::I128(other)) => {
2716                me.checked_rem_euclid(*other).map(|val| Value::I128(val))
2717            }
2718            (Value::U8(me), Value::U8(other)) => {
2719                me.checked_rem_euclid(*other).map(|val| Value::U8(val))
2720            }
2721            (Value::U16(me), Value::U16(other)) => {
2722                me.checked_rem_euclid(*other).map(|val| Value::U16(val))
2723            }
2724            (Value::U32(me), Value::U32(other)) => {
2725                me.checked_rem_euclid(*other).map(|val| Value::U32(val))
2726            }
2727            (Value::U64(me), Value::U64(other)) => {
2728                me.checked_rem_euclid(*other).map(|val| Value::U64(val))
2729            }
2730            (Value::U128(me), Value::U128(other)) => {
2731                me.checked_rem_euclid(*other).map(|val| Value::U128(val))
2732            }
2733            (Value::Data(obj), Value::Data(_)) => obj.checked_euclid_rem(other),
2734            (a, b) if a.datatype() != b.datatype() => {
2735                panic!("Unsupported operation, values involved must have same type")
2736            }
2737            (other, _) => panic!("CheckedEuclid not supported for {}", other.datatype()),
2738        }
2739    }
2740
2741    fn hash(&self, mut state: &mut dyn std::hash::Hasher) {
2742        match self {
2743            Value::I8(me) => me.hash(&mut state),
2744            Value::I16(me) => me.hash(&mut state),
2745            Value::I32(me) => me.hash(&mut state),
2746            Value::I64(me) => me.hash(&mut state),
2747            Value::I128(me) => me.hash(&mut state),
2748            Value::U8(me) => me.hash(&mut state),
2749            Value::U16(me) => me.hash(&mut state),
2750            Value::U32(me) => me.hash(&mut state),
2751            Value::U64(me) => me.hash(&mut state),
2752            Value::U128(me) => me.hash(&mut state),
2753            Value::Bool(me) => me.hash(&mut state),
2754            Value::Byte(me) => me.hash(&mut state),
2755            Value::Char(me) => me.hash(&mut state),
2756            Value::String(me) => me.hash(&mut state),
2757            Value::Vec(me) => me.iter().for_each(|elmt| elmt.hash(&mut state)),
2758            Value::Option(me) => {
2759                if let Some(elmt) = me {
2760                    elmt.hash(&mut state)
2761                } else {
2762                    ().hash(&mut state)
2763                }
2764            }
2765            Value::Data(me) => me.hash(&mut state),
2766            other => panic!("Hash not supported for {}", other.datatype()),
2767        }
2768    }
2769
2770    fn serialize(
2771        &self,
2772        serializer: &mut dyn erased_serde::Serializer,
2773    ) -> Result<(), erased_serde::Error> {
2774        self.erased_serialize(serializer)
2775    }
2776
2777    fn display(&self, mut f: &mut std::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
2778        match self {
2779            Value::Data(data) => data.display(&mut f),
2780            _ => self.fmt(f),
2781        }
2782    }
2783}
2784
2785impl serde::Serialize for Value {
2786    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
2787    where
2788        S: serde::Serializer,
2789    {
2790        match self {
2791            Value::Void(_) => serializer.serialize_unit(),
2792            Value::I8(me) => serializer.serialize_i8(*me),
2793            Value::I16(me) => serializer.serialize_i16(*me),
2794            Value::I32(me) => serializer.serialize_i32(*me),
2795            Value::I64(me) => serializer.serialize_i64(*me),
2796            Value::I128(me) => serializer.serialize_i128(*me),
2797            Value::U8(me) => serializer.serialize_u8(*me),
2798            Value::U16(me) => serializer.serialize_u16(*me),
2799            Value::U32(me) => serializer.serialize_u32(*me),
2800            Value::U64(me) => serializer.serialize_u64(*me),
2801            Value::U128(me) => serializer.serialize_u128(*me),
2802            Value::F32(me) => serializer.serialize_f32(*me),
2803            Value::F64(me) => serializer.serialize_f64(*me),
2804            Value::Bool(me) => serializer.serialize_bool(*me),
2805            Value::Byte(me) => serializer.serialize_bytes(&[*me]),
2806            Value::Char(me) => serializer.serialize_char(*me),
2807            Value::String(me) => serializer.serialize_str(me),
2808            Value::Vec(me) => {
2809                let mut ser = serializer.serialize_seq(Some(me.len()))?;
2810                for val in me {
2811                    ser.serialize_element(val)?;
2812                }
2813                ser.end()
2814            }
2815            Value::Option(me) => {
2816                if let Some(elmt) = me {
2817                    serializer.serialize_some(elmt)
2818                } else {
2819                    serializer.serialize_none()
2820                }
2821            }
2822            Value::Data(data) => serde::Serialize::serialize(&data, serializer),
2823        }
2824    }
2825}
2826
2827impl core::fmt::Display for Value {
2828    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
2829        match self {
2830            Value::Void(_) => write!(f, "_"),
2831            Value::I8(v) => write!(f, "{}", v),
2832            Value::I16(v) => write!(f, "{}", v),
2833            Value::I32(v) => write!(f, "{}", v),
2834            Value::I64(v) => write!(f, "{}", v),
2835            Value::I128(v) => write!(f, "{}", v),
2836            Value::U8(v) => write!(f, "{}", v),
2837            Value::U16(v) => write!(f, "{}", v),
2838            Value::U32(v) => write!(f, "{}", v),
2839            Value::U64(v) => write!(f, "{}", v),
2840            Value::U128(v) => write!(f, "{}", v),
2841            Value::F32(v) => write!(f, "{}", v),
2842            Value::F64(v) => write!(f, "{}", v),
2843            Value::Bool(v) => write!(f, "{}", v),
2844            Value::Byte(v) => write!(f, "0x{}", hex::encode([*v])),
2845            Value::Char(v) => write!(
2846                f,
2847                "'{}'",
2848                match v {
2849                    '\n' => r"\n".to_string(),
2850                    '\r' => r"\r".to_string(),
2851                    '\t' => r"\t".to_string(),
2852                    '\\' => r"\\".to_string(),
2853                    '\0' => r"\0".to_string(),
2854                    '\'' => r"\'".to_string(),
2855                    c if c.is_control() => c.escape_unicode().to_string(),
2856                    c => c.to_string(),
2857                }
2858            ),
2859            Value::String(v) => {
2860                if v.chars()
2861                    .any(|c| c.is_control() && c != '\n' && c != '\t' && c != '\r')
2862                    || v.contains(['\0'])
2863                    || !v.contains(['\n', '\t', '\r'])
2864                {
2865                    write!(
2866                        f,
2867                        "\"{}\"",
2868                        v.chars()
2869                            .map(|c| match c {
2870                                '\n' => r"\n".to_string(),
2871                                '\r' => r"\r".to_string(),
2872                                '\t' => r"\t".to_string(),
2873                                '\\' => r"\\".to_string(),
2874                                '\0' => r"\0".to_string(),
2875                                '\"' => r#"\""#.to_string(),
2876                                c if c.is_control() => c.escape_unicode().to_string(),
2877                                c => c.to_string(),
2878                            })
2879                            .collect::<String>()
2880                    )
2881                } else {
2882                    let mut start_braces: String = "{".into();
2883                    let mut end_braces: String = "}".into();
2884                    while v.contains(&start_braces) || v.contains(&end_braces) {
2885                        start_braces.push('{');
2886                        end_braces.push('}');
2887                    }
2888                    write!(f, "${start_braces}{v}{end_braces}")
2889                }
2890            }
2891            Value::Option(v) => {
2892                if let Some(v) = v {
2893                    write!(f, "{v}")
2894                } else {
2895                    write!(f, "_")
2896                }
2897            }
2898            Value::Vec(v) => write!(
2899                f,
2900                "[{}]",
2901                v.iter()
2902                    .map(|val| ToString::to_string(val))
2903                    .collect::<Vec<_>>()
2904                    .join(", ")
2905            ),
2906
2907            Value::Data(obj) => write!(f, "/* {} */", obj.descriptor().identifier().name()),
2908        }
2909    }
2910}