array_object/convert/
from_real.rs

1use crate::adaptor::*;
2use crate::error::ArrayObjectError;
3use crate::misc::Product;
4use crate::storage::*;
5
6macro_rules! from_float {
7    ($($ty:ty),*) => {
8        $(
9            impl From<$ty> for ArrayObject {
10                fn from(val: $ty) -> Self {
11                    let data = val.to_le_bytes().to_vec();
12                    Self {
13                        data,
14                        shape: vec![],
15                        datatype: DataType::Real,
16                    }
17                }
18            }
19            impl From<&$ty> for ArrayObject {
20                fn from(val: &$ty) -> Self {
21                    let data = val.to_le_bytes().to_vec();
22                    Self {
23                        data,
24                        shape: vec![],
25                        datatype: DataType::Real,
26                    }
27                }
28            }
29            impl From<Vec<$ty>> for ArrayObject {
30                fn from(val: Vec<$ty>) -> Self {
31                    let shape = vec![val.len() as u64];
32                    let mut data = Vec::<u8>::with_capacity(size_of::<$ty>() * val.len());
33                    for v in val {
34                        data.append(&mut v.to_le_bytes().to_vec());
35                    }
36                    Self {
37                        data,
38                        shape,
39                        datatype: DataType::Real,
40                    }
41                }
42            }
43            impl From<&Vec<$ty>> for ArrayObject {
44                fn from(val: &Vec<$ty>) -> Self {
45                    let shape = vec![val.len() as u64];
46                    let mut data = Vec::<u8>::with_capacity(size_of::<$ty>() * val.len());
47                    for v in val {
48                        data.append(&mut v.to_le_bytes().to_vec());
49                    }
50                    Self {
51                        data,
52                        shape,
53                        datatype: DataType::Real,
54                    }
55                }
56            }
57            impl<const N: usize> From<[$ty; N]> for ArrayObject {
58                fn from(val: [$ty; N]) -> Self {
59                    val.to_vec().into()
60                }
61            }
62            impl From<&[$ty]> for ArrayObject {
63                fn from(val: &[$ty]) -> Self {
64                    val.to_vec().into()
65                }
66            }
67            impl TryFrom<VecShape<$ty>> for ArrayObject {
68                type Error = ArrayObjectError;
69                fn try_from(VecShape(val, shape): VecShape<$ty>) -> Result<Self, Self::Error> {
70                    if val.len() != shape.product() as usize {
71                        return Err(ArrayObjectError::NumberOfElementsMismatch(val.len(), shape.product() as usize));
72                    }
73                    if shape.len() > 15 {
74                        return Err(ArrayObjectError::TooLargeDimension(shape.len()));
75                    }
76                    let mut temp: ArrayObject = val.into();
77                    temp.shape = shape;
78                    Ok(temp)
79                }
80            }
81        )*
82    };
83}
84
85from_float!(f32, f64);