Skip to content

Commit e5d4bf7

Browse files
authored
Merge pull request #958 from rust-ndarray/partial-eq-with-reference
Implement PartialEq for array == &array and &array == array
2 parents 3d7df5c + b1b5a0e commit e5d4bf7

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

RELEASES.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
Version 0.15.1 (not released yet)
2+
===========================
3+
4+
Enhancements
5+
------------
6+
7+
- Arrays and views now implement PartialEq so that it's possible to compare
8+
arrays with references to arrays and vice versa by [@bluss]
9+
10+
111
Version 0.15.0 (2021-03-25)
212
===========================
313

src/arraytraits.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,34 @@ where
112112
}
113113
}
114114

115+
/// Return `true` if the array shapes and all elements of `self` and
116+
/// `rhs` are equal. Return `false` otherwise.
117+
impl<'a, A, B, S, S2, D> PartialEq<&'a ArrayBase<S2, D>> for ArrayBase<S, D>
118+
where
119+
A: PartialEq<B>,
120+
S: Data<Elem = A>,
121+
S2: Data<Elem = B>,
122+
D: Dimension,
123+
{
124+
fn eq(&self, rhs: &&ArrayBase<S2, D>) -> bool {
125+
*self == **rhs
126+
}
127+
}
128+
129+
/// Return `true` if the array shapes and all elements of `self` and
130+
/// `rhs` are equal. Return `false` otherwise.
131+
impl<'a, A, B, S, S2, D> PartialEq<ArrayBase<S2, D>> for &'a ArrayBase<S, D>
132+
where
133+
A: PartialEq<B>,
134+
S: Data<Elem = A>,
135+
S2: Data<Elem = B>,
136+
D: Dimension,
137+
{
138+
fn eq(&self, rhs: &ArrayBase<S2, D>) -> bool {
139+
**self == *rhs
140+
}
141+
}
142+
115143
impl<S, D> Eq for ArrayBase<S, D>
116144
where
117145
D: Dimension,

tests/windows.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,22 +133,22 @@ fn test_window_neg_stride() {
133133

134134
itertools::assert_equal(
135135
array.slice(s![.., ..;-1]).windows((2, 2)),
136-
answer.iter().map(|a| a.view())
136+
answer.iter()
137137
);
138138

139139
answer.invert_axis(Axis(0));
140140
answer.map_inplace(|a| a.invert_axis(Axis(0)));
141141

142142
itertools::assert_equal(
143143
array.slice(s![..;-1, ..;-1]).windows((2, 2)),
144-
answer.iter().map(|a| a.view())
144+
answer.iter()
145145
);
146146

147147
answer.invert_axis(Axis(1));
148148
answer.map_inplace(|a| a.invert_axis(Axis(1)));
149149

150150
itertools::assert_equal(
151151
array.slice(s![..;-1, ..]).windows((2, 2)),
152-
answer.iter().map(|a| a.view())
152+
answer.iter()
153153
);
154154
}

0 commit comments

Comments
 (0)