@@ -304,6 +304,10 @@ pub const ARRAY_FORMAT_VERSION: u8 = 1u8;
304
304
305
305
// use "raw" form instead of type aliases here so that they show up in docs
306
306
/// Implementation of `ArrayView::from(&S)` where `S` is a slice or slicable.
307
+ ///
308
+ /// **Panics** if the length of the slice overflows `isize`. (This can only
309
+ /// occur if `A` is zero-sized, because slices cannot contain more than
310
+ /// `isize::MAX` number of bytes.)
307
311
impl < ' a , A , Slice : ?Sized > From < & ' a Slice > for ArrayView < ' a , A , Ix1 >
308
312
where
309
313
Slice : AsRef < [ A ] > ,
@@ -312,38 +316,19 @@ where
312
316
///
313
317
/// **Panics** if the slice length is greater than `isize::MAX`.
314
318
fn from ( slice : & ' a Slice ) -> Self {
315
- let xs = slice. as_ref ( ) ;
316
- if mem:: size_of :: < A > ( ) == 0 {
317
- assert ! (
318
- xs. len( ) <= :: std:: isize :: MAX as usize ,
319
- "Slice length must fit in `isize`." ,
320
- ) ;
321
- }
322
- unsafe { Self :: from_shape_ptr ( xs. len ( ) , xs. as_ptr ( ) ) }
319
+ aview1 ( slice. as_ref ( ) )
323
320
}
324
321
}
325
322
326
323
/// Implementation of ArrayView2::from(&S) where S is a slice to a 2D array
327
324
///
328
- /// **Panics** if the product of non-zero axis lengths overflows `isize` (This can only occur if A
329
- /// is zero-sized because slices cannot contain more than `isize::MAX` number of bytes).
325
+ /// **Panics** if the product of non-zero axis lengths overflows `isize` (This
326
+ /// can only occur if A is zero-sized or if `N` is zero, because slices cannot
327
+ /// contain more than `isize::MAX` number of bytes).
330
328
impl < ' a , A , const N : usize > From < & ' a [ [ A ; N ] ] > for ArrayView < ' a , A , Ix2 > {
331
329
/// Create a two-dimensional read-only array view of the data in `slice`
332
330
fn from ( xs : & ' a [ [ A ; N ] ] ) -> Self {
333
- let cols = N ;
334
- let rows = xs. len ( ) ;
335
- let dim = Ix2 ( rows, cols) ;
336
- if size_of :: < A > ( ) == 0 {
337
- dimension:: size_of_shape_checked ( & dim)
338
- . expect ( "Product of non-zero axis lengths must not overflow isize." ) ;
339
- }
340
-
341
- // `cols * rows` is guaranteed to fit in `isize` because we checked that it fits in
342
- // `isize::MAX`
343
- unsafe {
344
- let data = slice:: from_raw_parts ( xs. as_ptr ( ) as * const A , cols * rows) ;
345
- ArrayView :: from_shape_ptr ( dim, data. as_ptr ( ) )
346
- }
331
+ aview2 ( xs)
347
332
}
348
333
}
349
334
0 commit comments