1
1
use std:: fmt:: { Debug , Formatter , Result } ;
2
-
3
- use std:: ops:: {
4
- Bound , IndexMut , Range , RangeFrom , RangeFull , RangeInclusive , RangeTo , RangeToInclusive ,
5
- } ;
2
+ use std:: hash:: Hash ;
6
3
7
4
/// Array dimension trait.
8
- pub trait Dim : Copy + Debug + Default + Send + Sync {
5
+ pub trait Dim : Copy + Debug + Default + Eq + Hash + Send + Sync {
9
6
/// Merge dimensions, where constant size is preferred over dynamic.
10
7
type Merge < D : Dim > : Dim ;
11
8
@@ -23,50 +20,28 @@ pub trait Dim: Copy + Debug + Default + Send + Sync {
23
20
fn size ( self ) -> usize ;
24
21
}
25
22
26
- /// Array dimensions trait.
27
- pub trait Dims :
28
- Copy
29
- + Debug
30
- + Default
31
- + IndexMut < ( Bound < usize > , Bound < usize > ) , Output = [ usize ] >
32
- + IndexMut < usize , Output = usize >
33
- + IndexMut < Range < usize > , Output = [ usize ] >
34
- + IndexMut < RangeFrom < usize > , Output = [ usize ] >
35
- + IndexMut < RangeFull , Output = [ usize ] >
36
- + IndexMut < RangeInclusive < usize > , Output = [ usize ] >
37
- + IndexMut < RangeTo < usize > , Output = [ usize ] >
38
- + IndexMut < RangeToInclusive < usize > , Output = [ usize ] >
39
- + Send
40
- + Sync
41
- + for < ' a > TryFrom < & ' a [ usize ] , Error : Debug >
42
- {
43
- }
44
-
45
- /// Array strides trait.
46
- pub trait Strides :
47
- Copy
23
+ #[ allow( unreachable_pub) ]
24
+ pub trait Dims < T : Copy + Debug + Default + Eq + Hash + Send + Sync > :
25
+ AsMut < [ T ] >
26
+ + AsRef < [ T ] >
27
+ + Clone
48
28
+ Debug
49
29
+ Default
50
- + IndexMut < ( Bound < usize > , Bound < usize > ) , Output = [ isize ] >
51
- + IndexMut < usize , Output = isize >
52
- + IndexMut < Range < usize > , Output = [ isize ] >
53
- + IndexMut < RangeFrom < usize > , Output = [ isize ] >
54
- + IndexMut < RangeFull , Output = [ isize ] >
55
- + IndexMut < RangeInclusive < usize > , Output = [ isize ] >
56
- + IndexMut < RangeTo < usize > , Output = [ isize ] >
57
- + IndexMut < RangeToInclusive < usize > , Output = [ isize ] >
30
+ + Eq
31
+ + Hash
58
32
+ Send
59
33
+ Sync
60
- + for < ' a > TryFrom < & ' a [ isize ] , Error : Debug >
34
+ + for < ' a > TryFrom < & ' a [ T ] , Error : Debug >
61
35
{
36
+ fn new ( len : usize ) -> Self ;
62
37
}
63
38
64
39
/// Type-level constant.
65
- #[ derive( Clone , Copy , Default ) ]
40
+ #[ derive( Clone , Copy , Default , Eq , Hash , PartialEq ) ]
66
41
pub struct Const < const N : usize > ;
67
42
68
43
/// Dynamically-sized dimension type.
69
- #[ derive( Clone , Copy , Debug , Default ) ]
44
+ #[ derive( Clone , Copy , Debug , Default , Eq , Hash , PartialEq ) ]
70
45
pub struct Dyn ( pub usize ) ;
71
46
72
47
impl < const N : usize > Debug for Const < N > {
@@ -105,13 +80,24 @@ impl Dim for Dyn {
105
80
}
106
81
}
107
82
108
- macro_rules! impl_dims_strides {
83
+ macro_rules! impl_dims {
109
84
( $( $n: tt) ,+) => {
110
85
$(
111
- impl Dims for [ usize ; $n] { }
112
- impl Strides for [ isize ; $n] { }
86
+ impl <T : Copy + Debug + Default + Eq + Hash + Send + Sync > Dims <T > for [ T ; $n] {
87
+ fn new( len: usize ) -> Self {
88
+ assert!( len == $n, "invalid length" ) ;
89
+
90
+ Self :: default ( )
91
+ }
92
+ }
113
93
) +
114
94
} ;
115
95
}
116
96
117
- impl_dims_strides ! ( 0 , 1 , 2 , 3 , 4 , 5 , 6 ) ;
97
+ impl_dims ! ( 0 , 1 , 2 , 3 , 4 , 5 , 6 ) ;
98
+
99
+ impl < T : Copy + Debug + Default + Eq + Hash + Send + Sync > Dims < T > for Box < [ T ] > {
100
+ fn new ( len : usize ) -> Self {
101
+ vec ! [ T :: default ( ) ; len] . into ( )
102
+ }
103
+ }
0 commit comments