1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use crate::{backend::*, macros::*, BQNValue, LOCK};

impl From<f64> for BQNValue {
    fn from(v: f64) -> BQNValue {
        let _l = LOCK.lock();
        BQNValue::new(bqn_makeF64(v).unwrap())
    }
}

impl From<i32> for BQNValue {
    fn from(v: i32) -> BQNValue {
        let _l = LOCK.lock();
        BQNValue::new(bqn_makeF64(v as f64).unwrap())
    }
}

impl From<char> for BQNValue {
    fn from(v: char) -> BQNValue {
        let _l = LOCK.lock();
        BQNValue::new(bqn_makeChar(v as u32).unwrap())
    }
}

impl_from_string_like!(&str);
impl_from_string_like!(&String);
impl_from_string_like!(String);
impl_from_string_like_vec!(&str);
impl_from_string_like_vec!(&String);
impl_from_string_like_vec!(String);

impl<const N: usize> From<[&str; N]> for BQNValue {
    fn from(arr: [&str; N]) -> BQNValue {
        crate::INIT.call_once(|| {
            let _l = LOCK.lock();
            bqn_init().unwrap()
        });
        let mut strs = Vec::with_capacity(N);
        let _l = LOCK.lock();
        for s in arr {
            strs.push(bqn_makeUTF8Str(s).unwrap());
        }
        BQNValue::new(bqn_makeObjVec(&strs).unwrap())
    }
}

impl<const N: usize> From<[String; N]> for BQNValue {
    fn from(arr: [String; N]) -> BQNValue {
        crate::INIT.call_once(|| {
            let _l = LOCK.lock();
            bqn_init().unwrap();
        });
        let mut strs = Vec::with_capacity(N);
        let _l = LOCK.lock();
        for s in arr {
            strs.push(bqn_makeUTF8Str(&s).unwrap());
        }
        BQNValue::new(bqn_makeObjVec(&strs).unwrap())
    }
}

impl_from_array!(f64, bqn_makeF64Vec);
impl_from_array!(i32, bqn_makeI32Vec);
impl_from_array!(i16, bqn_makeI16Vec);
impl_from_array!(i8, bqn_makeI8Vec);
impl<const N: usize> From<[BQNValue; N]> for BQNValue {
    fn from(arr: [BQNValue; N]) -> BQNValue {
        crate::INIT.call_once(|| {
            let _l = LOCK.lock();
            bqn_init().unwrap()
        });
        let elems = arr.into_iter().map(|v| v.value).collect::<Vec<_>>();
        let _l = LOCK.lock();
        BQNValue::new(bqn_makeObjVec(&elems).unwrap())
    }
}

impl_from_slice!(&[f64], bqn_makeF64Vec);
impl_from_slice!(&[i32], bqn_makeI32Vec);
impl_from_slice!(&[i16], bqn_makeI16Vec);
impl_from_slice!(&[i8], bqn_makeI8Vec);
impl From<&[BQNValue]> for BQNValue {
    fn from(arr: &[BQNValue]) -> BQNValue {
        crate::INIT.call_once(|| {
            let _l = LOCK.lock();
            bqn_init().unwrap()
        });

        let elems = arr.iter().map(|v| v.value).collect::<Vec<_>>();
        let _l = LOCK.lock();
        BQNValue::new(bqn_makeObjVec(&elems).unwrap())
    }
}

impl_from_vec!(f64, bqn_makeF64Vec);
impl_from_vec!(i32, bqn_makeI32Vec);
impl_from_vec!(i16, bqn_makeI16Vec);
impl_from_vec!(i8, bqn_makeI8Vec);
impl From<Vec<BQNValue>> for BQNValue {
    fn from(arr: Vec<BQNValue>) -> BQNValue {
        crate::INIT.call_once(|| {
            let _l = LOCK.lock();
            bqn_init().unwrap();
        });

        let elems = arr.into_iter().map(|v| v.value).collect::<Vec<_>>();
        let _l = LOCK.lock();
        BQNValue::new(bqn_makeObjVec(&elems).unwrap())
    }
}

impl_from_iterator!(f64, bqn_makeF64Vec);
impl_from_iterator!(i32, bqn_makeI32Vec);
impl_from_iterator!(i16, bqn_makeI16Vec);
impl_from_iterator!(i8, bqn_makeI8Vec);
impl FromIterator<BQNValue> for BQNValue {
    fn from_iter<T>(iter: T) -> BQNValue
    where
        T: IntoIterator<Item = BQNValue>,
    {
        crate::INIT.call_once(|| {
            let _l = LOCK.lock();
            bqn_init().unwrap();
        });

        let elems = iter.into_iter().map(|v| v.value).collect::<Vec<_>>();
        let _l = LOCK.lock();
        BQNValue::new(bqn_makeObjVec(&elems).unwrap())
    }
}