File tree 1 file changed +12
-7
lines changed
1 file changed +12
-7
lines changed Original file line number Diff line number Diff line change @@ -102,26 +102,31 @@ where
102
102
assert_eq ! ( axis_len, perm. indices. len( ) ) ;
103
103
debug_assert ! ( perm. correct( ) ) ;
104
104
105
- let mut v = Vec :: with_capacity ( self . len ( ) ) ;
106
- let mut result;
105
+ let mut result = Array :: maybe_uninit ( self . dim ( ) ) ;
107
106
108
107
// panic-critical begin: we must not panic
109
108
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 ;
112
112
for i in 0 ..axis_len {
113
113
let perm_i = perm. indices [ i] ;
114
114
Zip :: from ( result. index_axis_mut ( axis, perm_i) )
115
115
. 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
+ } ) ;
117
120
}
118
121
// forget moved array elements but not its vec
122
+ // old_storage drops empty
119
123
let mut old_storage = self . into_raw_vec ( ) ;
120
124
old_storage. set_len ( 0 ) ;
121
- // old_storage drops empty
125
+
126
+ debug_assert_eq ! ( result. len( ) , moved_elements) ;
127
+ result. assume_init ( )
122
128
}
123
129
// panic-critical end
124
- result
125
130
}
126
131
}
127
132
You can’t perform that action at this time.
0 commit comments