File tree Expand file tree Collapse file tree 1 file changed +12
-7
lines changed Expand file tree Collapse file tree 1 file changed +12
-7
lines changed Original file line number Diff line number Diff line change @@ -102,26 +102,31 @@ where
102102 assert_eq ! ( axis_len, perm. indices. len( ) ) ;
103103 debug_assert ! ( perm. correct( ) ) ;
104104
105- let mut v = Vec :: with_capacity ( self . len ( ) ) ;
106- let mut result;
105+ let mut result = Array :: maybe_uninit ( self . dim ( ) ) ;
107106
108107 // panic-critical begin: we must not panic
109108 unsafe {
110- v. set_len ( self . len ( ) ) ;
111- result = Array :: from_shape_vec_unchecked ( self . dim ( ) , v) ;
109+ // logically move ownership of all elements from self into result
110+ // the result realizes this ownership at .assume_init() further down
111+ let mut moved_elements = 0 ;
112112 for i in 0 ..axis_len {
113113 let perm_i = perm. indices [ i] ;
114114 Zip :: from ( result. index_axis_mut ( axis, perm_i) )
115115 . and ( self . index_axis ( axis, i) )
116- . apply ( |to, from| copy_nonoverlapping ( from, to, 1 ) ) ;
116+ . apply ( |to, from| {
117+ copy_nonoverlapping ( from, to. as_mut_ptr ( ) , 1 ) ;
118+ moved_elements += 1 ;
119+ } ) ;
117120 }
118121 // forget moved array elements but not its vec
122+ // old_storage drops empty
119123 let mut old_storage = self . into_raw_vec ( ) ;
120124 old_storage. set_len ( 0 ) ;
121- // old_storage drops empty
125+
126+ debug_assert_eq ! ( result. len( ) , moved_elements) ;
127+ result. assume_init ( )
122128 }
123129 // panic-critical end
124- result
125130 }
126131}
127132
You can’t perform that action at this time.
0 commit comments