@@ -304,6 +304,10 @@ pub const ARRAY_FORMAT_VERSION: u8 = 1u8;
304304
305305// use "raw" form instead of type aliases here so that they show up in docs
306306/// 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.)
307311impl < ' a , A , Slice : ?Sized > From < & ' a Slice > for ArrayView < ' a , A , Ix1 >
308312where
309313 Slice : AsRef < [ A ] > ,
@@ -312,38 +316,19 @@ where
312316 ///
313317 /// **Panics** if the slice length is greater than `isize::MAX`.
314318 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 ( ) )
323320 }
324321}
325322
326323/// Implementation of ArrayView2::from(&S) where S is a slice to a 2D array
327324///
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).
330328impl < ' a , A , const N : usize > From < & ' a [ [ A ; N ] ] > for ArrayView < ' a , A , Ix2 > {
331329 /// Create a two-dimensional read-only array view of the data in `slice`
332330 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)
347332 }
348333}
349334
0 commit comments