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
use ndarray::{IntoDimension, Ix0, Ix1, Ix2, Ix3, Ix4, Ix5, Ix6};
#[derive(Fail, Debug)]
#[fail(
display = "Expected a {}-dimensional array, but got one with {} dimensions",
expected, actual
)]
pub struct DimensionMismatchError {
pub expected: usize,
pub actual: usize,
}
pub trait DimFromShapeSlice<T>: Sized {
fn from_shape_slice(shape: &[T]) -> Result<Self, DimensionMismatchError>;
}
macro_rules! impl_dim_from_shape_slice {
($dimtype:ty; $ndim:expr; $($numbers:expr);*) => {
impl DimFromShapeSlice<u64> for $dimtype {
fn from_shape_slice(shape: &[u64]) -> Result<Self, DimensionMismatchError> {
if shape.len() == $ndim {
Ok([$(shape[$numbers] as usize),*].into_dimension())
} else {
Err(DimensionMismatchError { expected: $ndim, actual: shape.len() })
}
}
}
impl DimFromShapeSlice<usize> for $dimtype {
fn from_shape_slice(shape: &[usize]) -> Result<Self, DimensionMismatchError> {
if shape.len() == $ndim {
Ok([$(shape[$numbers] as usize),*].into_dimension())
} else {
Err(DimensionMismatchError { expected: $ndim, actual: shape.len() })
}
}
}
}
}
impl_dim_from_shape_slice! { Ix0; 0; }
impl_dim_from_shape_slice! { Ix1; 1; 0 }
impl_dim_from_shape_slice! { Ix2; 2; 0;1 }
impl_dim_from_shape_slice! { Ix3; 3; 0;1;2 }
impl_dim_from_shape_slice! { Ix4; 4; 0;1;2;3 }
impl_dim_from_shape_slice! { Ix5; 5; 0;1;2;3;4 }
impl_dim_from_shape_slice! { Ix6; 6; 0;1;2;3;4;5 }