@@ -1642,14 +1642,8 @@ where
1642
1642
A : Clone ,
1643
1643
S : Data ,
1644
1644
{
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
1653
1647
unsafe {
1654
1648
if layout. is ( Layout :: CORDER ) && order == Order :: RowMajor {
1655
1649
let strides = shape. default_strides ( ) ;
@@ -1672,6 +1666,24 @@ where
1672
1666
}
1673
1667
}
1674
1668
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
+
1675
1687
/// Transform the array into `shape`; any shape with the same number of elements is accepted,
1676
1688
/// but the source array or view must be in contiguous and stored in standard row-major (C) or
1677
1689
/// column-major (Fortran) memory order.
@@ -1707,15 +1719,7 @@ where
1707
1719
where
1708
1720
E : Dimension ,
1709
1721
{
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) ?;
1719
1723
// safe because: the number of elements is preserved and it's contiguous
1720
1724
unsafe {
1721
1725
if layout. is ( Layout :: CORDER ) && order == Order :: RowMajor {
@@ -1769,15 +1773,7 @@ where
1769
1773
where
1770
1774
E : Dimension ,
1771
1775
{
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) ?;
1781
1777
// safe because: the number of elements is preserved and it's contiguous
1782
1778
unsafe {
1783
1779
if layout. is ( Layout :: CORDER ) && order == Order :: RowMajor {
0 commit comments