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                    let data = val.to_le_bytes().to_vec();
13                    Self {
14                        data,
15                        shape: vec![],
16                        datatype: DataType::UnsignedInteger,
17                    }
18                }
19            }
20            impl From<Vec<$ty>> for ArrayObject {
21                fn from(val: Vec<$ty>) -> Self {
22                    let shape = vec![val.len() as u64];
23                    let mut data = Vec::<u8>::with_capacity(size_of::<$ty>() * val.len());
24                    for v in val {
25                        data.append(&mut v.to_le_bytes().to_vec());
26                    }
27                    Self {
28                        data,
29                        shape,
30                        datatype: DataType::UnsignedInteger,
31                    }
32                }
33            }
34            impl<const N: usize> From<[$ty; N]> for ArrayObject {
35                fn from(val: [$ty; N]) -> Self {
36                    val.to_vec().into()
37                }
38            }
39            impl From<&[$ty]> for ArrayObject {
40                fn from(val: &[$ty]) -> Self {
41                    val.to_vec().into()
42                }
43            }
44            impl TryFrom<VecShape<$ty>> for ArrayObject {
45                type Error = ArrayObjectError;
46                fn try_from(VecShape(val, shape): VecShape<$ty>) -> Result<Self, Self::Error> {
47                    if val.len() != shape.product() as usize {
48                        return Err(ArrayObjectError::NumberOfElementsMismatch(val.len(), shape.product() as usize));
49                    }
50                    if shape.len() > 15 {
51                        return Err(ArrayObjectError::TooLargeDimension(shape.len()));
52                    }
53                    let mut temp: ArrayObject = val.into();
54                    temp.shape = shape;
55                    Ok(temp)
56                }
57            }
58        )*
59    };
60}
61
62from_unsigned_integer!(u8, u16, u32, u64, u128, usize);
63
64macro_rules! from_signed_integer {
65    ($($ty:ty),*) => {
66        $(
67            impl From<$ty> for ArrayObject {
68                fn from(val: $ty) -> Self {
69                    let data = val.zigzag().to_le_bytes().to_vec();
70                    Self {
71                        data,
72                        shape: vec![],
73                        datatype: DataType::SignedInteger,
74                    }
75                }
76            }
77            impl From<Vec<$ty>> for ArrayObject {
78                fn from(val: Vec<$ty>) -> Self {
79                    let shape = vec![val.len() as u64];
80                    let mut data = Vec::<u8>::with_capacity(size_of::<$ty>() * val.len());
81                    for v in val {
82                        data.append(&mut v.zigzag().to_le_bytes().to_vec());
83                    }
84                    Self {
85                        data,
86                        shape,
87                        datatype: DataType::SignedInteger,
88                    }
89                }
90            }
91            impl TryFrom<VecShape<$ty>> for ArrayObject {
92                type Error = ArrayObjectError;
93                fn try_from(VecShape(val, shape): VecShape<$ty>) -> Result<Self, Self::Error> {
94                    if val.len() != shape.product() as usize {
95                        return Err(ArrayObjectError::NumberOfElementsMismatch(val.len(), shape.product() as usize));
96                    }
97                    if shape.len() > 15 {
98                        return Err(ArrayObjectError::TooLargeDimension(shape.len()));
99                    }
100                    let mut temp: ArrayObject = val.into();
101                    temp.shape = shape;
102                    Ok(temp)
103                }
104            }
105        )*
106    };
107}
108
109from_signed_integer!(i8, i16, i32, i64, i128, isize);