@@ -307,6 +307,10 @@ pub const ARRAY_FORMAT_VERSION: u8 = 1u8;
307307
308308// use "raw" form instead of type aliases here so that they show up in docs
309309/// 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.)
310314impl < ' a , A , Slice : ?Sized > From < & ' a Slice > for ArrayView < ' a , A , Ix1 >
311315where
312316 Slice : AsRef < [ A ] > ,
@@ -315,38 +319,19 @@ where
315319 ///
316320 /// **Panics** if the slice length is greater than `isize::MAX`.
317321 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 ( ) )
326323 }
327324}
328325
329326/// Implementation of ArrayView2::from(&S) where S is a slice to a 2D array
330327///
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).
333331impl < ' a , A , const N : usize > From < & ' a [ [ A ; N ] ] > for ArrayView < ' a , A , Ix2 > {
334332 /// Create a two-dimensional read-only array view of the data in `slice`
335333 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)
350335 }
351336}
352337
0 commit comments