Skip to content

Commit 5c541a3

Browse files
committed
Make do_collapse_axis return offset instead of performing it
Otherwise, `do_collapse_axis` should really be an `unsafe` function.
1 parent 85c694d commit 5c541a3

File tree

2 files changed

+6
-14
lines changed

2 files changed

+6
-14
lines changed

src/dimension/mod.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -302,24 +302,20 @@ impl<'a> DimensionExt for [Ix]
302302
///
303303
/// **Panics** if `index` is larger than the size of the axis
304304
// FIXME: Move to Dimension trait
305-
pub fn do_collapse_axis<A, D: Dimension>(
305+
pub fn do_collapse_axis<D: Dimension>(
306306
dims: &mut D,
307-
ptr: &mut *mut A,
308307
strides: &D,
309308
axis: usize,
310309
index: usize,
311-
) {
310+
) -> isize {
312311
let dim = dims.slice()[axis];
313312
let stride = strides.slice()[axis];
314313
ndassert!(index < dim,
315314
"collapse_axis: Index {} must be less than axis length {} for \
316315
array with shape {:?}",
317316
index, dim, *dims);
318317
dims.slice_mut()[axis] = 1;
319-
let off = stride_offset(index, stride);
320-
unsafe {
321-
*ptr = ptr.offset(off);
322-
}
318+
stride_offset(index, stride)
323319
}
324320

325321
/// Compute the equivalent unsigned index given the axis length and signed index.

src/impl_methods.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -635,13 +635,9 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
635635
///
636636
/// **Panics** if `axis` or `index` is out of bounds.
637637
pub fn collapse_axis(&mut self, axis: Axis, index: usize) {
638-
dimension::do_collapse_axis(
639-
&mut self.dim,
640-
&mut self.ptr,
641-
&self.strides,
642-
axis.index(),
643-
index,
644-
)
638+
let offset = dimension::do_collapse_axis(&mut self.dim, &self.strides, axis.index(), index);
639+
self.ptr = unsafe { self.ptr.offset(offset) };
640+
debug_assert!(self.pointer_is_inbounds());
645641
}
646642

647643
/// Along `axis`, select the subview `index` and return a

0 commit comments

Comments
 (0)