6
6
// option. This file may not be copied, modified, or distributed
7
7
// except according to those terms.
8
8
9
- use std:: mem:: { forget, size_of} ;
10
- use alloc:: slice;
11
9
use alloc:: vec;
12
10
use alloc:: vec:: Vec ;
11
+ use std:: mem:: { forget, size_of} ;
13
12
14
13
use crate :: imp_prelude:: * ;
15
14
use crate :: { dimension, ArcArray1 , ArcArray2 } ;
@@ -87,26 +86,10 @@ pub fn aview1<A>(xs: &[A]) -> ArrayView1<'_, A> {
87
86
88
87
/// Create a two-dimensional array view with elements borrowing `xs`.
89
88
///
90
- /// **Panics** if the product of non-zero axis lengths overflows `isize`. (This
91
- /// can only occur when `V` is zero-sized.)
92
- pub fn aview2 < A , V : FixedInitializer < Elem = A > > ( xs : & [ V ] ) -> ArrayView2 < ' _ , A > {
93
- let cols = V :: len ( ) ;
94
- let rows = xs. len ( ) ;
95
- let dim = Ix2 ( rows, cols) ;
96
- if size_of :: < V > ( ) == 0 {
97
- dimension:: size_of_shape_checked ( & dim)
98
- . expect ( "Product of non-zero axis lengths must not overflow isize." ) ;
99
- }
100
- // `rows` is guaranteed to fit in `isize` because we've checked the ZST
101
- // case and slices never contain > `isize::MAX` bytes. `cols` is guaranteed
102
- // to fit in `isize` because `FixedInitializer` is not implemented for any
103
- // array lengths > `isize::MAX`. `cols * rows` is guaranteed to fit in
104
- // `isize` because we've checked the ZST case and slices never contain >
105
- // `isize::MAX` bytes.
106
- unsafe {
107
- let data = slice:: from_raw_parts ( xs. as_ptr ( ) as * const A , cols * rows) ;
108
- ArrayView :: from_shape_ptr ( dim, data. as_ptr ( ) )
109
- }
89
+ /// **Panics** if the product of non-zero axis lengths overflows `isize` (This can only occur if A
90
+ /// is zero-sized because slices cannot contain more than `isize::MAX` number of bytes).
91
+ pub fn aview2 < A , const N : usize > ( xs : & [ [ A ; N ] ] ) -> ArrayView2 < ' _ , A > {
92
+ ArrayView2 :: from ( xs)
110
93
}
111
94
112
95
/// Create a one-dimensional read-write array view with elements borrowing `xs`.
@@ -127,16 +110,15 @@ pub fn aview_mut1<A>(xs: &mut [A]) -> ArrayViewMut1<'_, A> {
127
110
128
111
/// Create a two-dimensional read-write array view with elements borrowing `xs`.
129
112
///
130
- /// **Panics** if the product of non-zero axis lengths overflows `isize`. (This
131
- /// can only occur when `V` is zero-sized.)
113
+ /// **Panics** if the product of non-zero axis lengths overflows `isize` (This can only occur if A
114
+ /// is zero-sized because slices cannot contain more than `isize::MAX` number of bytes).
132
115
///
133
116
/// # Example
134
117
///
135
118
/// ```
136
119
/// use ndarray::aview_mut2;
137
120
///
138
- /// // The inner (nested) array must be of length 1 to 16, but the outer
139
- /// // can be of any length.
121
+ /// // The inner (nested) and outer arrays can be of any length.
140
122
/// let mut data = [[0.; 2]; 128];
141
123
/// {
142
124
/// // Make a 128 x 2 mut array view then turn it into 2 x 128
@@ -148,57 +130,10 @@ pub fn aview_mut1<A>(xs: &mut [A]) -> ArrayViewMut1<'_, A> {
148
130
/// // look at the start of the result
149
131
/// assert_eq!(&data[..3], [[1., -1.], [1., -1.], [1., -1.]]);
150
132
/// ```
151
- pub fn aview_mut2 < A , V : FixedInitializer < Elem = A > > ( xs : & mut [ V ] ) -> ArrayViewMut2 < ' _ , A > {
152
- let cols = V :: len ( ) ;
153
- let rows = xs. len ( ) ;
154
- let dim = Ix2 ( rows, cols) ;
155
- if size_of :: < V > ( ) == 0 {
156
- dimension:: size_of_shape_checked ( & dim)
157
- . expect ( "Product of non-zero axis lengths must not overflow isize." ) ;
158
- }
159
- // `rows` is guaranteed to fit in `isize` because we've checked the ZST
160
- // case and slices never contain > `isize::MAX` bytes. `cols` is guaranteed
161
- // to fit in `isize` because `FixedInitializer` is not implemented for any
162
- // array lengths > `isize::MAX`. `cols * rows` is guaranteed to fit in
163
- // `isize` because we've checked the ZST case and slices never contain >
164
- // `isize::MAX` bytes.
165
- unsafe {
166
- let data = slice:: from_raw_parts_mut ( xs. as_mut_ptr ( ) as * mut A , cols * rows) ;
167
- ArrayViewMut :: from_shape_ptr ( dim, data. as_mut_ptr ( ) )
168
- }
133
+ pub fn aview_mut2 < A , const N : usize > ( xs : & mut [ [ A ; N ] ] ) -> ArrayViewMut2 < ' _ , A > {
134
+ ArrayViewMut2 :: from ( xs)
169
135
}
170
136
171
- /// Fixed-size array used for array initialization
172
- #[ allow( clippy:: missing_safety_doc) ] // Should not be implemented downstream and to be deprecated.
173
- pub unsafe trait FixedInitializer {
174
- type Elem ;
175
- fn as_init_slice ( & self ) -> & [ Self :: Elem ] ;
176
- fn len ( ) -> usize ;
177
- }
178
-
179
- macro_rules! impl_arr_init {
180
- ( __impl $n: expr) => (
181
- unsafe impl <T > FixedInitializer for [ T ; $n] {
182
- type Elem = T ;
183
- fn as_init_slice( & self ) -> & [ T ] { self }
184
- fn len( ) -> usize { $n }
185
- }
186
- ) ;
187
- ( ) => ( ) ;
188
- ( $n: expr, $( $m: expr, ) * ) => (
189
- impl_arr_init!( __impl $n) ;
190
- impl_arr_init!( $( $m, ) * ) ;
191
- )
192
-
193
- }
194
-
195
- // For implementors: If you ever implement `FixedInitializer` for array lengths
196
- // > `isize::MAX` (e.g. once Rust adds const generics), you must update
197
- // `aview2` and `aview_mut2` to perform the necessary checks. In particular,
198
- // the assumption that `cols` can never exceed `isize::MAX` would be incorrect.
199
- // (Consider e.g. `let xs: &[[i32; ::std::usize::MAX]] = &[]`.)
200
- impl_arr_init ! ( 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , ) ;
201
-
202
137
/// Create a two-dimensional array with elements from `xs`.
203
138
///
204
139
/// ```
@@ -210,22 +145,16 @@ impl_arr_init!(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,);
210
145
/// a.shape() == [2, 3]
211
146
/// );
212
147
/// ```
213
- pub fn arr2 < A : Clone , V : FixedInitializer < Elem = A > > ( xs : & [ V ] ) -> Array2 < A >
214
- where
215
- V : Clone ,
216
- {
148
+ pub fn arr2 < A : Clone , const N : usize > ( xs : & [ [ A ; N ] ] ) -> Array2 < A > {
217
149
Array2 :: from ( xs. to_vec ( ) )
218
150
}
219
151
220
- impl < A , V > From < Vec < V > > for Array2 < A >
221
- where
222
- V : FixedInitializer < Elem = A > ,
223
- {
152
+ impl < A , const N : usize > From < Vec < [ A ; N ] > > for Array2 < A > {
224
153
/// Converts the `Vec` of arrays to an owned 2-D array.
225
154
///
226
155
/// **Panics** if the product of non-zero axis lengths overflows `isize`.
227
- fn from ( mut xs : Vec < V > ) -> Self {
228
- let dim = Ix2 ( xs. len ( ) , V :: len ( ) ) ;
156
+ fn from ( mut xs : Vec < [ A ; N ] > ) -> Self {
157
+ let dim = Ix2 ( xs. len ( ) , N ) ;
229
158
let ptr = xs. as_mut_ptr ( ) ;
230
159
let cap = xs. capacity ( ) ;
231
160
let expand_len = dimension:: size_of_shape_checked ( & dim)
@@ -234,29 +163,25 @@ where
234
163
unsafe {
235
164
let v = if size_of :: < A > ( ) == 0 {
236
165
Vec :: from_raw_parts ( ptr as * mut A , expand_len, expand_len)
237
- } else if V :: len ( ) == 0 {
166
+ } else if N == 0 {
238
167
Vec :: new ( )
239
168
} else {
240
169
// Guaranteed not to overflow in this case since A is non-ZST
241
170
// and Vec never allocates more than isize bytes.
242
- let expand_cap = cap * V :: len ( ) ;
171
+ let expand_cap = cap * N ;
243
172
Vec :: from_raw_parts ( ptr as * mut A , expand_len, expand_cap)
244
173
} ;
245
174
ArrayBase :: from_shape_vec_unchecked ( dim, v)
246
175
}
247
176
}
248
177
}
249
178
250
- impl < A , V , U > From < Vec < V > > for Array3 < A >
251
- where
252
- V : FixedInitializer < Elem = U > ,
253
- U : FixedInitializer < Elem = A > ,
254
- {
179
+ impl < A , const N : usize , const M : usize > From < Vec < [ [ A ; M ] ; N ] > > for Array3 < A > {
255
180
/// Converts the `Vec` of arrays to an owned 3-D array.
256
181
///
257
182
/// **Panics** if the product of non-zero axis lengths overflows `isize`.
258
- fn from ( mut xs : Vec < V > ) -> Self {
259
- let dim = Ix3 ( xs. len ( ) , V :: len ( ) , U :: len ( ) ) ;
183
+ fn from ( mut xs : Vec < [ [ A ; M ] ; N ] > ) -> Self {
184
+ let dim = Ix3 ( xs. len ( ) , N , M ) ;
260
185
let ptr = xs. as_mut_ptr ( ) ;
261
186
let cap = xs. capacity ( ) ;
262
187
let expand_len = dimension:: size_of_shape_checked ( & dim)
@@ -265,12 +190,12 @@ where
265
190
unsafe {
266
191
let v = if size_of :: < A > ( ) == 0 {
267
192
Vec :: from_raw_parts ( ptr as * mut A , expand_len, expand_len)
268
- } else if V :: len ( ) == 0 || U :: len ( ) == 0 {
193
+ } else if N == 0 || M == 0 {
269
194
Vec :: new ( )
270
195
} else {
271
196
// Guaranteed not to overflow in this case since A is non-ZST
272
197
// and Vec never allocates more than isize bytes.
273
- let expand_cap = cap * V :: len ( ) * U :: len ( ) ;
198
+ let expand_cap = cap * N * M ;
274
199
Vec :: from_raw_parts ( ptr as * mut A , expand_len, expand_cap)
275
200
} ;
276
201
ArrayBase :: from_shape_vec_unchecked ( dim, v)
@@ -280,7 +205,7 @@ where
280
205
281
206
/// Create a two-dimensional array with elements from `xs`.
282
207
///
283
- pub fn rcarr2 < A : Clone , V : Clone + FixedInitializer < Elem = A > > ( xs : & [ V ] ) -> ArcArray2 < A > {
208
+ pub fn rcarr2 < A : Clone , const N : usize > ( xs : & [ [ A ; N ] ] ) -> ArcArray2 < A > {
284
209
arr2 ( xs) . into_shared ( )
285
210
}
286
211
@@ -301,23 +226,11 @@ pub fn rcarr2<A: Clone, V: Clone + FixedInitializer<Elem = A>>(xs: &[V]) -> ArcA
301
226
/// a.shape() == [3, 2, 2]
302
227
/// );
303
228
/// ```
304
- pub fn arr3 < A : Clone , V : FixedInitializer < Elem = U > , U : FixedInitializer < Elem = A > > (
305
- xs : & [ V ] ,
306
- ) -> Array3 < A >
307
- where
308
- V : Clone ,
309
- U : Clone ,
310
- {
229
+ pub fn arr3 < A : Clone , const N : usize , const M : usize > ( xs : & [ [ [ A ; M ] ; N ] ] ) -> Array3 < A > {
311
230
Array3 :: from ( xs. to_vec ( ) )
312
231
}
313
232
314
233
/// Create a three-dimensional array with elements from `xs`.
315
- pub fn rcarr3 < A : Clone , V : FixedInitializer < Elem = U > , U : FixedInitializer < Elem = A > > (
316
- xs : & [ V ] ,
317
- ) -> ArcArray < A , Ix3 >
318
- where
319
- V : Clone ,
320
- U : Clone ,
321
- {
234
+ pub fn rcarr3 < A : Clone , const N : usize , const M : usize > ( xs : & [ [ [ A ; M ] ; N ] ] ) -> ArcArray < A , Ix3 > {
322
235
arr3 ( xs) . into_shared ( )
323
236
}
0 commit comments