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