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