array_object/convert/
from_integer.rs

1use crate::adaptor::*;
2use crate::convert::zigzag::Zigzag;
3use crate::error::ArrayObjectError;
4use crate::misc::Product;
5use crate::storage::*;
6
7macro_rules! from_unsigned_integer {
8    ($($ty:ty),*) => {
9        $(
10            impl From<$ty> for ArrayObject {
11                fn from(val: $ty) -> Self {
12                    (&val).into()
13                }
14            }
15            impl From<Vec<$ty>> for ArrayObject {
16                fn from(val: Vec<$ty>) -> Self {
17                    (&val).into()
18                }
19            }
20            impl<const N: usize> From<[$ty; N]> for ArrayObject {
21                fn from(val: [$ty; N]) -> Self {
22                    (&val).into()
23                }
24            }
25            // Reference
26            impl From<&$ty> for ArrayObject {
27                fn from(val: &$ty) -> Self {
28                    let data = val.to_le_bytes().to_vec();
29                    Self {
30                        data,
31                        shape: vec![],
32                        datatype: DataType::UnsignedInteger,
33                    }
34                }
35            }
36            impl From<&Vec<$ty>> for ArrayObject {
37                fn from(val: &Vec<$ty>) -> Self {
38                    let shape = vec![val.len() as u64];
39                    let mut data = Vec::<u8>::with_capacity(size_of::<$ty>() * val.len());
40                    for v in val {
41                        data.append(&mut v.to_le_bytes().to_vec());
42                    }
43                    Self {
44                        data,
45                        shape,
46                        datatype: DataType::UnsignedInteger,
47                    }
48                }
49            }
50            impl<const N: usize> From<&[$ty; N]> for ArrayObject {
51                fn from(val: &[$ty; N]) -> Self {
52                    val.to_vec().into()
53                }
54            }
55            // Double reference
56            impl From<&&$ty> for ArrayObject {
57                fn from(val: &&$ty) -> Self {
58                    (*val).into()
59                }
60            }
61            impl From<&&Vec<$ty>> for ArrayObject {
62                fn from(val: &&Vec<$ty>) -> Self {
63                    (*val).into()
64                }
65            }
66            impl<const N: usize> From<&&[$ty; N]> for ArrayObject {
67                fn from(val: &&[$ty; N]) -> Self {
68                    (*val).into()
69                }
70            }
71            // Box
72            impl From<Box<$ty>> for ArrayObject {
73                fn from(val: Box<$ty>) -> Self {
74                    val.as_ref().into()
75                }
76            }
77            impl From<Box<Vec<$ty>>> for ArrayObject {
78                fn from(val: Box<Vec<$ty>>) -> Self {
79                    val.as_ref().into()
80                }
81            }
82            impl<const N: usize> From<Box<[$ty; N]>> for ArrayObject {
83                fn from(val: Box<[$ty; N]>) -> Self {
84                    val.as_ref().into()
85                }
86            }
87            // Adaptor
88            impl TryFrom<VecShape<$ty>> for ArrayObject {
89                type Error = ArrayObjectError;
90                fn try_from(VecShape(val, shape): VecShape<$ty>) -> Result<Self, Self::Error> {
91                    if val.len() != shape.product() as usize {
92                        return Err(ArrayObjectError::NumberOfElementsMismatch(val.len(), shape.product() as usize));
93                    }
94                    if shape.len() > 15 {
95                        return Err(ArrayObjectError::TooLargeDimension(shape.len()));
96                    }
97                    let mut temp: ArrayObject = val.into();
98                    temp.shape = shape;
99                    Ok(temp)
100                }
101            }
102        )*
103    };
104}
105
106from_unsigned_integer!(u8, u16, u32, u64, u128, usize);
107
108macro_rules! from_signed_integer {
109    ($($ty:ty),*) => {
110        $(
111            impl From<$ty> for ArrayObject {
112                fn from(val: $ty) -> Self {
113                    (&val).into()
114                }
115            }
116            impl From<Vec<$ty>> for ArrayObject {
117                fn from(val: Vec<$ty>) -> Self {
118                    (&val).into()
119                }
120            }
121            impl<const N: usize> From<[$ty; N]> for ArrayObject {
122                fn from(val: [$ty; N]) -> Self {
123                    (&val).into()
124                }
125            }
126            // Reference
127            impl From<&$ty> for ArrayObject {
128                fn from(val: &$ty) -> Self {
129                    let data = val.zigzag().to_le_bytes().to_vec();
130                    Self {
131                        data,
132                        shape: vec![],
133                        datatype: DataType::SignedInteger,
134                    }
135                }
136            }
137            impl From<&Vec<$ty>> for ArrayObject {
138                fn from(val: &Vec<$ty>) -> Self {
139                    let shape = vec![val.len() as u64];
140                    let mut data = Vec::<u8>::with_capacity(size_of::<$ty>() * val.len());
141                    for v in val {
142                        data.append(&mut v.zigzag().to_le_bytes().to_vec());
143                    }
144                    Self {
145                        data,
146                        shape,
147                        datatype: DataType::SignedInteger,
148                    }
149                }
150            }
151            impl<const N: usize> From<&[$ty; N]> for ArrayObject {
152                fn from(val: &[$ty; N]) -> Self {
153                    val.to_vec().into()
154                }
155            }
156            // Double reference
157            impl From<&&$ty> for ArrayObject {
158                fn from(val: &&$ty) -> Self {
159                    (*val).into()
160                }
161            }
162            impl From<&&Vec<$ty>> for ArrayObject {
163                fn from(val: &&Vec<$ty>) -> Self {
164                    (*val).into()
165                }
166            }
167            impl<const N: usize> From<&&[$ty; N]> for ArrayObject {
168                fn from(val: &&[$ty; N]) -> Self {
169                    (*val).into()
170                }
171            }
172            // Box
173            impl From<Box<$ty>> for ArrayObject {
174                fn from(val: Box<$ty>) -> Self {
175                    val.as_ref().into()
176                }
177            }
178            impl From<Box<Vec<$ty>>> for ArrayObject {
179                fn from(val: Box<Vec<$ty>>) -> Self {
180                    val.as_ref().into()
181                }
182            }
183            impl<const N: usize> From<Box<[$ty; N]>> for ArrayObject {
184                fn from(val: Box<[$ty; N]>) -> Self {
185                    val.as_ref().into()
186                }
187            }
188            // Adaptor
189            impl TryFrom<VecShape<$ty>> for ArrayObject {
190                type Error = ArrayObjectError;
191                fn try_from(VecShape(val, shape): VecShape<$ty>) -> Result<Self, Self::Error> {
192                    if val.len() != shape.product() as usize {
193                        return Err(ArrayObjectError::NumberOfElementsMismatch(val.len(), shape.product() as usize));
194                    }
195                    if shape.len() > 15 {
196                        return Err(ArrayObjectError::TooLargeDimension(shape.len()));
197                    }
198                    let mut temp: ArrayObject = val.into();
199                    temp.shape = shape;
200                    Ok(temp)
201                }
202            }
203        )*
204    };
205}
206
207from_signed_integer!(i8, i16, i32, i64, i128, isize);