Skip to content

Commit

Permalink
TEST: Update example sort-axis to use MaybeUninit
Browse files Browse the repository at this point in the history
  • Loading branch information
bluss committed Apr 18, 2020
1 parent 1650601 commit dda72c3
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions examples/sort-axis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,26 +102,31 @@ where
assert_eq!(axis_len, perm.indices.len());
debug_assert!(perm.correct());

let mut v = Vec::with_capacity(self.len());
let mut result;
let mut result = Array::maybe_uninit(self.dim());

// panic-critical begin: we must not panic
unsafe {
v.set_len(self.len());
result = Array::from_shape_vec_unchecked(self.dim(), v);
// logically move ownership of all elements from self into result
// the result realizes this ownership at .assume_init() further down
let mut moved_elements = 0;
for i in 0..axis_len {
let perm_i = perm.indices[i];
Zip::from(result.index_axis_mut(axis, perm_i))
.and(self.index_axis(axis, i))
.apply(|to, from| copy_nonoverlapping(from, to, 1));
.apply(|to, from| {
copy_nonoverlapping(from, to.as_mut_ptr(), 1);
moved_elements += 1;
});
}
// forget moved array elements but not its vec
// old_storage drops empty
let mut old_storage = self.into_raw_vec();
old_storage.set_len(0);
// old_storage drops empty

debug_assert_eq!(result.len(), moved_elements);
result.assume_init()
}
// panic-critical end
result
}
}

Expand Down

0 comments on commit dda72c3

Please sign in to comment.