Skip to content

Commit 355b153

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 d1b4027 commit 355b153

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
@@ -646,13 +646,9 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
646646
///
647647
/// **Panics** if `axis` or `index` is out of bounds.
648648
pub fn collapse_axis(&mut self, axis: Axis, index: usize) {
649-
dimension::do_collapse_axis(
650-
&mut self.dim,
651-
&mut self.ptr,
652-
&self.strides,
653-
axis.index(),
654-
index,
655-
)
649+
let offset = dimension::do_collapse_axis(&mut self.dim, &self.strides, axis.index(), index);
650+
self.ptr = unsafe { self.ptr.offset(offset) };
651+
debug_assert!(self.pointer_is_inbounds());
656652
}
657653

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

0 commit comments

Comments
 (0)