Skip to content

Commit ec8ec54

Browse files
committed
auto merge of #14289 : TyOverby/rust/master, r=alexcrichton
Closes #14278. Previously the type signatures of the ordering functions in `core::iter::order` took two iterators, but only if they were the same type of iterator. This commit loosens that restriction and allows different kinds of iterators (but with the same type of elements) to be compared.
2 parents ffe2686 + 3001450 commit ec8ec54

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/libcore/iter.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -2184,7 +2184,7 @@ pub mod order {
21842184
use super::Iterator;
21852185

21862186
/// Compare `a` and `b` for equality using `TotalEq`
2187-
pub fn equals<A: TotalEq, T: Iterator<A>>(mut a: T, mut b: T) -> bool {
2187+
pub fn equals<A: TotalEq, T: Iterator<A>, S: Iterator<A>>(mut a: T, mut b: S) -> bool {
21882188
loop {
21892189
match (a.next(), b.next()) {
21902190
(None, None) => return true,
@@ -2195,7 +2195,7 @@ pub mod order {
21952195
}
21962196

21972197
/// Order `a` and `b` lexicographically using `TotalOrd`
2198-
pub fn cmp<A: TotalOrd, T: Iterator<A>>(mut a: T, mut b: T) -> cmp::Ordering {
2198+
pub fn cmp<A: TotalOrd, T: Iterator<A>, S: Iterator<A>>(mut a: T, mut b: S) -> cmp::Ordering {
21992199
loop {
22002200
match (a.next(), b.next()) {
22012201
(None, None) => return cmp::Equal,
@@ -2210,7 +2210,7 @@ pub mod order {
22102210
}
22112211

22122212
/// Compare `a` and `b` for equality (Using partial equality, `Eq`)
2213-
pub fn eq<A: Eq, T: Iterator<A>>(mut a: T, mut b: T) -> bool {
2213+
pub fn eq<A: Eq, T: Iterator<A>, S: Iterator<A>>(mut a: T, mut b: S) -> bool {
22142214
loop {
22152215
match (a.next(), b.next()) {
22162216
(None, None) => return true,
@@ -2221,7 +2221,7 @@ pub mod order {
22212221
}
22222222

22232223
/// Compare `a` and `b` for nonequality (Using partial equality, `Eq`)
2224-
pub fn ne<A: Eq, T: Iterator<A>>(mut a: T, mut b: T) -> bool {
2224+
pub fn ne<A: Eq, T: Iterator<A>, S: Iterator<A>>(mut a: T, mut b: S) -> bool {
22252225
loop {
22262226
match (a.next(), b.next()) {
22272227
(None, None) => return false,
@@ -2232,7 +2232,7 @@ pub mod order {
22322232
}
22332233

22342234
/// Return `a` < `b` lexicographically (Using partial order, `Ord`)
2235-
pub fn lt<A: Ord, T: Iterator<A>>(mut a: T, mut b: T) -> bool {
2235+
pub fn lt<A: Ord, T: Iterator<A>, S: Iterator<A>>(mut a: T, mut b: S) -> bool {
22362236
loop {
22372237
match (a.next(), b.next()) {
22382238
(None, None) => return false,
@@ -2244,7 +2244,7 @@ pub mod order {
22442244
}
22452245

22462246
/// Return `a` <= `b` lexicographically (Using partial order, `Ord`)
2247-
pub fn le<A: Ord, T: Iterator<A>>(mut a: T, mut b: T) -> bool {
2247+
pub fn le<A: Ord, T: Iterator<A>, S: Iterator<A>>(mut a: T, mut b: S) -> bool {
22482248
loop {
22492249
match (a.next(), b.next()) {
22502250
(None, None) => return true,
@@ -2256,7 +2256,7 @@ pub mod order {
22562256
}
22572257

22582258
/// Return `a` > `b` lexicographically (Using partial order, `Ord`)
2259-
pub fn gt<A: Ord, T: Iterator<A>>(mut a: T, mut b: T) -> bool {
2259+
pub fn gt<A: Ord, T: Iterator<A>, S: Iterator<A>>(mut a: T, mut b: S) -> bool {
22602260
loop {
22612261
match (a.next(), b.next()) {
22622262
(None, None) => return false,
@@ -2268,7 +2268,7 @@ pub mod order {
22682268
}
22692269

22702270
/// Return `a` >= `b` lexicographically (Using partial order, `Ord`)
2271-
pub fn ge<A: Ord, T: Iterator<A>>(mut a: T, mut b: T) -> bool {
2271+
pub fn ge<A: Ord, T: Iterator<A>, S: Iterator<A>>(mut a: T, mut b: S) -> bool {
22722272
loop {
22732273
match (a.next(), b.next()) {
22742274
(None, None) => return true,
@@ -2325,6 +2325,16 @@ pub mod order {
23252325
assert!(gt(c.iter(), b.iter()) == (c[0] > b[0]));
23262326
assert!(ge(c.iter(), b.iter()) == (c[0] >= b[0]));
23272327
}
2328+
2329+
#[test]
2330+
fn test_multi_iter() {
2331+
use slice::ImmutableVector;
2332+
use iter::DoubleEndedIterator;
2333+
let xs = [1i,2,3,4];
2334+
let ys = [4i,3,2,1];
2335+
assert!(eq(xs.iter(), ys.iter().rev()));
2336+
assert!(lt(xs.iter(), xs.iter().skip(2)));
2337+
}
23282338
}
23292339

23302340
#[cfg(test)]

0 commit comments

Comments
 (0)