Skip to content

Commit 39ccd4a

Browse files
authored
Merge pull request #546 from rust-ndarray/fold-axis-check-axis
fold axis: Include panic on axis oob in doc
2 parents 8ed5720 + 560604b commit 39ccd4a

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

src/dimension/remove_axis.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ pub trait RemoveAxis : Dimension {
1919

2020
impl RemoveAxis for Dim<[Ix; 1]> {
2121
#[inline]
22-
fn remove_axis(&self, _: Axis) -> Ix0 { Ix0() }
22+
fn remove_axis(&self, axis: Axis) -> Ix0 {
23+
debug_assert!(axis.index() < self.ndim());
24+
Ix0()
25+
}
2326
}
2427

2528
impl RemoveAxis for Dim<[Ix; 2]> {
@@ -38,6 +41,7 @@ macro_rules! impl_remove_axis_array(
3841
{
3942
#[inline]
4043
fn remove_axis(&self, axis: Axis) -> Self::Smaller {
44+
debug_assert!(axis.index() < self.ndim());
4145
let mut tup = Dim([0; $n - 1]);
4246
{
4347
let mut it = tup.slice_mut().iter_mut();

src/impl_methods.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,6 +1849,8 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
18491849
/// function and initial value `init`.
18501850
///
18511851
/// Return the result as an `Array`.
1852+
///
1853+
/// **Panics** if `axis` is out of bounds.
18521854
pub fn fold_axis<B, F>(&self, axis: Axis, init: B, mut fold: F)
18531855
-> Array<B, D::Smaller>
18541856
where D: RemoveAxis,

tests/higher_order_f.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
extern crate ndarray;
3+
4+
use ndarray::prelude::*;
5+
6+
#[test]
7+
#[should_panic]
8+
fn test_fold_axis_oob() {
9+
let a = arr2(&[[1., 2.], [3., 4.]]);
10+
a.fold_axis(Axis(2), 0., |x, y| x + y);
11+
}

0 commit comments

Comments
 (0)