@@ -1642,14 +1642,8 @@ where
16421642 A : Clone ,
16431643 S : Data ,
16441644 {
1645- if size_of_shape_checked ( & shape) != Ok ( self . dim . size ( ) ) {
1646- return Err ( error:: incompatible_shapes ( & self . dim , & shape) ) ;
1647- }
1648- let layout = self . layout_impl ( ) ;
1649- if order == Order :: Automatic {
1650- order = preferred_order_for_layout ( layout) ;
1651- }
1652-
1645+ let layout = self . layout_and_order ( & shape, & mut order) ?;
1646+ // safe because: the number of elements is preserved and it's contiguous
16531647 unsafe {
16541648 if layout. is ( Layout :: CORDER ) && order == Order :: RowMajor {
16551649 let strides = shape. default_strides ( ) ;
@@ -1672,6 +1666,24 @@ where
16721666 }
16731667 }
16741668
1669+ /// Check if `shape` is valid for reshaping the array (in terms of number of elements),
1670+ /// and also compute the Layout of self and resolve Order if it is Automatic.
1671+ ///
1672+ /// After returning successfully, `order` is either RowMajor or ColumnMajor.
1673+ fn layout_and_order < E > ( & self , shape : & E , order : & mut Order ) -> Result < Layout , ShapeError >
1674+ where
1675+ E : Dimension ,
1676+ {
1677+ if size_of_shape_checked ( shape) != Ok ( self . dim . size ( ) ) {
1678+ return Err ( error:: incompatible_shapes ( & self . dim , shape) ) ;
1679+ }
1680+ let layout = self . layout_impl ( ) ;
1681+ if * order == Order :: Automatic {
1682+ * order = preferred_order_for_layout ( layout) ;
1683+ }
1684+ Ok ( layout)
1685+ }
1686+
16751687 /// Transform the array into `shape`; any shape with the same number of elements is accepted,
16761688 /// but the source array or view must be in contiguous and stored in standard row-major (C) or
16771689 /// column-major (Fortran) memory order.
@@ -1707,15 +1719,7 @@ where
17071719 where
17081720 E : Dimension ,
17091721 {
1710- if size_of_shape_checked ( & shape) != Ok ( self . dim . size ( ) ) {
1711- return Err ( error:: incompatible_shapes ( & self . dim , & shape) ) ;
1712- }
1713-
1714- let layout = self . layout_impl ( ) ;
1715- if order == Order :: Automatic {
1716- order = preferred_order_for_layout ( layout) ;
1717- }
1718-
1722+ let layout = self . layout_and_order ( & shape, & mut order) ?;
17191723 // safe because: the number of elements is preserved and it's contiguous
17201724 unsafe {
17211725 if layout. is ( Layout :: CORDER ) && order == Order :: RowMajor {
@@ -1769,15 +1773,7 @@ where
17691773 where
17701774 E : Dimension ,
17711775 {
1772- if size_of_shape_checked ( & shape) != Ok ( self . dim . size ( ) ) {
1773- return Err ( error:: incompatible_shapes ( & self . dim , & shape) ) ;
1774- }
1775-
1776- let layout = self . layout_impl ( ) ;
1777- if order == Order :: Automatic {
1778- order = preferred_order_for_layout ( layout) ;
1779- }
1780-
1776+ let layout = self . layout_and_order ( & shape, & mut order) ?;
17811777 // safe because: the number of elements is preserved and it's contiguous
17821778 unsafe {
17831779 if layout. is ( Layout :: CORDER ) && order == Order :: RowMajor {
0 commit comments