array_object/convert/
from_integer.rs

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
use crate::adaptor::*;
use crate::convert::zigzag::Zigzag;
use crate::error::ArrayObjectError;
use crate::misc::Product;
use crate::storage::*;

macro_rules! from_unsigned_integer {
    ($($ty:ty),*) => {
        $(
            impl From<$ty> for ArrayObject {
                fn from(val: $ty) -> Self {
                    let data = val.to_le_bytes().to_vec();
                    Self {
                        data,
                        shape: vec![],
                        datatype: DataType::UnsignedInteger,
                    }
                }
            }
            impl From<Vec<$ty>> for ArrayObject {
                fn from(val: Vec<$ty>) -> Self {
                    let shape = vec![val.len() as u64];
                    let mut data = Vec::<u8>::with_capacity(size_of::<$ty>() * val.len());
                    for v in val {
                        data.append(&mut v.to_le_bytes().to_vec());
                    }
                    Self {
                        data,
                        shape,
                        datatype: DataType::UnsignedInteger,
                    }
                }
            }
            impl<const N: usize> From<[$ty; N]> for ArrayObject {
                fn from(val: [$ty; N]) -> Self {
                    val.to_vec().into()
                }
            }
            impl From<&[$ty]> for ArrayObject {
                fn from(val: &[$ty]) -> Self {
                    val.to_vec().into()
                }
            }
            impl TryFrom<VecShape<$ty>> for ArrayObject {
                type Error = ArrayObjectError;
                fn try_from(VecShape(val, shape): VecShape<$ty>) -> Result<Self, Self::Error> {
                    if val.len() != shape.product() as usize {
                        return Err(ArrayObjectError::NumberOfElementsMismatch(val.len(), shape.product() as usize));
                    }
                    if shape.len() > 15 {
                        return Err(ArrayObjectError::TooLargeDimension(shape.len()));
                    }
                    let mut temp: ArrayObject = val.into();
                    temp.shape = shape;
                    Ok(temp)
                }
            }
        )*
    };
}

from_unsigned_integer!(u8, u16, u32, u64, u128);

macro_rules! from_signed_integer {
    ($($ty:ty),*) => {
        $(
            impl From<$ty> for ArrayObject {
                fn from(val: $ty) -> Self {
                    let data = val.zigzag().to_le_bytes().to_vec();
                    Self {
                        data,
                        shape: vec![],
                        datatype: DataType::SignedInteger,
                    }
                }
            }
            impl From<Vec<$ty>> for ArrayObject {
                fn from(val: Vec<$ty>) -> Self {
                    let shape = vec![val.len() as u64];
                    let mut data = Vec::<u8>::with_capacity(size_of::<$ty>() * val.len());
                    for v in val {
                        data.append(&mut v.zigzag().to_le_bytes().to_vec());
                    }
                    Self {
                        data,
                        shape,
                        datatype: DataType::SignedInteger,
                    }
                }
            }
            impl TryFrom<VecShape<$ty>> for ArrayObject {
                type Error = ArrayObjectError;
                fn try_from(VecShape(val, shape): VecShape<$ty>) -> Result<Self, Self::Error> {
                    if val.len() != shape.product() as usize {
                        return Err(ArrayObjectError::NumberOfElementsMismatch(val.len(), shape.product() as usize));
                    }
                    if shape.len() > 15 {
                        return Err(ArrayObjectError::TooLargeDimension(shape.len()));
                    }
                    let mut temp: ArrayObject = val.into();
                    temp.shape = shape;
                    Ok(temp)
                }
            }
        )*
    };
}

from_signed_integer!(i8, i16, i32, i64, i128);