anyrust/
lib.rs

1#![allow(non_upper_case_globals)]
2#![allow(clippy::vec_init_then_push)]
3#![allow(clippy::if_same_then_else)]
4
5use std::{
6    any::TypeId,
7    borrow::BorrowMut,
8    collections::HashMap,
9    fmt::{Debug, Display},
10    hash::Hash,
11    ops::{
12        Add, AddAssign, Div, DivAssign, Index, IndexMut, Mul, MulAssign, Neg, Not, Shr, Sub,
13        SubAssign,
14    },
15    rc::Rc,
16};
17
18use dyn_clone::{clone_trait_object, DynClone};
19
20/// shortcut function for creating any value
21pub fn any(value: impl Into<Any>) -> Any {
22    value.into()
23}
24
25/// any trait
26pub trait Anyable:
27    std::any::Any + Send + Sync + std::fmt::Debug + DynClone + Display + AutoCast + DynClone
28{
29}
30
31clone_trait_object!(Anyable);
32
33impl<
34        T: std::any::Any + Send + Sync + std::fmt::Debug + DynClone + Display + AutoCast + DynClone,
35    > Anyable for T
36{
37}
38
39/// type of null value
40#[derive(Debug, Clone, Copy)]
41pub struct Null;
42
43/// null value
44#[allow(non_upper_case_globals)]
45pub(crate) const _null: Null = Null {};
46
47/// function type
48pub struct Function {
49    f: Rc<dyn Fn(Any) -> Any>,
50    args_count: usize,
51}
52
53impl Debug for Function {
54    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55        write!(f, "<function>")
56    }
57}
58
59unsafe impl Send for Function {}
60
61unsafe impl Sync for Function {}
62
63impl Clone for Function {
64    fn clone(&self) -> Self {
65        Self {
66            f: self.f.clone(),
67            args_count: self.args_count,
68        }
69    }
70}
71
72impl Function {
73    pub fn new(f: impl Fn(Any) -> Any + 'static + Send + Sync, args_count: usize) -> Self {
74        Self {
75            f: Rc::new(f),
76            args_count,
77        }
78    }
79
80    pub fn call(&self, args: Any) -> Any {
81        let mut rc = self.f.clone();
82        let borrowed = rc.borrow_mut();
83
84        borrowed(args)
85    }
86
87    pub fn composite(&self, other: Self) -> Self {
88        use crate as anyrust;
89
90        let f = self.f.clone();
91        let other_f = other.f.clone();
92        let args_count = self.args_count;
93
94        Self {
95            f: Rc::new(move |args| {
96                let result = f(args.clone());
97                other_f(params![result])
98            }),
99            args_count,
100        }
101    }
102}
103
104#[cfg(test)]
105mod test_function {
106    use super::*;
107    use crate as anyrust;
108
109    #[test]
110    fn test_function() {
111        let f = Function::new(
112            |args| {
113                let mut sum = Any::from(0);
114                for arg in args.to_array().0 {
115                    sum += arg;
116                }
117                sum
118            },
119            1,
120        );
121
122        let result = f.call(array![1, 2, 3, 4, 5]);
123        assert_eq!(result, Any::from(15_i64));
124
125        let result = f.call(array![1, 2, 3, 4, 5, 7]);
126        assert_eq!(result, Any::from(22_i64));
127    }
128
129    #[test]
130    fn test_composite_function() {
131        let add = Function::new(
132            |args| {
133                let lhs = args[0].clone();
134                let rhs = args[1].clone();
135                lhs + rhs
136            },
137            2,
138        );
139
140        let negative = Function::new(
141            |args| {
142                let value = args[0].clone();
143                value * Any::from(-1)
144            },
145            1,
146        );
147
148        let add_result = add.call(array![1, 2]);
149        assert_eq!(add_result, Any::from(3_i64), "add_result: {:?}", add_result);
150
151        let negative_result = negative.call(array![add_result]);
152        assert_eq!(
153            negative_result,
154            Any::from(-3_i64),
155            "negative_result: {:?}",
156            negative_result
157        );
158
159        let composited = add.composite(negative);
160
161        let result = composited.call(array![1, 2]);
162        assert_eq!(result, Any::from(-3_i64), "result: {:?}", result);
163    }
164}
165
166#[allow(non_upper_case_globals)]
167/// array type
168#[derive(Debug, Clone)]
169pub struct Array(Vec<Any>);
170
171impl Default for Array {
172    fn default() -> Self {
173        Self::new()
174    }
175}
176
177impl Array {
178    pub fn new() -> Self {
179        Self(Vec::new())
180    }
181
182    pub fn push(&mut self, value: impl Into<Any>) {
183        self.0.push(value.into());
184    }
185
186    pub fn pop(&mut self) -> Option<Any> {
187        self.0.pop()
188    }
189
190    pub fn shift(&mut self) -> Option<Any> {
191        let first_value = self.0.first().cloned()?;
192        self.0.remove(0);
193
194        Some(first_value)
195    }
196
197    pub fn unshift(&mut self, value: impl Into<Any>) {
198        self.0.insert(0, value.into());
199    }
200
201    pub fn length(&self) -> usize {
202        self.0.len()
203    }
204
205    pub fn is_empty(&self) -> bool {
206        self.0.is_empty()
207    }
208
209    pub fn reverse(&mut self) -> &mut Self {
210        self.0.reverse();
211        self
212    }
213}
214
215#[cfg(test)]
216mod test_array {
217    use super::*;
218
219    #[test]
220    fn test_push() {
221        let mut a = Array::new();
222        assert_eq!(a.length(), 0);
223
224        a.push(Any::new(1));
225        assert_eq!(a.length(), 1);
226        assert_eq!(a.0[0], Any::from(1));
227    }
228
229    #[test]
230    fn test_pop() {
231        let mut a = Array::new();
232        a.push(Any::new(1));
233        a.push(Any::new(2));
234        a.push(Any::new(3));
235
236        assert_eq!(a.length(), 3);
237
238        let value = a.pop().unwrap();
239        assert_eq!(value, Any::from(3));
240        assert_eq!(a.length(), 2);
241    }
242
243    #[test]
244    fn test_shift() {
245        let mut a = Array::new();
246        a.push(Any::new(1));
247        a.push(Any::new(2));
248        a.push(Any::new(3));
249
250        assert_eq!(a.length(), 3);
251
252        let value = a.shift().unwrap();
253        assert_eq!(value, Any::from(1));
254        assert_eq!(a.length(), 2);
255    }
256
257    #[test]
258    fn test_unshift() {
259        let mut a = Array::new();
260        assert_eq!(a.length(), 0);
261
262        a.unshift(Any::new(1));
263        assert_eq!(a.length(), 1);
264        assert_eq!(a.0[0], Any::from(1));
265
266        a.unshift(Any::new(2));
267        assert_eq!(a.length(), 2);
268        assert_eq!(a.0[0], Any::from(2));
269    }
270
271    #[test]
272    fn test_length() {
273        let mut a = Array::new();
274        assert_eq!(a.length(), 0);
275
276        a.push(Any::new(1));
277        assert_eq!(a.length(), 1);
278
279        a.push(Any::new(2));
280        assert_eq!(a.length(), 2);
281
282        a.push(Any::new(3));
283        assert_eq!(a.length(), 3);
284    }
285
286    #[test]
287    fn test_is_empty() {
288        let mut a = Array::new();
289        assert!(a.is_empty());
290
291        a.push(Any::new(1));
292        assert!(!a.is_empty());
293    }
294
295    #[test]
296    fn test_reverse() {
297        let mut a = Array::new();
298        a.push(Any::new(1));
299        a.push(Any::new(2));
300        a.push(Any::new(3));
301
302        assert_eq!(a.length(), 3);
303        assert_eq!(a.0[0], Any::from(1));
304        assert_eq!(a.0[1], Any::from(2));
305        assert_eq!(a.0[2], Any::from(3));
306
307        a.reverse();
308
309        assert_eq!(a.length(), 3);
310        assert_eq!(a.0[0], Any::from(3));
311        assert_eq!(a.0[1], Any::from(2));
312        assert_eq!(a.0[2], Any::from(1));
313    }
314}
315
316/// Any Tuple type
317#[derive(Debug, Clone)]
318pub struct Pair((Any, Any));
319
320impl Pair {
321    pub fn new(key: impl Into<Any>, value: impl Into<Any>) -> Self {
322        Self((key.into(), value.into()))
323    }
324
325    pub fn to_tuple(&self) -> (Any, Any) {
326        self.0.to_owned()
327    }
328}
329
330/// key-value map type
331#[derive(Debug, Clone)]
332pub struct Map(std::collections::HashMap<Any, Any>);
333
334impl Default for Map {
335    fn default() -> Self {
336        Self::new()
337    }
338}
339
340impl Map {
341    pub fn new() -> Self {
342        Self(HashMap::new())
343    }
344
345    pub fn set(&mut self, key: impl Into<Any>, value: impl Into<Any>) {
346        self.0.insert(key.into(), value.into());
347    }
348
349    pub fn delete(&mut self, key: &Any) -> Option<Any> {
350        self.0.remove(key)
351    }
352
353    pub fn get(&self, key: &Any) -> Option<&Any> {
354        self.0.get(key)
355    }
356
357    pub fn get_mut(&mut self, key: &Any) -> Option<&mut Any> {
358        self.0.get_mut(key)
359    }
360
361    pub fn length(&self) -> usize {
362        self.0.len()
363    }
364
365    pub fn is_empty(&self) -> bool {
366        self.0.is_empty()
367    }
368}
369
370#[cfg(test)]
371mod test_map {
372    use super::*;
373
374    #[test]
375    fn test_set() {
376        let mut m = Map::new();
377        assert_eq!(m.length(), 0);
378
379        m.set(Any::new("key"), Any::new("value"));
380        assert_eq!(m.length(), 1);
381        assert_eq!(m.0.get(&Any::new("key")).unwrap(), &Any::new("value"));
382    }
383
384    #[test]
385    fn test_delete() {
386        let mut m = Map::new();
387        m.set(Any::new("key"), Any::new("value"));
388        assert_eq!(m.length(), 1);
389
390        let value = m.delete(&Any::new("key")).unwrap();
391        assert_eq!(value, Any::new("value"));
392        assert_eq!(m.length(), 0);
393    }
394
395    #[test]
396    fn test_get() {
397        let mut m = Map::new();
398        m.set(Any::new("key"), Any::new("value"));
399
400        let value = m.get(&Any::new("key")).unwrap();
401        assert_eq!(value, &Any::new("value"));
402    }
403
404    #[test]
405    fn test_get_mut() {
406        let mut m = Map::new();
407        m.set(Any::new("key"), Any::new("value"));
408
409        let value = m.get_mut(&Any::new("key")).unwrap();
410        assert_eq!(value, &Any::new("value"));
411    }
412
413    #[test]
414    fn test_length() {
415        let mut m = Map::new();
416        assert_eq!(m.length(), 0);
417
418        m.set(Any::new("key"), Any::new("value"));
419        assert_eq!(m.length(), 1);
420
421        m.set(Any::new("key2"), Any::new("value2"));
422        assert_eq!(m.length(), 2);
423    }
424
425    #[test]
426    fn test_is_empty() {
427        let mut m = Map::new();
428        assert!(m.is_empty());
429
430        m.set(Any::new("key"), Any::new("value"));
431        assert!(!m.is_empty());
432    }
433}
434
435/// castable trait
436pub trait AutoCast:
437    ToInteger + ToFloat + ToArray + ToMap + ToBoolean + ToStr + ToPair + ToFunction
438{
439}
440
441impl<T: ToInteger + ToFloat + ToArray + ToMap + ToBoolean + ToStr + ToPair + ToFunction> AutoCast
442    for T
443{
444}
445
446/// Trait for casting: Defines how to convert when cast to an integer.
447pub trait ToInteger {
448    fn to_integer(&self) -> i64;
449}
450
451/// Trait for casting: Defines how to convert when cast to a float.
452pub trait ToFloat {
453    fn to_float(&self) -> f64;
454}
455
456/// Trait for casting: Defines how to convert when cast to a string.
457pub trait ToStr {
458    fn to_str(&self) -> String;
459}
460
461/// Trait for casting: Defines how to convert when cast to an array.
462pub trait ToArray {
463    fn to_array(&self) -> Array;
464
465    fn to_array_ref(&self) -> &Array {
466        &EMPTY_ARRAY
467    }
468
469    fn to_array_mut(&mut self) -> &mut Array {
470        unreachable!()
471    }
472}
473
474/// Trait for casting: Defines how to convert when cast to a map.
475pub trait ToMap {
476    fn to_map(&self) -> Map;
477
478    fn to_map_ref(&self) -> &Map {
479        &EMPTY_MAP
480    }
481
482    fn to_map_mut(&mut self) -> &mut Map {
483        unreachable!()
484    }
485}
486
487/// Trait for casting: Defines how to convert when cast to a Pair.
488pub trait ToPair {
489    fn to_pair(&self) -> Pair {
490        Pair::new(null.clone(), null.clone())
491    }
492}
493
494/// Trait for casting: Defines how to convert when cast to a boolean.
495pub trait ToBoolean {
496    fn to_boolean(&self) -> bool;
497}
498
499/// Trait for casting: Defines how to convert when cast to a function.
500pub trait ToFunction {
501    fn to_function(&self) -> Function {
502        Function::new(|_| Any::from(_null), 0)
503    }
504}
505
506impl Display for Any {
507    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
508        write!(f, "{}", self.data)
509    }
510}
511
512/// array 트레잇 구현
513impl PartialEq for Array {
514    fn eq(&self, other: &Self) -> bool {
515        self.0 == other.0
516    }
517}
518
519impl Eq for Array {}
520
521impl<T> From<Vec<T>> for Any
522where
523    T: Anyable,
524{
525    fn from(value: Vec<T>) -> Self {
526        Any::new(Array(value.into_iter().map(|v| Any::new(v)).collect()))
527    }
528}
529
530impl From<Array> for Any {
531    fn from(array: Array) -> Self {
532        Any::new(array)
533    }
534}
535
536impl From<Vec<Any>> for Array {
537    fn from(vec: Vec<Any>) -> Self {
538        Self(vec)
539    }
540}
541
542impl Display for Array {
543    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
544        let mut result = String::from("[");
545        for (i, item) in self.0.iter().enumerate() {
546            if i > 0 {
547                result.push_str(", ");
548            }
549            result.push_str(&item.to_string());
550        }
551        result.push(']');
552        write!(f, "{}", result)
553    }
554}
555
556impl ToArray for Array {
557    fn to_array(&self) -> Array {
558        self.clone()
559    }
560
561    fn to_array_ref(&self) -> &Array {
562        self
563    }
564
565    fn to_array_mut(&mut self) -> &mut Array {
566        self
567    }
568}
569
570impl ToMap for Array {
571    fn to_map(&self) -> Map {
572        Map(HashMap::new())
573    }
574}
575
576impl ToBoolean for Array {
577    fn to_boolean(&self) -> bool {
578        true
579    }
580}
581
582impl ToInteger for Array {
583    fn to_integer(&self) -> i64 {
584        0
585    }
586}
587
588impl ToFloat for Array {
589    fn to_float(&self) -> f64 {
590        0.0
591    }
592}
593
594impl ToStr for Array {
595    fn to_str(&self) -> String {
596        let mut result = String::from("[");
597        for (i, item) in self.0.iter().enumerate() {
598            if i > 0 {
599                result.push_str(", ");
600            }
601            result.push_str(&item.to_string());
602        }
603        result.push(']');
604        result
605    }
606}
607
608impl ToPair for Array {
609    fn to_pair(&self) -> Pair {
610        let lhs = self.0.first().unwrap_or(&null).clone();
611        let rhs = self.0.get(1).unwrap_or(&null).clone();
612        Pair::new(lhs, rhs)
613    }
614}
615
616impl ToFunction for Array {}
617
618// Pair 트레잇 구현
619impl From<(Any, Any)> for Pair {
620    fn from(value: (Any, Any)) -> Self {
621        Pair(value)
622    }
623}
624
625impl From<Pair> for Any {
626    fn from(pair: Pair) -> Self {
627        Any::new(pair)
628    }
629}
630
631impl Display for Pair {
632    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
633        write!(f, "({}, {})", self.0 .0, self.0 .1)
634    }
635}
636
637impl ToInteger for Pair {
638    fn to_integer(&self) -> i64 {
639        0
640    }
641}
642
643impl ToStr for Pair {
644    fn to_str(&self) -> String {
645        format!("({}, {})", self.0 .0, self.0 .1)
646    }
647}
648
649impl ToFloat for Pair {
650    fn to_float(&self) -> f64 {
651        0.0
652    }
653}
654
655impl ToArray for Pair {
656    fn to_array(&self) -> Array {
657        vec![Any::new(self.clone())].into()
658    }
659}
660
661impl ToMap for Pair {
662    fn to_map(&self) -> Map {
663        Map(HashMap::new())
664    }
665}
666
667impl ToBoolean for Pair {
668    fn to_boolean(&self) -> bool {
669        true
670    }
671}
672
673impl ToPair for Pair {
674    fn to_pair(&self) -> Pair {
675        self.clone()
676    }
677}
678
679impl ToFunction for Pair {}
680
681// i8 트레잇 구현
682impl From<i8> for Any {
683    fn from(value: i8) -> Self {
684        Any::new(value)
685    }
686}
687
688impl ToInteger for i8 {
689    fn to_integer(&self) -> i64 {
690        *self as i64
691    }
692}
693
694impl ToStr for i8 {
695    fn to_str(&self) -> String {
696        self.to_string()
697    }
698}
699
700impl ToFloat for i8 {
701    fn to_float(&self) -> f64 {
702        *self as f64
703    }
704}
705
706impl ToArray for i8 {
707    fn to_array(&self) -> Array {
708        vec![Any::new(*self)].into()
709    }
710}
711
712impl ToMap for i8 {
713    fn to_map(&self) -> Map {
714        Map(HashMap::new())
715    }
716}
717
718impl ToBoolean for i8 {
719    fn to_boolean(&self) -> bool {
720        *self != 0
721    }
722}
723
724impl ToPair for i8 {}
725
726impl ToFunction for i8 {}
727// ---------------
728
729// i16 트레잇 구현
730impl From<i16> for Any {
731    fn from(value: i16) -> Self {
732        Any::new(value)
733    }
734}
735
736impl ToInteger for i16 {
737    fn to_integer(&self) -> i64 {
738        *self as i64
739    }
740}
741
742impl ToStr for i16 {
743    fn to_str(&self) -> String {
744        self.to_string()
745    }
746}
747
748impl ToFloat for i16 {
749    fn to_float(&self) -> f64 {
750        *self as f64
751    }
752}
753
754impl ToArray for i16 {
755    fn to_array(&self) -> Array {
756        vec![Any::new(*self)].into()
757    }
758}
759
760impl ToMap for i16 {
761    fn to_map(&self) -> Map {
762        Map(HashMap::new())
763    }
764}
765
766impl ToBoolean for i16 {
767    fn to_boolean(&self) -> bool {
768        *self != 0
769    }
770}
771
772impl ToPair for i16 {}
773
774impl ToFunction for i16 {}
775// ---------------
776
777// i32 트레잇 구현
778impl From<i32> for Any {
779    fn from(value: i32) -> Self {
780        Any::new(value)
781    }
782}
783
784impl ToInteger for i32 {
785    fn to_integer(&self) -> i64 {
786        *self as i64
787    }
788}
789
790impl ToStr for i32 {
791    fn to_str(&self) -> String {
792        self.to_string()
793    }
794}
795
796impl ToFloat for i32 {
797    fn to_float(&self) -> f64 {
798        *self as f64
799    }
800}
801
802impl ToArray for i32 {
803    fn to_array(&self) -> Array {
804        vec![Any::new(*self)].into()
805    }
806}
807
808impl ToMap for i32 {
809    fn to_map(&self) -> Map {
810        Map(HashMap::new())
811    }
812}
813
814impl ToBoolean for i32 {
815    fn to_boolean(&self) -> bool {
816        *self != 0
817    }
818}
819
820impl ToPair for i32 {}
821
822impl ToFunction for i32 {}
823// ---------------
824
825// i64 트레잇 구현
826impl From<i64> for Any {
827    fn from(value: i64) -> Self {
828        Any::new(value)
829    }
830}
831
832impl ToInteger for i64 {
833    fn to_integer(&self) -> i64 {
834        *self
835    }
836}
837
838impl ToStr for i64 {
839    fn to_str(&self) -> String {
840        self.to_string()
841    }
842}
843
844impl ToFloat for i64 {
845    fn to_float(&self) -> f64 {
846        *self as f64
847    }
848}
849
850impl ToArray for i64 {
851    fn to_array(&self) -> Array {
852        vec![Any::new(*self)].into()
853    }
854}
855
856impl ToMap for i64 {
857    fn to_map(&self) -> Map {
858        Map(HashMap::new())
859    }
860}
861
862impl ToBoolean for i64 {
863    fn to_boolean(&self) -> bool {
864        *self != 0
865    }
866}
867
868impl ToPair for i64 {}
869
870impl ToFunction for i64 {}
871// ---------------
872
873// isize 트레잇 구현
874impl From<isize> for Any {
875    fn from(value: isize) -> Self {
876        Any::new(value)
877    }
878}
879
880impl ToInteger for isize {
881    fn to_integer(&self) -> i64 {
882        *self as i64
883    }
884}
885
886impl ToStr for isize {
887    fn to_str(&self) -> String {
888        self.to_string()
889    }
890}
891
892impl ToFloat for isize {
893    fn to_float(&self) -> f64 {
894        *self as f64
895    }
896}
897
898impl ToArray for isize {
899    fn to_array(&self) -> Array {
900        vec![Any::new(*self)].into()
901    }
902}
903
904impl ToMap for isize {
905    fn to_map(&self) -> Map {
906        Map(HashMap::new())
907    }
908}
909
910impl ToBoolean for isize {
911    fn to_boolean(&self) -> bool {
912        *self != 0
913    }
914}
915
916impl ToPair for isize {}
917
918impl ToFunction for isize {}
919// ---------------
920
921// u8 트레잇 구현
922impl From<u8> for Any {
923    fn from(value: u8) -> Self {
924        Any::new(value)
925    }
926}
927
928impl ToInteger for u8 {
929    fn to_integer(&self) -> i64 {
930        *self as i64
931    }
932}
933
934impl ToStr for u8 {
935    fn to_str(&self) -> String {
936        self.to_string()
937    }
938}
939
940impl ToFloat for u8 {
941    fn to_float(&self) -> f64 {
942        *self as f64
943    }
944}
945
946impl ToArray for u8 {
947    fn to_array(&self) -> Array {
948        vec![Any::new(*self)].into()
949    }
950}
951
952impl ToMap for u8 {
953    fn to_map(&self) -> Map {
954        Map(HashMap::new())
955    }
956}
957
958impl ToBoolean for u8 {
959    fn to_boolean(&self) -> bool {
960        *self != 0
961    }
962}
963
964impl ToPair for u8 {}
965
966impl ToFunction for u8 {}
967// ---------------
968
969// u16 트레잇 구현
970impl From<u16> for Any {
971    fn from(value: u16) -> Self {
972        Any::new(value)
973    }
974}
975
976impl ToInteger for u16 {
977    fn to_integer(&self) -> i64 {
978        *self as i64
979    }
980}
981
982impl ToStr for u16 {
983    fn to_str(&self) -> String {
984        self.to_string()
985    }
986}
987
988impl ToFloat for u16 {
989    fn to_float(&self) -> f64 {
990        *self as f64
991    }
992}
993
994impl ToArray for u16 {
995    fn to_array(&self) -> Array {
996        vec![Any::new(*self)].into()
997    }
998}
999
1000impl ToMap for u16 {
1001    fn to_map(&self) -> Map {
1002        Map(HashMap::new())
1003    }
1004}
1005
1006impl ToBoolean for u16 {
1007    fn to_boolean(&self) -> bool {
1008        *self != 0
1009    }
1010}
1011
1012impl ToPair for u16 {}
1013
1014impl ToFunction for u16 {}
1015// ---------------
1016
1017// u32 트레잇 구현
1018impl From<u32> for Any {
1019    fn from(value: u32) -> Self {
1020        Any::new(value)
1021    }
1022}
1023
1024impl ToInteger for u32 {
1025    fn to_integer(&self) -> i64 {
1026        *self as i64
1027    }
1028}
1029
1030impl ToStr for u32 {
1031    fn to_str(&self) -> String {
1032        self.to_string()
1033    }
1034}
1035
1036impl ToFloat for u32 {
1037    fn to_float(&self) -> f64 {
1038        *self as f64
1039    }
1040}
1041
1042impl ToArray for u32 {
1043    fn to_array(&self) -> Array {
1044        vec![Any::new(*self)].into()
1045    }
1046}
1047
1048impl ToMap for u32 {
1049    fn to_map(&self) -> Map {
1050        Map(HashMap::new())
1051    }
1052}
1053
1054impl ToBoolean for u32 {
1055    fn to_boolean(&self) -> bool {
1056        *self != 0
1057    }
1058}
1059
1060impl ToPair for u32 {}
1061
1062impl ToFunction for u32 {}
1063// ---------------
1064
1065// u64 트레잇 구현
1066impl From<u64> for Any {
1067    fn from(value: u64) -> Self {
1068        Any::new(value)
1069    }
1070}
1071
1072impl ToInteger for u64 {
1073    fn to_integer(&self) -> i64 {
1074        *self as i64
1075    }
1076}
1077
1078impl ToStr for u64 {
1079    fn to_str(&self) -> String {
1080        self.to_string()
1081    }
1082}
1083
1084impl ToFloat for u64 {
1085    fn to_float(&self) -> f64 {
1086        *self as f64
1087    }
1088}
1089
1090impl ToArray for u64 {
1091    fn to_array(&self) -> Array {
1092        vec![Any::new(*self)].into()
1093    }
1094}
1095
1096impl ToMap for u64 {
1097    fn to_map(&self) -> Map {
1098        Map(HashMap::new())
1099    }
1100}
1101
1102impl ToBoolean for u64 {
1103    fn to_boolean(&self) -> bool {
1104        *self != 0
1105    }
1106}
1107
1108impl ToPair for u64 {}
1109
1110impl ToFunction for u64 {}
1111// ---------------
1112
1113// usize 트레잇 구현
1114impl From<usize> for Any {
1115    fn from(value: usize) -> Self {
1116        Any::new(value)
1117    }
1118}
1119
1120impl ToInteger for usize {
1121    fn to_integer(&self) -> i64 {
1122        *self as i64
1123    }
1124}
1125
1126impl ToStr for usize {
1127    fn to_str(&self) -> String {
1128        self.to_string()
1129    }
1130}
1131
1132impl ToFloat for usize {
1133    fn to_float(&self) -> f64 {
1134        *self as f64
1135    }
1136}
1137
1138impl ToArray for usize {
1139    fn to_array(&self) -> Array {
1140        vec![Any::new(*self)].into()
1141    }
1142}
1143
1144impl ToMap for usize {
1145    fn to_map(&self) -> Map {
1146        Map(HashMap::new())
1147    }
1148}
1149
1150impl ToBoolean for usize {
1151    fn to_boolean(&self) -> bool {
1152        *self != 0
1153    }
1154}
1155
1156impl ToPair for usize {}
1157
1158impl ToFunction for usize {}
1159// ---------------
1160
1161// f32 트레잇 구현
1162impl From<f32> for Any {
1163    fn from(value: f32) -> Self {
1164        Any::new(value)
1165    }
1166}
1167
1168impl ToInteger for f32 {
1169    fn to_integer(&self) -> i64 {
1170        *self as i64
1171    }
1172}
1173
1174impl ToStr for f32 {
1175    fn to_str(&self) -> String {
1176        self.to_string()
1177    }
1178}
1179
1180impl ToFloat for f32 {
1181    fn to_float(&self) -> f64 {
1182        *self as f64
1183    }
1184}
1185
1186impl ToArray for f32 {
1187    fn to_array(&self) -> Array {
1188        vec![Any::new(*self)].into()
1189    }
1190}
1191
1192impl ToMap for f32 {
1193    fn to_map(&self) -> Map {
1194        Map(HashMap::new())
1195    }
1196}
1197
1198impl ToBoolean for f32 {
1199    fn to_boolean(&self) -> bool {
1200        *self != 0.0
1201    }
1202}
1203
1204impl ToPair for f32 {}
1205
1206impl ToFunction for f32 {}
1207// ---------------
1208
1209// f64 트레잇 구현
1210impl From<f64> for Any {
1211    fn from(value: f64) -> Self {
1212        Any::new(value)
1213    }
1214}
1215
1216impl ToInteger for f64 {
1217    fn to_integer(&self) -> i64 {
1218        *self as i64
1219    }
1220}
1221
1222impl ToStr for f64 {
1223    fn to_str(&self) -> String {
1224        self.to_string()
1225    }
1226}
1227
1228impl ToFloat for f64 {
1229    fn to_float(&self) -> f64 {
1230        *self
1231    }
1232}
1233
1234impl ToArray for f64 {
1235    fn to_array(&self) -> Array {
1236        vec![Any::new(*self)].into()
1237    }
1238}
1239
1240impl ToMap for f64 {
1241    fn to_map(&self) -> Map {
1242        Map(HashMap::new())
1243    }
1244}
1245
1246impl ToBoolean for f64 {
1247    fn to_boolean(&self) -> bool {
1248        *self != 0.0
1249    }
1250}
1251
1252impl ToPair for f64 {}
1253
1254impl ToFunction for f64 {}
1255// ---------------
1256
1257// 문자 트레잇 구현
1258impl From<char> for Any {
1259    fn from(value: char) -> Self {
1260        Any::new(value.to_string())
1261    }
1262}
1263
1264// 문자열 트레잇 구현
1265impl From<String> for Any {
1266    fn from(value: String) -> Self {
1267        Any::new(value)
1268    }
1269}
1270
1271impl ToInteger for String {
1272    fn to_integer(&self) -> i64 {
1273        self.parse().unwrap()
1274    }
1275}
1276
1277impl ToStr for String {
1278    fn to_str(&self) -> String {
1279        self.clone()
1280    }
1281}
1282
1283impl ToFloat for String {
1284    fn to_float(&self) -> f64 {
1285        self.parse().unwrap()
1286    }
1287}
1288
1289impl ToArray for String {
1290    fn to_array(&self) -> Array {
1291        vec![Any::new(self.clone())].into()
1292    }
1293}
1294
1295impl ToMap for String {
1296    fn to_map(&self) -> Map {
1297        Map(HashMap::new())
1298    }
1299}
1300
1301impl ToBoolean for String {
1302    fn to_boolean(&self) -> bool {
1303        self.parse().unwrap_or(false)
1304    }
1305}
1306
1307impl ToPair for String {}
1308
1309impl ToFunction for String {}
1310// ---------------
1311
1312// 문자열 슬라이스 트레잇 구현
1313impl From<&str> for Any {
1314    fn from(value: &str) -> Self {
1315        Any::new(value.to_string())
1316    }
1317}
1318
1319impl ToInteger for &str {
1320    fn to_integer(&self) -> i64 {
1321        self.parse().unwrap()
1322    }
1323}
1324
1325impl ToStr for &str {
1326    fn to_str(&self) -> String {
1327        self.to_string()
1328    }
1329}
1330
1331impl ToFloat for &str {
1332    fn to_float(&self) -> f64 {
1333        self.parse().unwrap()
1334    }
1335}
1336
1337impl ToArray for &str {
1338    fn to_array(&self) -> Array {
1339        vec![Any::new(self.to_string())].into()
1340    }
1341}
1342
1343impl ToMap for &str {
1344    fn to_map(&self) -> Map {
1345        Map(HashMap::new())
1346    }
1347}
1348
1349impl ToBoolean for &str {
1350    fn to_boolean(&self) -> bool {
1351        self.parse().unwrap_or(false)
1352    }
1353}
1354
1355impl ToPair for &str {}
1356
1357impl ToFunction for &str {}
1358// ---------------
1359
1360// 불리언 트레잇 구현
1361impl From<bool> for Any {
1362    fn from(value: bool) -> Self {
1363        Any::new(value)
1364    }
1365}
1366
1367impl ToInteger for bool {
1368    fn to_integer(&self) -> i64 {
1369        if *self {
1370            1
1371        } else {
1372            0
1373        }
1374    }
1375}
1376
1377impl ToStr for bool {
1378    fn to_str(&self) -> String {
1379        self.to_string()
1380    }
1381}
1382
1383impl ToFloat for bool {
1384    fn to_float(&self) -> f64 {
1385        if *self {
1386            1.0
1387        } else {
1388            0.0
1389        }
1390    }
1391}
1392
1393impl ToArray for bool {
1394    fn to_array(&self) -> Array {
1395        vec![Any::new(*self)].into()
1396    }
1397}
1398
1399impl ToMap for bool {
1400    fn to_map(&self) -> Map {
1401        Map(HashMap::new())
1402    }
1403}
1404
1405impl ToBoolean for bool {
1406    fn to_boolean(&self) -> bool {
1407        *self
1408    }
1409}
1410
1411impl ToPair for bool {}
1412
1413impl ToFunction for bool {}
1414// ---------------
1415
1416// Map 트레잇 구현
1417impl PartialEq for Map {
1418    fn eq(&self, other: &Self) -> bool {
1419        self.0 == other.0
1420    }
1421}
1422
1423impl Eq for Map {}
1424
1425impl From<HashMap<Any, Any>> for Any {
1426    fn from(value: HashMap<Any, Any>) -> Self {
1427        Any::new(Map(value))
1428    }
1429}
1430
1431impl From<HashMap<Any, Any>> for Map {
1432    fn from(value: HashMap<Any, Any>) -> Self {
1433        Map(value)
1434    }
1435}
1436
1437impl From<Map> for Any {
1438    fn from(value: Map) -> Self {
1439        Any::new(value)
1440    }
1441}
1442
1443impl Display for Map {
1444    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1445        write!(f, "{}", self.to_str())
1446    }
1447}
1448
1449impl ToInteger for Map {
1450    fn to_integer(&self) -> i64 {
1451        0_i64
1452    }
1453}
1454
1455impl ToStr for Map {
1456    fn to_str(&self) -> String {
1457        let mut result = String::from("{");
1458
1459        for (i, (key, value)) in self.0.iter().enumerate() {
1460            if i > 0 {
1461                result.push_str(", ");
1462            }
1463
1464            result.push_str(&key.to_str());
1465            result.push_str(": ");
1466            result.push_str(&value.to_str());
1467        }
1468
1469        result.push('}');
1470
1471        result
1472    }
1473}
1474
1475impl ToFloat for Map {
1476    fn to_float(&self) -> f64 {
1477        0 as f64
1478    }
1479}
1480
1481impl ToArray for Map {
1482    fn to_array(&self) -> Array {
1483        vec![Any::new(self.clone())].into()
1484    }
1485}
1486
1487impl ToMap for Map {
1488    fn to_map(&self) -> Map {
1489        self.clone()
1490    }
1491
1492    fn to_map_ref(&self) -> &Map {
1493        self
1494    }
1495
1496    fn to_map_mut(&mut self) -> &mut Map {
1497        self
1498    }
1499}
1500
1501impl ToBoolean for Map {
1502    fn to_boolean(&self) -> bool {
1503        true
1504    }
1505}
1506
1507impl ToPair for Map {}
1508
1509impl ToFunction for Map {}
1510// ---------------
1511
1512// Null 트레잇 구현
1513impl From<Null> for Any {
1514    fn from(value: Null) -> Self {
1515        Any::new(value)
1516    }
1517}
1518
1519impl ToInteger for Null {
1520    fn to_integer(&self) -> i64 {
1521        0_i64
1522    }
1523}
1524
1525impl ToStr for Null {
1526    fn to_str(&self) -> String {
1527        String::from("null")
1528    }
1529}
1530
1531impl ToFloat for Null {
1532    fn to_float(&self) -> f64 {
1533        0 as f64
1534    }
1535}
1536
1537impl ToArray for Null {
1538    fn to_array(&self) -> Array {
1539        vec![].into()
1540    }
1541}
1542
1543impl ToMap for Null {
1544    fn to_map(&self) -> Map {
1545        Map(HashMap::new())
1546    }
1547}
1548
1549impl ToBoolean for Null {
1550    fn to_boolean(&self) -> bool {
1551        false
1552    }
1553}
1554
1555impl Display for Null {
1556    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1557        write!(f, "null")
1558    }
1559}
1560
1561impl ToPair for Null {}
1562
1563impl ToFunction for Null {}
1564// ---------------
1565
1566// Function 트레잇 구현
1567
1568impl From<Function> for Any {
1569    fn from(value: Function) -> Self {
1570        Any::new(value)
1571    }
1572}
1573
1574impl ToInteger for Function {
1575    fn to_integer(&self) -> i64 {
1576        0_i64
1577    }
1578}
1579
1580impl ToStr for Function {
1581    fn to_str(&self) -> String {
1582        String::from("function")
1583    }
1584}
1585
1586impl ToFloat for Function {
1587    fn to_float(&self) -> f64 {
1588        0 as f64
1589    }
1590}
1591
1592impl ToArray for Function {
1593    fn to_array(&self) -> Array {
1594        vec![].into()
1595    }
1596}
1597
1598impl ToMap for Function {
1599    fn to_map(&self) -> Map {
1600        Map(HashMap::new())
1601    }
1602}
1603
1604impl ToBoolean for Function {
1605    fn to_boolean(&self) -> bool {
1606        false
1607    }
1608}
1609
1610impl Display for Function {
1611    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1612        write!(f, "function")
1613    }
1614}
1615
1616impl ToPair for Function {}
1617
1618impl ToFunction for Function {
1619    fn to_function(&self) -> Function {
1620        self.clone()
1621    }
1622}
1623
1624/// type for all
1625///
1626/// You can use this to box and save almost any type.
1627/// It implements all the basic operators or behavior for basic collection types.
1628/// Because it operates based on dynamic types, you can observe humorous and witty phenomena.
1629/**
1630```
1631use anyrust::*;
1632
1633let a: Any = any(5);
1634let b = any("10");
1635let result = a + b;
1636
1637println!("result: {result}"); // result: 510
1638```
1639*/
1640#[derive(Debug)]
1641pub struct Any {
1642    type_id: std::any::TypeId,
1643    data: Box<dyn Anyable>,
1644}
1645
1646impl Clone for Any {
1647    fn clone(&self) -> Self {
1648        Self {
1649            type_id: self.type_id,
1650            data: dyn_clone::clone_box(&*self.data),
1651        }
1652    }
1653}
1654
1655/// Implements basic behavior common to all types.
1656impl Any {
1657    /// Create a new Any type.
1658    pub fn new<T>(value: T) -> Self
1659    where
1660        T: Anyable,
1661    {
1662        Self {
1663            type_id: std::any::TypeId::of::<T>(),
1664            data: Box::new(value),
1665        }
1666    }
1667
1668    /// Returns the TypeId of the stored value.
1669    pub fn type_id(&self) -> TypeId {
1670        self.type_id
1671    }
1672
1673    // pub fn downcast_ref<T: 'static>(&self) -> Option<&T> {
1674    //     if self.type_id == std::any::TypeId::of::<T>() {
1675    //         unsafe { Some(&*(self.data.as_ref() as *const dyn std::any::Any as *const T)) }
1676    //     } else {
1677    //         None
1678    //     }
1679    // }
1680
1681    // pub fn downcast_mut<T: 'static>(&mut self) -> Option<&mut T> {
1682    //     if self.type_id == std::any::TypeId::of::<T>() {
1683    //         unsafe { Some(&mut *(self.data.as_mut() as *mut dyn std::any::Any as *mut T)) }
1684    //     } else {
1685    //         None
1686    //     }
1687    // }
1688}
1689
1690/// Implements type checking for primitive types.
1691impl Any {
1692    /// Returns true if the type is an integer.
1693    pub fn is_integer(&self) -> bool {
1694        self.type_id == *I8
1695            || self.type_id == *I16
1696            || self.type_id == *I32
1697            || self.type_id == *I64
1698            || self.type_id == *U8
1699            || self.type_id == *U16
1700            || self.type_id == *U32
1701            || self.type_id == *U64
1702            || self.type_id == *ISIZE
1703            || self.type_id == *USIZE
1704    }
1705
1706    /// Returns true if the type is a float.
1707    pub fn is_float(&self) -> bool {
1708        self.type_id == *F32 || self.type_id == *F64
1709    }
1710
1711    /// Returns true if the type is a number.
1712    pub fn is_number(&self) -> bool {
1713        self.is_integer() || self.is_float()
1714    }
1715
1716    /// Returns true if the type is NaN.
1717    pub fn is_nan(&self) -> bool {
1718        self.is_float() && self.data.to_float().is_nan()
1719    }
1720
1721    /// Returns true if the type is a string.
1722    pub fn is_string(&self) -> bool {
1723        self.type_id == *STRING || self.type_id == *STR
1724    }
1725
1726    /// Returns true if the type is an array.
1727    pub fn is_array(&self) -> bool {
1728        self.type_id == *ARRAY
1729    }
1730
1731    /// Returns true if the type is a map.
1732    pub fn is_map(&self) -> bool {
1733        self.type_id == *MAP
1734    }
1735
1736    /// Returns true if the type is null.
1737    pub fn is_null(&self) -> bool {
1738        self.type_id == *NULL
1739    }
1740
1741    /// Returns true if the type is a boolean.
1742    pub fn is_boolean(&self) -> bool {
1743        self.type_id == *BOOL
1744    }
1745
1746    /// Returns true if the type is a function.
1747    pub fn is_function(&self) -> bool {
1748        self.type_id == *FUNCTION
1749    }
1750
1751    /// Returns true if the type is a pair.
1752    pub fn is_pair(&self) -> bool {
1753        self.type_id == *PAIR
1754    }
1755}
1756
1757/// Implements type conversion functionality for primitive types.
1758impl Any {
1759    /// Converts the type to an integer.
1760    pub fn to_integer(&self) -> i64 {
1761        self.data.to_integer()
1762    }
1763
1764    /// Converts the type to a float.
1765    pub fn to_float(&self) -> f64 {
1766        self.data.to_float()
1767    }
1768
1769    /// Converts the type to a string.
1770    pub fn to_str(&self) -> String {
1771        self.data.to_str()
1772    }
1773
1774    /// Converts the type to an array.
1775    pub fn to_array(&self) -> Array {
1776        self.data.to_array()
1777    }
1778
1779    /// Converts the type to a map.
1780    pub fn to_map(&self) -> Map {
1781        self.data.to_map()
1782    }
1783
1784    /// Converts the type to a boolean.
1785    pub fn to_boolean(&self) -> bool {
1786        self.data.to_boolean()
1787    }
1788
1789    /// Converts the type to a pair.
1790    pub fn to_pair(&self) -> Pair {
1791        self.data.to_pair()
1792    }
1793
1794    /// Converts the type to a function.
1795    pub fn to_function(&self) -> Function {
1796        self.data.to_function()
1797    }
1798}
1799
1800#[cfg(test)]
1801mod test_type_check_for_any {
1802    use super::*;
1803
1804    #[test]
1805    fn test_is_integer() {
1806        let a = Any::new(5_i64);
1807        assert!(a.is_integer());
1808
1809        let a = Any::new(5_u64);
1810        assert!(a.is_integer());
1811
1812        let a = Any::new(5_i32);
1813        assert!(a.is_integer());
1814
1815        let a = Any::new(5_u32);
1816        assert!(a.is_integer());
1817
1818        let a = Any::new(5_i16);
1819        assert!(a.is_integer());
1820
1821        let a = Any::new(5_u16);
1822        assert!(a.is_integer());
1823
1824        let a = Any::new(5_i8);
1825        assert!(a.is_integer());
1826
1827        let a = Any::new(5_u8);
1828        assert!(a.is_integer());
1829
1830        let a = Any::new(5_isize);
1831        assert!(a.is_integer());
1832
1833        let a = Any::new(5_usize);
1834        assert!(a.is_integer());
1835
1836        let a = Any::new(5.0);
1837        assert!(!a.is_integer());
1838
1839        let a = Any::new("5");
1840        assert!(!a.is_integer());
1841
1842        let a = Any::new("5.0");
1843        assert!(!a.is_integer());
1844
1845        let a = Any::new(true);
1846        assert!(!a.is_integer());
1847
1848        let a = Any::new(_null);
1849        assert!(!a.is_integer());
1850
1851        let a = Any::from(vec![1, 2, 3]);
1852        assert!(!a.is_integer());
1853
1854        let a = Any::from(HashMap::new());
1855        assert!(!a.is_integer());
1856    }
1857
1858    #[test]
1859    fn test_is_float() {
1860        let a = Any::new(5.0);
1861        assert!(a.is_float());
1862
1863        let a = Any::new(5.0_f32);
1864        assert!(a.is_float());
1865
1866        let a = Any::new(5.0_f64);
1867        assert!(a.is_float());
1868
1869        let a = Any::new(5);
1870        assert!(!a.is_float());
1871
1872        let a = Any::new("5");
1873        assert!(!a.is_float());
1874
1875        let a = Any::new("5.0");
1876        assert!(!a.is_float());
1877
1878        let a = Any::new(true);
1879        assert!(!a.is_float());
1880
1881        let a = Any::new(_null);
1882        assert!(!a.is_float());
1883
1884        let a = Any::from(vec![1, 2, 3]);
1885        assert!(!a.is_float());
1886
1887        let a = Any::from(HashMap::new());
1888        assert!(!a.is_float());
1889    }
1890
1891    #[test]
1892    fn test_is_number() {
1893        let a = Any::new(5.0);
1894        assert!(a.is_number());
1895
1896        let a = Any::new(5.0_f32);
1897        assert!(a.is_number());
1898
1899        let a = Any::new(5.0_f64);
1900        assert!(a.is_number());
1901
1902        let a = Any::new(5);
1903        assert!(a.is_number());
1904
1905        let a = Any::new("5");
1906        assert!(!a.is_number());
1907
1908        let a = Any::new("5.0");
1909        assert!(!a.is_number());
1910
1911        let a = Any::new(true);
1912        assert!(!a.is_number());
1913
1914        let a = Any::new(_null);
1915        assert!(!a.is_number());
1916
1917        let a = Any::from(vec![1, 2, 3]);
1918        assert!(!a.is_number());
1919
1920        let a = Any::from(HashMap::new());
1921        assert!(!a.is_number());
1922    }
1923
1924    #[test]
1925    fn test_is_nan() {
1926        let a = Any::new(f64::NAN);
1927        assert!(a.is_nan());
1928
1929        let a = Any::new(5.0);
1930        assert!(!a.is_nan());
1931
1932        let a = Any::new(5);
1933        assert!(!a.is_nan());
1934
1935        let a = Any::new("5");
1936        assert!(!a.is_nan());
1937
1938        let a = Any::new("5.0");
1939        assert!(!a.is_nan());
1940
1941        let a = Any::new(true);
1942        assert!(!a.is_nan());
1943
1944        let a = Any::new(_null);
1945        assert!(!a.is_nan());
1946
1947        let a = Any::from(vec![1, 2, 3]);
1948        assert!(!a.is_nan());
1949
1950        let a = Any::from(HashMap::new());
1951        assert!(!a.is_nan());
1952    }
1953
1954    #[test]
1955    fn test_is_string() {
1956        let a = Any::new("5");
1957        assert!(a.is_string());
1958
1959        let a = Any::new("5.0");
1960        assert!(a.is_string());
1961
1962        let a = Any::new(5.0);
1963        assert!(!a.is_string());
1964
1965        let a = Any::new(5);
1966        assert!(!a.is_string());
1967
1968        let a = Any::new(true);
1969        assert!(!a.is_string());
1970
1971        let a = Any::new(_null);
1972        assert!(!a.is_string());
1973
1974        let a = Any::from(vec![1, 2, 3]);
1975        assert!(!a.is_string());
1976
1977        let a = Any::from(HashMap::new());
1978        assert!(!a.is_string());
1979    }
1980
1981    #[test]
1982    fn test_is_array() {
1983        let a = Any::from(vec![1, 2, 3]);
1984        assert!(a.is_array());
1985
1986        let a = Any::from(HashMap::new());
1987        assert!(!a.is_array());
1988
1989        let a = Any::new("5");
1990        assert!(!a.is_array());
1991
1992        let a = Any::new("5.0");
1993        assert!(!a.is_array());
1994
1995        let a = Any::new(5.0);
1996        assert!(!a.is_array());
1997
1998        let a = Any::new(5);
1999        assert!(!a.is_array());
2000
2001        let a = Any::new(true);
2002        assert!(!a.is_array());
2003
2004        let a = Any::new(_null);
2005        assert!(!a.is_array());
2006    }
2007
2008    #[test]
2009    fn test_is_map() {
2010        let a = Any::from(HashMap::new());
2011        assert!(a.is_map());
2012
2013        let a = Any::from(vec![1, 2, 3]);
2014        assert!(!a.is_map());
2015
2016        let a = Any::new("5");
2017        assert!(!a.is_map());
2018
2019        let a = Any::new("5.0");
2020        assert!(!a.is_map());
2021
2022        let a = Any::new(5.0);
2023        assert!(!a.is_map());
2024
2025        let a = Any::new(5);
2026        assert!(!a.is_map());
2027
2028        let a = Any::new(true);
2029        assert!(!a.is_map());
2030
2031        let a = Any::new(_null);
2032        assert!(!a.is_map());
2033    }
2034
2035    #[test]
2036    fn test_is_null() {
2037        let a = Any::new(_null);
2038        assert!(a.is_null());
2039
2040        let a = Any::from(HashMap::new());
2041        assert!(!a.is_null());
2042
2043        let a = Any::from(vec![1, 2, 3]);
2044        assert!(!a.is_null());
2045
2046        let a = Any::new("5");
2047        assert!(!a.is_null());
2048
2049        let a = Any::new("5.0");
2050        assert!(!a.is_null());
2051
2052        let a = Any::new(5.0);
2053        assert!(!a.is_null());
2054
2055        let a = Any::new(5);
2056        assert!(!a.is_null());
2057
2058        let a = Any::new(true);
2059        assert!(!a.is_null());
2060    }
2061
2062    #[test]
2063    fn test_is_boolean() {
2064        let a = Any::new(true);
2065        assert!(a.is_boolean());
2066
2067        let a = Any::new(false);
2068        assert!(a.is_boolean());
2069
2070        let a = Any::new(5.0);
2071        assert!(!a.is_boolean());
2072
2073        let a = Any::new(5);
2074        assert!(!a.is_boolean());
2075
2076        let a = Any::new("5");
2077        assert!(!a.is_boolean());
2078
2079        let a = Any::new("5.0");
2080        assert!(!a.is_boolean());
2081
2082        let a = Any::new(_null);
2083        assert!(!a.is_boolean());
2084
2085        let a = Any::from(vec![1, 2, 3]);
2086        assert!(!a.is_boolean());
2087
2088        let a = Any::from(HashMap::new());
2089        assert!(!a.is_boolean());
2090    }
2091}
2092
2093/// Implements basic behavior for Array objects.
2094impl Any {
2095    /// Adds a value to the end of the array.
2096    pub fn push(&mut self, value: impl Into<Any>) {
2097        if self.is_array() {
2098            self.data.to_array_mut().push(value.into())
2099        }
2100    }
2101
2102    /// Removes the last element from the array and returns it.
2103    pub fn pop(&mut self) -> Option<Any> {
2104        if self.is_array() {
2105            self.data.to_array_mut().pop()
2106        } else {
2107            None
2108        }
2109    }
2110
2111    /// Adds a value to the beginning of the array.
2112    pub fn unshift(&mut self, value: impl Into<Any>) {
2113        if self.is_array() {
2114            self.data.to_array_mut().unshift(value.into())
2115        }
2116    }
2117
2118    /// Removes the first element from the array and returns it.
2119    pub fn shift(&mut self) -> Option<Any> {
2120        if self.is_array() {
2121            self.data.to_array_mut().shift()
2122        } else {
2123            None
2124        }
2125    }
2126
2127    /// Reverses the array.
2128    pub fn reverse(&mut self) -> Any {
2129        if self.is_array() {
2130            self.data.to_array_mut().reverse().clone().into()
2131        } else {
2132            Any::from(_null)
2133        }
2134    }
2135}
2136
2137/// Implements basic behavior for Map objects.
2138impl Any {
2139    /// Sets a key-value pair in the map.
2140    pub fn set(&mut self, key: impl Into<Any>, value: impl Into<Any>) {
2141        if self.is_map() {
2142            self.data.to_map_mut().0.insert(key.into(), value.into());
2143        }
2144    }
2145
2146    /// Gets a value from the map.
2147    pub fn get(&self, key: impl Into<Any>) -> Any {
2148        if self.is_map() {
2149            self.data
2150                .to_map()
2151                .0
2152                .get(&key.into())
2153                .cloned()
2154                .unwrap_or_else(|| Any::from(_null))
2155        } else {
2156            Any::from(_null)
2157        }
2158    }
2159
2160    /// Deletes a key-value pair from the map.
2161    pub fn delete(&mut self, key: impl Into<Any>) -> Any {
2162        if self.is_map() {
2163            self.data
2164                .to_map_mut()
2165                .0
2166                .remove(&key.into())
2167                .unwrap_or_else(|| Any::from(_null))
2168        } else {
2169            Any::from(_null)
2170        }
2171    }
2172}
2173
2174/// Implements basic behavior for Collection objects.
2175impl Any {
2176    /// Returns the length of the collection.
2177    pub fn length(&self) -> Any {
2178        if self.is_array() {
2179            self.data.to_array().length().into()
2180        } else if self.is_map() {
2181            self.data.to_map().length().into()
2182        } else if self.is_string() {
2183            self.data.to_string().len().into()
2184        } else {
2185            Any::from(_null)
2186        }
2187    }
2188
2189    /// Returns true if the collection is empty.
2190    pub fn is_empty(&self) -> Any {
2191        if self.is_array() {
2192            self.data.to_array().is_empty().into()
2193        } else if self.is_map() {
2194            self.data.to_map().is_empty().into()
2195        } else if self.is_string() {
2196            self.data.to_string().is_empty().into()
2197        } else {
2198            Any::from(_null)
2199        }
2200    }
2201}
2202
2203/// function operations
2204impl Any {
2205    /// Calls the function with the specified arguments.
2206    ///
2207    /// The first argument must be an Array object value.
2208    pub fn call(&self, args: Any) -> Any {
2209        if self.is_function() {
2210            self.data.to_function().call(args)
2211        } else {
2212            Any::from(_null)
2213        }
2214    }
2215}
2216
2217lazy_static::lazy_static! {
2218    pub(crate) static ref I8: TypeId = TypeId::of::<i8>();
2219    pub(crate) static ref I16: TypeId = TypeId::of::<i16>();
2220    pub(crate) static ref I32: TypeId = TypeId::of::<i32>();
2221    pub(crate) static ref I64: TypeId = TypeId::of::<i64>();
2222    pub(crate) static ref ISIZE: TypeId = TypeId::of::<isize>();
2223    pub(crate) static ref U8: TypeId = TypeId::of::<u8>();
2224    pub(crate) static ref U16: TypeId = TypeId::of::<u16>();
2225    pub(crate) static ref U32: TypeId = TypeId::of::<u32>();
2226    pub(crate) static ref U64: TypeId = TypeId::of::<u64>();
2227    pub(crate) static ref USIZE: TypeId = TypeId::of::<usize>();
2228    pub(crate) static ref F32: TypeId = TypeId::of::<f32>();
2229    pub(crate) static ref F64: TypeId = TypeId::of::<f64>();
2230    pub(crate) static ref STRING: TypeId = TypeId::of::<String>();
2231    pub(crate) static ref STR: TypeId = TypeId::of::<&str>();
2232    pub(crate) static ref BOOL: TypeId = TypeId::of::<bool>();
2233    pub(crate) static ref ARRAY: TypeId = TypeId::of::<Array>();
2234    pub(crate) static ref MAP: TypeId = TypeId::of::<Map>();
2235    pub(crate) static ref NULL: TypeId = TypeId::of::<Null>();
2236    pub(crate) static ref FUNCTION: TypeId = TypeId::of::<Function>();
2237    pub(crate) static ref PAIR: TypeId = TypeId::of::<Pair>();
2238
2239    /// value of Null type
2240    pub static ref null: Any = Any::new(_null);
2241    static ref EMPTY_ARRAY: Array = Array(vec![]);
2242    static ref EMPTY_MAP: Map = Map(HashMap::new());
2243}
2244
2245impl Add for Any {
2246    type Output = Self;
2247
2248    fn add(self, other: Self) -> Self {
2249        if self.type_id == *NULL || other.type_id == *NULL {
2250            Any::new(_null)
2251        } else if self.type_id == other.type_id {
2252            match self.type_id {
2253                type_id if type_id == *I8 => {
2254                    let a = self.data.to_integer();
2255                    let b = other.data.to_integer();
2256                    Any::new(a + b)
2257                }
2258                type_id if type_id == *I16 => {
2259                    let a = self.data.to_integer();
2260                    let b = other.data.to_integer();
2261                    Any::new(a + b)
2262                }
2263                type_id if type_id == *I32 => {
2264                    let a = self.data.to_integer();
2265                    let b = other.data.to_integer();
2266                    Any::new(a + b)
2267                }
2268                type_id if type_id == *I64 => {
2269                    let a = self.data.to_integer();
2270                    let b = other.data.to_integer();
2271                    Any::new(a + b)
2272                }
2273                type_id if type_id == *ISIZE => {
2274                    let a = self.data.to_integer();
2275                    let b = other.data.to_integer();
2276                    Any::new(a + b)
2277                }
2278                type_id if type_id == *U8 => {
2279                    let a = self.data.to_integer();
2280                    let b = other.data.to_integer();
2281                    Any::new(a + b)
2282                }
2283                type_id if type_id == *U16 => {
2284                    let a = self.data.to_integer();
2285                    let b = other.data.to_integer();
2286                    Any::new(a + b)
2287                }
2288                type_id if type_id == *U32 => {
2289                    let a = self.data.to_integer();
2290                    let b = other.data.to_integer();
2291                    Any::new(a + b)
2292                }
2293                type_id if type_id == *U64 => {
2294                    let a = self.data.to_integer();
2295                    let b = other.data.to_integer();
2296                    Any::new(a + b)
2297                }
2298                type_id if type_id == *USIZE => {
2299                    let a = self.data.to_integer();
2300                    let b = other.data.to_integer();
2301                    Any::new(a + b)
2302                }
2303                type_id if type_id == *F32 => {
2304                    let a = self.data.to_float();
2305                    let b = other.data.to_float();
2306                    Any::new(a + b)
2307                }
2308                type_id if type_id == *F64 => {
2309                    let a = self.data.to_float();
2310                    let b = other.data.to_float();
2311                    Any::new(a + b)
2312                }
2313                type_id if type_id == *STRING => {
2314                    let mut a = self.data.to_string();
2315                    let b = other.data.to_string();
2316                    a.push_str(b.as_str());
2317                    Any::new(a)
2318                }
2319                type_id if type_id == *STR => {
2320                    let mut a = self.data.to_string();
2321                    let b = other.data.to_string();
2322                    a.push_str(b.as_str());
2323                    Any::new(a)
2324                }
2325                type_id if type_id == *BOOL => {
2326                    let a = self.data.to_boolean();
2327                    let b = other.data.to_boolean();
2328                    Any::new(a || b)
2329                }
2330                // TODO: ARRAY, MAP
2331                type_id if type_id == *ARRAY => {
2332                    let a = self.data.to_array();
2333                    let b = other.data.to_array();
2334                    let mut result = a.clone();
2335                    result.0.extend(b.0.clone());
2336                    Any::new(result)
2337                }
2338                _ => {
2339                    let a = self.data.to_string();
2340                    let b = other.data.to_string();
2341                    Any::new(a + &b)
2342                }
2343            }
2344        } else if self.type_id == *STRING || other.type_id == *STRING {
2345            let a = self.data.to_string();
2346            let b = other.data.to_string();
2347            Any::new(a + &b)
2348        } else if self.type_id == *STR || other.type_id == *STR {
2349            let a = self.data.to_string();
2350            let b = other.data.to_string();
2351            Any::new(a + &b)
2352        } else if self.type_id == *F64 || other.type_id == *F64 {
2353            let a: f64 = self.data.to_float();
2354            let b = other.data.to_float();
2355            Any::new(a + b)
2356        } else if self.type_id == *F32 || other.type_id == *F32 {
2357            let a: f64 = self.data.to_float();
2358            let b = other.data.to_float();
2359            Any::new(a + b)
2360        } else if self.type_id == *I64 || other.type_id == *I64 {
2361            let a = self.data.to_integer();
2362            let b = other.data.to_integer();
2363            Any::new(a + b)
2364        } else if self.type_id == *I32 || other.type_id == *I32 {
2365            let a = self.data.to_integer();
2366            let b = other.data.to_integer();
2367            Any::new(a + b)
2368        } else if self.type_id == *I16 || other.type_id == *I16 {
2369            let a = self.data.to_integer();
2370            let b = other.data.to_integer();
2371            Any::new(a + b)
2372        } else if self.type_id == *I8 || other.type_id == *I8 {
2373            let a = self.data.to_integer();
2374            let b = other.data.to_integer();
2375            let result = a + b;
2376            Any::new(result)
2377        } else if self.type_id == *U64 || other.type_id == *U64 {
2378            let a = self.data.to_integer();
2379            let b = other.data.to_integer();
2380            Any::new(a + b)
2381        } else if self.type_id == *U32 || other.type_id == *U32 {
2382            let a = self.data.to_integer();
2383            let b = other.data.to_integer();
2384            Any::new(a + b)
2385        } else if self.type_id == *U16 || other.type_id == *U16 {
2386            let a = self.data.to_integer();
2387            let b = other.data.to_integer();
2388            Any::new(a + b)
2389        } else if self.type_id == *U8 || other.type_id == *U8 {
2390            let a = self.data.to_integer();
2391            let b = other.data.to_integer();
2392            Any::new(a + b)
2393        } else {
2394            let a = self.data.to_string();
2395            let b = other.data.to_string();
2396            Any::new(a + &b)
2397        }
2398    }
2399}
2400
2401impl AddAssign for Any {
2402    fn add_assign(&mut self, other: Self) {
2403        *self = self.clone() + other;
2404    }
2405}
2406
2407#[cfg(test)]
2408mod test_add_for_any {
2409    use super::*;
2410
2411    #[test]
2412    fn test_add() {
2413        struct TestCase {
2414            name: String,
2415            a: Any,
2416            b: Any,
2417            result: Any,
2418        }
2419
2420        let test_cases = vec![
2421            TestCase {
2422                name: "i64".to_string(),
2423                a: Any::new(5_i64),
2424                b: Any::new(10_i64),
2425                result: Any::new(15_i64),
2426            },
2427            TestCase {
2428                name: "u64".to_string(),
2429                a: Any::new(5_u64),
2430                b: Any::new(10_u64),
2431                result: Any::new(15_i64),
2432            },
2433            TestCase {
2434                name: "f32".to_string(),
2435                a: Any::new(5.0_f32),
2436                b: Any::new(10.0_f32),
2437                result: Any::new(15.0_f64),
2438            },
2439            TestCase {
2440                name: "f64".to_string(),
2441                a: Any::new(5.0),
2442                b: Any::new(10.0),
2443                result: Any::new(15.0),
2444            },
2445            TestCase {
2446                name: "string".to_string(),
2447                a: Any::new("5".to_string()),
2448                b: Any::new("10".to_string()),
2449                result: Any::new("510".to_string()),
2450            },
2451            TestCase {
2452                name: "str".to_string(),
2453                a: Any::new("5"),
2454                b: Any::new("10"),
2455                result: Any::new("510".to_string()),
2456            },
2457        ];
2458
2459        for test_case in test_cases {
2460            let result = test_case.a + test_case.b;
2461            assert_eq!(result, test_case.result, "TC: {}", test_case.name);
2462        }
2463    }
2464
2465    #[test]
2466    fn test_add_assign() {
2467        let mut a = Any::new(5_i64);
2468        let b = Any::new(10_i64);
2469        a += b;
2470        assert_eq!(a, Any::new(15_i64));
2471    }
2472}
2473
2474impl Sub for Any {
2475    type Output = Self;
2476
2477    fn sub(self, other: Self) -> Self {
2478        if self.type_id == *NULL || other.type_id == *NULL {
2479            Any::new(_null)
2480        } else if self.type_id == other.type_id {
2481            match self.type_id {
2482                type_id if type_id == *I8 => {
2483                    let a = self.data.to_integer();
2484                    let b = other.data.to_integer();
2485                    Any::new(a - b)
2486                }
2487                type_id if type_id == *I16 => {
2488                    let a = self.data.to_integer();
2489                    let b = other.data.to_integer();
2490                    Any::new(a - b)
2491                }
2492                type_id if type_id == *I32 => {
2493                    let a = self.data.to_integer();
2494                    let b = other.data.to_integer();
2495                    Any::new(a - b)
2496                }
2497                type_id if type_id == *I64 => {
2498                    let a = self.data.to_integer();
2499                    let b = other.data.to_integer();
2500                    Any::new(a - b)
2501                }
2502                type_id if type_id == *ISIZE => {
2503                    let a = self.data.to_integer();
2504                    let b = other.data.to_integer();
2505                    Any::new(a - b)
2506                }
2507                type_id if type_id == *U8 => {
2508                    let a = self.data.to_integer();
2509                    let b = other.data.to_integer();
2510                    Any::new(a - b)
2511                }
2512                type_id if type_id == *U16 => {
2513                    let a = self.data.to_integer();
2514                    let b = other.data.to_integer();
2515                    Any::new(a - b)
2516                }
2517                type_id if type_id == *U32 => {
2518                    let a = self.data.to_integer();
2519                    let b = other.data.to_integer();
2520                    Any::new(a - b)
2521                }
2522                type_id if type_id == *U64 => {
2523                    let a = self.data.to_integer();
2524                    let b = other.data.to_integer();
2525                    Any::new(a - b)
2526                }
2527                type_id if type_id == *USIZE => {
2528                    let a = self.data.to_integer();
2529                    let b = other.data.to_integer();
2530                    Any::new(a - b)
2531                }
2532                type_id if type_id == *F32 => {
2533                    let a = self.data.to_float();
2534                    let b = other.data.to_float();
2535                    Any::new(a - b)
2536                }
2537                type_id if type_id == *F64 => {
2538                    let a = self.data.to_float();
2539                    let b = other.data.to_float();
2540                    Any::new(a - b)
2541                }
2542                _ => Any::new(f64::NAN),
2543            }
2544        } else if self.type_id == *STRING || other.type_id == *STRING {
2545            Any::new(f64::NAN)
2546        } else if self.type_id == *STR || other.type_id == *STR {
2547            Any::new(f64::NAN)
2548        } else if self.type_id == *F64 || other.type_id == *F64 {
2549            let a: f64 = self.data.to_float();
2550            let b = other.data.to_float();
2551            Any::new(a - b)
2552        } else if self.type_id == *F32 || other.type_id == *F32 {
2553            let a: f64 = self.data.to_float();
2554            let b = other.data.to_float();
2555            Any::new(a - b)
2556        } else if self.type_id == *I64 || other.type_id == *I64 {
2557            let a = self.data.to_integer();
2558            let b = other.data.to_integer();
2559            Any::new(a - b)
2560        } else if self.type_id == *I32 || other.type_id == *I32 {
2561            let a = self.data.to_integer();
2562            let b = other.data.to_integer();
2563            Any::new(a - b)
2564        } else if self.type_id == *I16 || other.type_id == *I16 {
2565            let a = self.data.to_integer();
2566            let b = other.data.to_integer();
2567            Any::new(a - b)
2568        } else if self.type_id == *I8 || other.type_id == *I8 {
2569            let a = self.data.to_integer();
2570            let b = other.data.to_integer();
2571            let result = a + b;
2572            Any::new(result)
2573        } else if self.type_id == *U64 || other.type_id == *U64 {
2574            let a = self.data.to_integer();
2575            let b = other.data.to_integer();
2576            Any::new(a - b)
2577        } else if self.type_id == *U32 || other.type_id == *U32 {
2578            let a = self.data.to_integer();
2579            let b = other.data.to_integer();
2580            Any::new(a - b)
2581        } else if self.type_id == *U16 || other.type_id == *U16 {
2582            let a = self.data.to_integer();
2583            let b = other.data.to_integer();
2584            Any::new(a - b)
2585        } else if self.type_id == *U8 || other.type_id == *U8 {
2586            let a = self.data.to_integer();
2587            let b = other.data.to_integer();
2588            Any::new(a - b)
2589        } else {
2590            Any::new(f64::NAN)
2591        }
2592    }
2593}
2594
2595impl SubAssign for Any {
2596    fn sub_assign(&mut self, other: Self) {
2597        *self = self.clone() - other;
2598    }
2599}
2600
2601#[cfg(test)]
2602mod test_sub_for_any {
2603    use super::*;
2604
2605    #[test]
2606    fn test_sub() {
2607        struct TestCase {
2608            name: String,
2609            a: Any,
2610            b: Any,
2611            result: Any,
2612        }
2613
2614        let test_cases = vec![
2615            TestCase {
2616                name: "i64".to_string(),
2617                a: Any::new(5_i64),
2618                b: Any::new(10_i64),
2619                result: Any::new(-5_i64),
2620            },
2621            TestCase {
2622                name: "u64".to_string(),
2623                a: Any::new(5_u64),
2624                b: Any::new(10_u64),
2625                result: Any::new(-5_i64),
2626            },
2627            TestCase {
2628                name: "f32".to_string(),
2629                a: Any::new(5.0_f32),
2630                b: Any::new(10.0_f32),
2631                result: Any::new(-5.0_f64),
2632            },
2633            TestCase {
2634                name: "f64".to_string(),
2635                a: Any::new(5.0),
2636                b: Any::new(10.0),
2637                result: Any::new(-5.0),
2638            },
2639        ];
2640
2641        for test_case in test_cases {
2642            let result = test_case.a - test_case.b;
2643            assert_eq!(result, test_case.result, "TC: {}", test_case.name);
2644        }
2645    }
2646
2647    #[test]
2648    fn test_sub_assign() {
2649        let mut a = Any::new(5_i64);
2650        let b = Any::new(10_i64);
2651        a -= b;
2652        assert_eq!(a, Any::new(-5_i64));
2653    }
2654}
2655
2656impl Neg for Any {
2657    type Output = Self;
2658
2659    fn neg(self) -> Self {
2660        if self.type_id == *I8 {
2661            let a = self.data.to_integer();
2662            Any::new(-a)
2663        } else if self.type_id == *I16 {
2664            let a = self.data.to_integer();
2665            Any::new(-a)
2666        } else if self.type_id == *I32 {
2667            let a = self.data.to_integer();
2668            Any::new(-a)
2669        } else if self.type_id == *I64 {
2670            let a = self.data.to_integer();
2671            Any::new(-a)
2672        } else if self.type_id == *ISIZE {
2673            let a = self.data.to_integer();
2674            Any::new(-a)
2675        } else if self.type_id == *U8 {
2676            let a = self.data.to_integer();
2677            Any::new(-a)
2678        } else if self.type_id == *U16 {
2679            let a = self.data.to_integer();
2680            Any::new(-a)
2681        } else if self.type_id == *U32 {
2682            let a = self.data.to_integer();
2683            Any::new(-a)
2684        } else if self.type_id == *U64 {
2685            let a = self.data.to_integer();
2686            Any::new(-a)
2687        } else if self.type_id == *USIZE {
2688            let a = self.data.to_integer();
2689            Any::new(-a)
2690        } else if self.type_id == *F32 {
2691            let a = self.data.to_float();
2692            Any::new(-a)
2693        } else if self.type_id == *F64 {
2694            let a = self.data.to_float();
2695            Any::new(-a)
2696        } else {
2697            Any::new(f64::NAN)
2698        }
2699    }
2700}
2701
2702#[cfg(test)]
2703mod test_neg_for_any {
2704    use super::*;
2705
2706    #[test]
2707    fn test_neg() {
2708        struct TestCase {
2709            name: String,
2710            a: Any,
2711            result: Any,
2712        }
2713
2714        let test_cases = vec![
2715            TestCase {
2716                name: "i64".to_string(),
2717                a: Any::new(5_i64),
2718                result: Any::new(-5_i64),
2719            },
2720            TestCase {
2721                name: "u64".to_string(),
2722                a: Any::new(5_u64),
2723                result: Any::new(-5_i64),
2724            },
2725            TestCase {
2726                name: "f32".to_string(),
2727                a: Any::new(5.0_f32),
2728                result: Any::new(-5.0_f64),
2729            },
2730            TestCase {
2731                name: "f64".to_string(),
2732                a: Any::new(5.0),
2733                result: Any::new(-5.0),
2734            },
2735        ];
2736
2737        for test_case in test_cases {
2738            let result = -test_case.a;
2739            assert_eq!(result, test_case.result, "TC: {}", test_case.name);
2740        }
2741    }
2742}
2743
2744impl Mul for Any {
2745    type Output = Self;
2746
2747    fn mul(self, other: Self) -> Self {
2748        if self.type_id == *NULL || other.type_id == *NULL {
2749            Any::new(_null)
2750        } else if self.type_id == other.type_id {
2751            match self.type_id {
2752                type_id if type_id == *I8 => {
2753                    let a = self.data.to_integer();
2754                    let b = other.data.to_integer();
2755                    Any::new(a * b)
2756                }
2757                type_id if type_id == *I16 => {
2758                    let a = self.data.to_integer();
2759                    let b = other.data.to_integer();
2760                    Any::new(a * b)
2761                }
2762                type_id if type_id == *I32 => {
2763                    let a = self.data.to_integer();
2764                    let b = other.data.to_integer();
2765                    Any::new(a * b)
2766                }
2767                type_id if type_id == *I64 => {
2768                    let a = self.data.to_integer();
2769                    let b = other.data.to_integer();
2770                    Any::new(a * b)
2771                }
2772                type_id if type_id == *ISIZE => {
2773                    let a = self.data.to_integer();
2774                    let b = other.data.to_integer();
2775                    Any::new(a * b)
2776                }
2777                type_id if type_id == *U8 => {
2778                    let a = self.data.to_integer();
2779                    let b = other.data.to_integer();
2780                    Any::new(a * b)
2781                }
2782                type_id if type_id == *U16 => {
2783                    let a = self.data.to_integer();
2784                    let b = other.data.to_integer();
2785                    Any::new(a * b)
2786                }
2787                type_id if type_id == *U32 => {
2788                    let a = self.data.to_integer();
2789                    let b = other.data.to_integer();
2790                    Any::new(a * b)
2791                }
2792                type_id if type_id == *U64 => {
2793                    let a = self.data.to_integer();
2794                    let b = other.data.to_integer();
2795                    Any::new(a * b)
2796                }
2797                type_id if type_id == *USIZE => {
2798                    let a = self.data.to_integer();
2799                    let b = other.data.to_integer();
2800                    Any::new(a * b)
2801                }
2802                type_id if type_id == *F32 => {
2803                    let a = self.data.to_float();
2804                    let b = other.data.to_float();
2805                    Any::new(a * b)
2806                }
2807                type_id if type_id == *F64 => {
2808                    let a = self.data.to_float();
2809                    let b = other.data.to_float();
2810                    Any::new(a * b)
2811                }
2812                _ => Any::new(f64::NAN),
2813            }
2814        } else if self.type_id == *STRING || other.type_id == *STRING {
2815            Any::new(f64::NAN)
2816        } else if self.type_id == *STR || other.type_id == *STR {
2817            Any::new(f64::NAN)
2818        } else if self.type_id == *F64 || other.type_id == *F64 {
2819            let a: f64 = self.data.to_float();
2820            let b = other.data.to_float();
2821            Any::new(a * b)
2822        } else if self.type_id == *F32 || other.type_id == *F32 {
2823            let a: f64 = self.data.to_float();
2824            let b = other.data.to_float();
2825            Any::new(a * b)
2826        } else if self.type_id == *I64 || other.type_id == *I64 {
2827            let a = self.data.to_integer();
2828            let b = other.data.to_integer();
2829            Any::new(a * b)
2830        } else if self.type_id == *I32 || other.type_id == *I32 {
2831            let a = self.data.to_integer();
2832            let b = other.data.to_integer();
2833            Any::new(a * b)
2834        } else if self.type_id == *I16 || other.type_id == *I16 {
2835            let a = self.data.to_integer();
2836            let b = other.data.to_integer();
2837            Any::new(a * b)
2838        } else if self.type_id == *I8 || other.type_id == *I8 {
2839            let a = self.data.to_integer();
2840            let b = other.data.to_integer();
2841            let result = a + b;
2842            Any::new(result)
2843        } else if self.type_id == *U64 || other.type_id == *U64 {
2844            let a = self.data.to_integer();
2845            let b = other.data.to_integer();
2846            Any::new(a * b)
2847        } else if self.type_id == *U32 || other.type_id == *U32 {
2848            let a = self.data.to_integer();
2849            let b = other.data.to_integer();
2850            Any::new(a * b)
2851        } else if self.type_id == *U16 || other.type_id == *U16 {
2852            let a = self.data.to_integer();
2853            let b = other.data.to_integer();
2854            Any::new(a * b)
2855        } else if self.type_id == *U8 || other.type_id == *U8 {
2856            let a = self.data.to_integer();
2857            let b = other.data.to_integer();
2858            Any::new(a * b)
2859        } else {
2860            Any::new(f64::NAN)
2861        }
2862    }
2863}
2864
2865impl MulAssign for Any {
2866    fn mul_assign(&mut self, other: Self) {
2867        *self = self.clone() * other;
2868    }
2869}
2870
2871#[cfg(test)]
2872mod test_mul_for_any {
2873    use super::*;
2874
2875    #[test]
2876    fn test_mul() {
2877        struct TestCase {
2878            name: String,
2879            a: Any,
2880            b: Any,
2881            result: Any,
2882        }
2883
2884        let test_cases = vec![
2885            TestCase {
2886                name: "i64".to_string(),
2887                a: Any::new(5_i64),
2888                b: Any::new(10_i64),
2889                result: Any::new(50_i64),
2890            },
2891            TestCase {
2892                name: "u64".to_string(),
2893                a: Any::new(5_u64),
2894                b: Any::new(10_u64),
2895                result: Any::new(50_i64),
2896            },
2897            TestCase {
2898                name: "f32".to_string(),
2899                a: Any::new(5.0_f32),
2900                b: Any::new(10.0_f32),
2901                result: Any::new(50.0_f64),
2902            },
2903            TestCase {
2904                name: "f64".to_string(),
2905                a: Any::new(5.0),
2906                b: Any::new(10.0),
2907                result: Any::new(50.0),
2908            },
2909        ];
2910
2911        for test_case in test_cases {
2912            let result = test_case.a * test_case.b;
2913            assert_eq!(result, test_case.result, "TC: {}", test_case.name);
2914        }
2915    }
2916
2917    #[test]
2918    fn test_mul_assign() {
2919        let mut a = Any::new(5_i64);
2920        let b = Any::new(10_i64);
2921        a *= b;
2922        assert_eq!(a, Any::new(50_i64));
2923    }
2924}
2925
2926impl Div for Any {
2927    type Output = Self;
2928
2929    fn div(self, other: Self) -> Self {
2930        if self.type_id == *NULL || other.type_id == *NULL {
2931            Any::new(_null)
2932        } else if self.type_id == other.type_id {
2933            match self.type_id {
2934                type_id if type_id == *I8 => {
2935                    let a = self.data.to_integer();
2936                    let b = other.data.to_integer();
2937                    Any::new(a / b)
2938                }
2939                type_id if type_id == *I16 => {
2940                    let a = self.data.to_integer();
2941                    let b = other.data.to_integer();
2942                    Any::new(a / b)
2943                }
2944                type_id if type_id == *I32 => {
2945                    let a = self.data.to_integer();
2946                    let b = other.data.to_integer();
2947                    Any::new(a / b)
2948                }
2949                type_id if type_id == *I64 => {
2950                    let a = self.data.to_integer();
2951                    let b = other.data.to_integer();
2952                    Any::new(a / b)
2953                }
2954                type_id if type_id == *ISIZE => {
2955                    let a = self.data.to_integer();
2956                    let b = other.data.to_integer();
2957                    Any::new(a / b)
2958                }
2959                type_id if type_id == *U8 => {
2960                    let a = self.data.to_integer();
2961                    let b = other.data.to_integer();
2962                    Any::new(a / b)
2963                }
2964                type_id if type_id == *U16 => {
2965                    let a = self.data.to_integer();
2966                    let b = other.data.to_integer();
2967                    Any::new(a / b)
2968                }
2969                type_id if type_id == *U32 => {
2970                    let a = self.data.to_integer();
2971                    let b = other.data.to_integer();
2972                    Any::new(a / b)
2973                }
2974                type_id if type_id == *U64 => {
2975                    let a = self.data.to_integer();
2976                    let b = other.data.to_integer();
2977                    Any::new(a / b)
2978                }
2979                type_id if type_id == *USIZE => {
2980                    let a = self.data.to_integer();
2981                    let b = other.data.to_integer();
2982                    Any::new(a / b)
2983                }
2984                type_id if type_id == *F32 => {
2985                    let a = self.data.to_float();
2986                    let b = other.data.to_float();
2987                    Any::new(a / b)
2988                }
2989                type_id if type_id == *F64 => {
2990                    let a = self.data.to_float();
2991                    let b = other.data.to_float();
2992                    Any::new(a / b)
2993                }
2994                _ => Any::new(f64::NAN),
2995            }
2996        } else if self.type_id == *STRING || other.type_id == *STRING {
2997            Any::new(f64::NAN)
2998        } else if self.type_id == *STR || other.type_id == *STR {
2999            Any::new(f64::NAN)
3000        } else if self.type_id == *F64 || other.type_id == *F64 {
3001            let a: f64 = self.data.to_float();
3002            let b = other.data.to_float();
3003            Any::new(a / b)
3004        } else if self.type_id == *F32 || other.type_id == *F32 {
3005            let a: f64 = self.data.to_float();
3006            let b = other.data.to_float();
3007            Any::new(a / b)
3008        } else if self.type_id == *I64 || other.type_id == *I64 {
3009            let a = self.data.to_integer();
3010            let b = other.data.to_integer();
3011            Any::new(a / b)
3012        } else if self.type_id == *I32 || other.type_id == *I32 {
3013            let a = self.data.to_integer();
3014            let b = other.data.to_integer();
3015            Any::new(a / b)
3016        } else if self.type_id == *I16 || other.type_id == *I16 {
3017            let a = self.data.to_integer();
3018            let b = other.data.to_integer();
3019            Any::new(a / b)
3020        } else if self.type_id == *I8 || other.type_id == *I8 {
3021            let a = self.data.to_integer();
3022            let b = other.data.to_integer();
3023            let result = a + b;
3024            Any::new(result)
3025        } else if self.type_id == *U64 || other.type_id == *U64 {
3026            let a = self.data.to_integer();
3027            let b = other.data.to_integer();
3028            Any::new(a / b)
3029        } else if self.type_id == *U32 || other.type_id == *U32 {
3030            let a = self.data.to_integer();
3031            let b = other.data.to_integer();
3032            Any::new(a / b)
3033        } else if self.type_id == *U16 || other.type_id == *U16 {
3034            let a = self.data.to_integer();
3035            let b = other.data.to_integer();
3036            Any::new(a / b)
3037        } else if self.type_id == *U8 || other.type_id == *U8 {
3038            let a = self.data.to_integer();
3039            let b = other.data.to_integer();
3040            Any::new(a / b)
3041        } else {
3042            Any::new(f64::NAN)
3043        }
3044    }
3045}
3046
3047impl DivAssign for Any {
3048    fn div_assign(&mut self, other: Self) {
3049        *self = self.clone() / other;
3050    }
3051}
3052
3053#[cfg(test)]
3054mod test_div_for_any {
3055    use super::*;
3056
3057    #[test]
3058    fn test_div() {
3059        struct TestCase {
3060            name: String,
3061            a: Any,
3062            b: Any,
3063            result: Any,
3064        }
3065
3066        let test_cases = vec![
3067            TestCase {
3068                name: "i64".to_string(),
3069                a: Any::new(5_i64),
3070                b: Any::new(10_i64),
3071                result: Any::new(0_i64),
3072            },
3073            TestCase {
3074                name: "u64".to_string(),
3075                a: Any::new(5_u64),
3076                b: Any::new(10_u64),
3077                result: Any::new(0_i64),
3078            },
3079            TestCase {
3080                name: "f32".to_string(),
3081                a: Any::new(5.0_f32),
3082                b: Any::new(10.0_f32),
3083                result: Any::new(0.5_f64),
3084            },
3085            TestCase {
3086                name: "f64".to_string(),
3087                a: Any::new(5.0),
3088                b: Any::new(10.0),
3089                result: Any::new(0.5),
3090            },
3091        ];
3092
3093        for test_case in test_cases {
3094            let result = test_case.a / test_case.b;
3095            assert_eq!(result, test_case.result, "TC: {}", test_case.name);
3096        }
3097    }
3098
3099    #[test]
3100    fn test_div_assign() {
3101        let mut a = Any::new(5_i64);
3102        let b = Any::new(10_i64);
3103        a /= b;
3104        assert_eq!(a, Any::new(0_i64));
3105    }
3106}
3107
3108impl Not for Any {
3109    type Output = Self;
3110
3111    fn not(self) -> Self {
3112        if self.type_id == *NULL {
3113            Any::new(_null)
3114        } else {
3115            let a = self.data.to_boolean();
3116            Any::new(!a)
3117        }
3118    }
3119}
3120
3121#[cfg(test)]
3122mod test_not_for_any {
3123    use super::*;
3124
3125    #[test]
3126    fn test_not() {
3127        struct TestCase {
3128            name: String,
3129            a: Any,
3130            result: Any,
3131        }
3132
3133        let test_cases = vec![
3134            TestCase {
3135                name: "true".to_string(),
3136                a: Any::new(true),
3137                result: Any::new(false),
3138            },
3139            TestCase {
3140                name: "false".to_string(),
3141                a: Any::new(false),
3142                result: Any::new(true),
3143            },
3144            TestCase {
3145                name: "zero value".to_string(),
3146                a: Any::new(0),
3147                result: Any::new(true),
3148            },
3149            TestCase {
3150                name: "non zero value".to_string(),
3151                a: Any::new(4444),
3152                result: Any::new(false),
3153            },
3154        ];
3155
3156        for test_case in test_cases {
3157            let result = !test_case.a;
3158            assert_eq!(result, test_case.result, "TC: {}", test_case.name);
3159        }
3160    }
3161}
3162
3163impl PartialEq for Any {
3164    fn eq(&self, other: &Self) -> bool {
3165        if self.type_id != other.type_id {
3166            false
3167        } else {
3168            match self.type_id {
3169                type_id if type_id == *I8 => self.data.to_integer() == other.data.to_integer(),
3170                type_id if type_id == *I16 => self.data.to_integer() == other.data.to_integer(),
3171                type_id if type_id == *I32 => self.data.to_integer() == other.data.to_integer(),
3172                type_id if type_id == *I64 => self.data.to_integer() == other.data.to_integer(),
3173                type_id if type_id == *ISIZE => self.data.to_integer() == other.data.to_integer(),
3174                type_id if type_id == *U8 => self.data.to_integer() == other.data.to_integer(),
3175                type_id if type_id == *U16 => self.data.to_integer() == other.data.to_integer(),
3176                type_id if type_id == *U32 => self.data.to_integer() == other.data.to_integer(),
3177                type_id if type_id == *U64 => self.data.to_integer() == other.data.to_integer(),
3178                type_id if type_id == *USIZE => self.data.to_integer() == other.data.to_integer(),
3179                type_id if type_id == *F32 => self.data.to_float() == other.data.to_float(),
3180                type_id if type_id == *F64 => self.data.to_float() == other.data.to_float(),
3181                type_id if type_id == *STRING => self.data.to_string() == other.data.to_string(),
3182                type_id if type_id == *STR => self.data.to_string() == other.data.to_string(),
3183                type_id if type_id == *BOOL => self.data.to_boolean() == other.data.to_boolean(),
3184                type_id if type_id == *ARRAY => self.data.to_array() == other.data.to_array(),
3185                type_id if type_id == *MAP => self.data.to_map() == other.data.to_map(),
3186                _ => self.data.to_string() == other.data.to_string(),
3187            }
3188        }
3189    }
3190}
3191
3192impl Eq for Any {}
3193
3194#[cfg(test)]
3195mod test_eq_for_any {
3196    use super::*;
3197
3198    #[test]
3199    fn test_eq() {
3200        let a = Any::new(5);
3201        let b = Any::new(5);
3202        assert_eq!(a, b);
3203
3204        let a = Any::new(5);
3205        let b = Any::new(10);
3206        assert_ne!(a, b);
3207
3208        let a = Any::new(5);
3209        let b = Any::new(5.0);
3210        assert_ne!(a, b);
3211
3212        let a = Any::new(5);
3213        let b = Any::new(5.0);
3214        assert_ne!(a, b);
3215
3216        let a = Any::new(5);
3217        let b = Any::new("5");
3218        assert_ne!(a, b);
3219
3220        let a = Any::new(5);
3221        let b = Any::new("5");
3222        assert_ne!(a, b);
3223
3224        let a = Any::new(5);
3225        let b = Any::new(true);
3226        assert_ne!(a, b);
3227
3228        let a = Any::new(5);
3229        let b = Any::new(false);
3230        assert_ne!(a, b);
3231
3232        let a = Any::new(5);
3233        let b = Any::from(vec![1, 2, 3]);
3234        assert_ne!(a, b);
3235
3236        let a = Any::new(5);
3237        let b = Any::from(HashMap::new());
3238        assert_ne!(a, b);
3239
3240        let a = Any::new(5);
3241        let b = Any::new(_null);
3242        assert_ne!(a, b);
3243    }
3244}
3245
3246impl Hash for Any {
3247    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
3248        self.type_id.hash(state);
3249        match self.type_id {
3250            type_id if type_id == *I8 => self.data.to_integer().hash(state),
3251            type_id if type_id == *I16 => self.data.to_integer().hash(state),
3252            type_id if type_id == *I32 => self.data.to_integer().hash(state),
3253            type_id if type_id == *I64 => self.data.to_integer().hash(state),
3254            type_id if type_id == *ISIZE => self.data.to_integer().hash(state),
3255            type_id if type_id == *U8 => self.data.to_integer().hash(state),
3256            type_id if type_id == *U16 => self.data.to_integer().hash(state),
3257            type_id if type_id == *U32 => self.data.to_integer().hash(state),
3258            type_id if type_id == *U64 => self.data.to_integer().hash(state),
3259            type_id if type_id == *USIZE => self.data.to_integer().hash(state),
3260            type_id if type_id == *F32 => self.data.to_float().to_bits().hash(state),
3261            type_id if type_id == *F64 => self.data.to_float().to_bits().hash(state),
3262            type_id if type_id == *STRING => self.data.to_string().hash(state),
3263            type_id if type_id == *STR => self.data.to_string().hash(state),
3264            type_id if type_id == *BOOL => self.data.to_boolean().hash(state),
3265            type_id if type_id == *ARRAY => self.data.to_array().0.hash(state),
3266            type_id if type_id == *MAP => self.data.to_string().hash(state),
3267            _ => self.data.to_string().hash(state),
3268        }
3269    }
3270}
3271
3272impl<T> Index<T> for Any
3273where
3274    T: Into<Any>,
3275{
3276    type Output = Any;
3277
3278    fn index(&self, index: T) -> &Self::Output {
3279        let key: Any = index.into();
3280
3281        if self.type_id == *ARRAY {
3282            let array = self.data.to_array_ref();
3283            let key = key.data.to_integer() as usize;
3284            if key >= array.0.len() {
3285                return &null;
3286            }
3287
3288            &array.0[key]
3289        } else if self.type_id == *MAP {
3290            let map = self.data.to_map_ref();
3291
3292            map.0.get(&key).unwrap_or(&null)
3293        } else {
3294            &null
3295        }
3296    }
3297}
3298
3299impl<T> IndexMut<T> for Any
3300where
3301    T: Into<Any>,
3302{
3303    fn index_mut(&mut self, index: T) -> &mut Self::Output {
3304        let key: Any = index.into();
3305
3306        if self.type_id == *ARRAY {
3307            let array = self.data.to_array_mut();
3308            let key = key.data.to_integer() as usize;
3309            if key >= array.0.len() {
3310                unsafe {
3311                    let uninit: std::mem::MaybeUninit<Self::Output> =
3312                        std::mem::MaybeUninit::uninit();
3313                    let ptr = uninit.as_ptr() as *mut Self::Output;
3314                    *ptr = null.clone();
3315                    return &mut *ptr;
3316                }
3317            }
3318
3319            &mut array.0[key]
3320        } else if self.type_id == *MAP {
3321            let map = self.data.to_map_mut();
3322
3323            if map.0.get(&key).is_none() {
3324                map.0.insert(key.clone(), null.clone());
3325            }
3326
3327            map.0.get_mut(&key).unwrap()
3328        } else {
3329            unsafe {
3330                let uninit: std::mem::MaybeUninit<Self::Output> = std::mem::MaybeUninit::uninit();
3331                let ptr = uninit.as_ptr() as *mut Self::Output;
3332                *ptr = null.clone();
3333                &mut *ptr
3334            }
3335        }
3336    }
3337}
3338
3339#[cfg(test)]
3340mod test_indexer_for_any {
3341    use super::*;
3342
3343    #[test]
3344    fn test_array_indexer() {
3345        let a = Any::from(vec![1, 2, 3]);
3346        assert_eq!(a[0], Any::new(1));
3347        assert_eq!(a[1], Any::new(2));
3348        assert_eq!(a[2], Any::new(3));
3349        assert_eq!(a[3], Any::new(_null));
3350    }
3351
3352    #[test]
3353    fn test_map_indexer() {
3354        let mut a = Any::from(HashMap::new());
3355        a[Any::from(1)] = Any::new(1);
3356        a[Any::from(2)] = Any::new(2);
3357        a[3] = Any::new(3);
3358        assert_eq!(a[Any::from(1)], Any::new(1));
3359        assert_eq!(a[Any::from(2)], Any::new(2));
3360        assert_eq!(a[Any::from(3)], Any::new(3));
3361        assert_eq!(a[Any::from(4)], Any::new(_null));
3362    }
3363}
3364
3365impl IntoIterator for Any {
3366    type Item = Any;
3367    type IntoIter = AnyIterator;
3368
3369    fn into_iter(self) -> Self::IntoIter {
3370        if self.type_id == *ARRAY {
3371            let array = self.data.to_array();
3372            Box::new(array.0.into_iter())
3373        } else if self.type_id == *MAP {
3374            let map = self.data.to_map();
3375            Box::new(map.0.into_iter().map(|(k, v)| Any::from(Pair::new(k, v))))
3376        } else if self.type_id == *STRING || self.type_id == *STR {
3377            let iter = self
3378                .data
3379                .to_string()
3380                .chars()
3381                .map(Any::from)
3382                .collect::<Vec<_>>()
3383                .into_iter();
3384            Box::new(iter)
3385        } else {
3386            panic!("Cannot iterate over non-iterable type");
3387        }
3388    }
3389}
3390
3391type AnyIterator = Box<dyn Iterator<Item = Any>>;
3392
3393impl IntoIterator for Array {
3394    type Item = Any;
3395    type IntoIter = AnyIterator;
3396
3397    fn into_iter(self) -> Self::IntoIter {
3398        Box::new(self.0.into_iter())
3399    }
3400}
3401
3402type AnyPairIterator = Box<dyn Iterator<Item = (Any, Any)>>;
3403
3404impl IntoIterator for Map {
3405    type Item = (Any, Any);
3406    type IntoIter = AnyPairIterator;
3407
3408    fn into_iter(self) -> Self::IntoIter {
3409        (Box::new(self.0.into_iter())) as _
3410    }
3411}
3412
3413impl Shr for Any {
3414    type Output = Any;
3415
3416    fn shr(self, other: Self) -> Self {
3417        if self.type_id == *NULL || other.type_id == *NULL {
3418            Any::new(_null)
3419        } else if self.type_id == *FUNCTION && other.type_id == *FUNCTION {
3420            let a = self.to_function();
3421            let b = other.to_function();
3422            Any::from(a.composite(b))
3423        } else {
3424            let a = self.data.to_integer();
3425            let b = other.data.to_integer();
3426            Any::new(a >> b)
3427        }
3428    }
3429}
3430
3431/// Create a new array
3432///
3433/// Usage is similar to the `vec!` macro.
3434/// Each element is auto-boxed as any, and the final return value is also any.
3435/**
3436 ```rust
3437use anyrust::*;
3438let mut arr = array![1, 2, 3, 4, 5];
3439arr.push(4444);
3440arr.push("foo");
3441
3442for e in arr {
3443    println!("{e}");
3444}
3445 ```
3446 */
3447#[macro_export]
3448macro_rules! array {
3449    ($($x:expr),*) => {
3450        {
3451            let mut temp_vec = Vec::new();
3452            $(
3453                temp_vec.push(Any::from($x));
3454            )*
3455
3456            Any::from(anyrust::Array::from(temp_vec))
3457        }
3458    };
3459}
3460
3461/// Create a new params array (same as array! macro)
3462///
3463pub use array as params;
3464
3465/// Create a new function
3466///
3467/// This provides a shortcut to creating a Function object via macro expansion.
3468/**
3469```rust
3470use anyrust::*;
3471
3472let add = function!(lhs, rhs => {
3473    lhs + rhs
3474});
3475
3476let result = add.call(array![1, 2]);
3477println!("Result: {}", result);
3478
3479let four: Any = function!( => {
3480    let sum = any(4444);
3481    sum
3482});
3483
3484let result = four.call(array![]);
3485println!("Result: {}", result);
3486```
3487 */
3488#[macro_export]
3489macro_rules! function {
3490    ($($arg:ident),* => $body:block) => {
3491        {
3492            // for args_count
3493            let mut n = 0;
3494
3495            $(
3496                #[allow(unused_variables)]
3497                let $arg = 0;
3498                n += 1;
3499            )*
3500
3501            anyrust::Any::from(anyrust::Function::new(move |args| {
3502                let mut arg_index = 0;
3503                $(
3504                    let $arg = args[arg_index].clone();
3505                    arg_index += 1;
3506                )*
3507                $body
3508            }, n))
3509        }
3510    };
3511}
3512
3513/// Create a new pair
3514///
3515/// This provides a shortcut to creating a Pair object via macro expansion.
3516/**
3517```rust
3518use anyrust::*;
3519
3520let pair = pair!(1, 2);
3521assert_eq!(Any::from(Pair::new(1,2)), pair);
3522```
3523*/
3524#[macro_export]
3525macro_rules! pair {
3526    ($key:expr, $value:expr) => {{
3527        anyrust::Any::from(anyrust::Pair::new(
3528            anyrust::Any::from($key),
3529            anyrust::Any::from($value),
3530        ))
3531    }};
3532}
3533
3534/// Create a new map
3535///
3536/// This provides a shortcut to creating a Map object via macro expansion.
3537/**
3538```rust
3539use anyrust::*;
3540
3541let map = map!{
3542    "foo" => 1,
3543    "bar" => 2,
3544    "baz" => 3,
3545};
3546
3547let result = map[any("foo")].clone();
3548assert_eq!(result, Any::from(1));
3549```
3550*/
3551#[macro_export]
3552macro_rules! map {
3553    ($($key:expr => $value:expr),* $(,)?) => {
3554        {
3555            let mut temp_map = std::collections::HashMap::new();
3556            $(
3557                temp_map.insert(anyrust::Any::from($key), anyrust::Any::from($value));
3558            )*
3559
3560            anyrust::Any::from(anyrust::Map::from(temp_map))
3561        }
3562    };
3563}