Make iter::order functions into methods on Iterator
This does cause some breakage due to deficiencies in resolve - `path::Components` is both an `Iterator` and implements `Eq`, `Ord`, etc. If one calls e.g. `partial_cmp` on a `Components` and passes a `&Components` intending to target the `PartialOrd` impl, the compiler will select the `partial_cmp` from `Iterator` and then error out. I doubt anyone will run into breakage from `Components` specifically, but we should see if there are third party types that will run into issues. `iter::order::equals` wasn't moved to `Iterator` since it's exactly the same as `iter::order::eq` but with an `Eq` instead of `PartialEq` bound, which doensn't seem very useful. I also updated `le`, `gt`, etc to use `partial_cmp` which lets us drop the extra `PartialEq` bound. cc #27737
This commit is contained in:
@@ -58,7 +58,7 @@
|
||||
|
||||
use clone::Clone;
|
||||
use cmp;
|
||||
use cmp::{Ord, PartialOrd, PartialEq};
|
||||
use cmp::{Ord, PartialOrd, PartialEq, Ordering};
|
||||
use default::Default;
|
||||
use marker;
|
||||
use mem;
|
||||
@@ -1005,6 +1005,198 @@ pub trait Iterator {
|
||||
{
|
||||
self.fold(One::one(), |p, e| p * e)
|
||||
}
|
||||
|
||||
/// Lexicographically compares the elements of this `Iterator` with those
|
||||
/// of another.
|
||||
#[unstable(feature = "iter_order", reason = "needs review and revision", issue = "27737")]
|
||||
fn cmp<I>(mut self, other: I) -> Ordering where
|
||||
I: IntoIterator<Item = Self::Item>,
|
||||
Self::Item: Ord,
|
||||
Self: Sized,
|
||||
{
|
||||
let mut other = other.into_iter();
|
||||
|
||||
loop {
|
||||
match (self.next(), other.next()) {
|
||||
(None, None) => return Ordering::Equal,
|
||||
(None, _ ) => return Ordering::Less,
|
||||
(_ , None) => return Ordering::Greater,
|
||||
(Some(x), Some(y)) => match x.cmp(&y) {
|
||||
Ordering::Equal => (),
|
||||
non_eq => return non_eq,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Lexicographically compares the elements of this `Iterator` with those
|
||||
/// of another.
|
||||
#[unstable(feature = "iter_order", reason = "needs review and revision", issue = "27737")]
|
||||
fn partial_cmp<I>(mut self, other: I) -> Option<Ordering> where
|
||||
I: IntoIterator,
|
||||
Self::Item: PartialOrd<I::Item>,
|
||||
Self: Sized,
|
||||
{
|
||||
let mut other = other.into_iter();
|
||||
|
||||
loop {
|
||||
match (self.next(), other.next()) {
|
||||
(None, None) => return Some(Ordering::Equal),
|
||||
(None, _ ) => return Some(Ordering::Less),
|
||||
(_ , None) => return Some(Ordering::Greater),
|
||||
(Some(x), Some(y)) => match x.partial_cmp(&y) {
|
||||
Some(Ordering::Equal) => (),
|
||||
non_eq => return non_eq,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Determines if the elements of this `Iterator` are equal to those of
|
||||
/// another.
|
||||
#[unstable(feature = "iter_order", reason = "needs review and revision", issue = "27737")]
|
||||
fn eq<I>(mut self, other: I) -> bool where
|
||||
I: IntoIterator,
|
||||
Self::Item: PartialEq<I::Item>,
|
||||
Self: Sized,
|
||||
{
|
||||
let mut other = other.into_iter();
|
||||
|
||||
loop {
|
||||
match (self.next(), other.next()) {
|
||||
(None, None) => return true,
|
||||
(None, _) | (_, None) => return false,
|
||||
(Some(x), Some(y)) => if x != y { return false },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Determines if the elements of this `Iterator` are unequal to those of
|
||||
/// another.
|
||||
#[unstable(feature = "iter_order", reason = "needs review and revision", issue = "27737")]
|
||||
fn ne<I>(mut self, other: I) -> bool where
|
||||
I: IntoIterator,
|
||||
Self::Item: PartialEq<I::Item>,
|
||||
Self: Sized,
|
||||
{
|
||||
let mut other = other.into_iter();
|
||||
|
||||
loop {
|
||||
match (self.next(), other.next()) {
|
||||
(None, None) => return false,
|
||||
(None, _) | (_, None) => return true,
|
||||
(Some(x), Some(y)) => if x.ne(&y) { return true },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Determines if the elements of this `Iterator` are lexicographically
|
||||
/// less than those of another.
|
||||
#[unstable(feature = "iter_order", reason = "needs review and revision", issue = "27737")]
|
||||
fn lt<I>(mut self, other: I) -> bool where
|
||||
I: IntoIterator,
|
||||
Self::Item: PartialOrd<I::Item>,
|
||||
Self: Sized,
|
||||
{
|
||||
let mut other = other.into_iter();
|
||||
|
||||
loop {
|
||||
match (self.next(), other.next()) {
|
||||
(None, None) => return false,
|
||||
(None, _ ) => return true,
|
||||
(_ , None) => return false,
|
||||
(Some(x), Some(y)) => {
|
||||
match x.partial_cmp(&y) {
|
||||
Some(Ordering::Less) => return true,
|
||||
Some(Ordering::Equal) => {}
|
||||
Some(Ordering::Greater) => return false,
|
||||
None => return false,
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Determines if the elements of this `Iterator` are lexicographically
|
||||
/// less or equal to those of another.
|
||||
#[unstable(feature = "iter_order", reason = "needs review and revision", issue = "27737")]
|
||||
fn le<I>(mut self, other: I) -> bool where
|
||||
I: IntoIterator,
|
||||
Self::Item: PartialOrd<I::Item>,
|
||||
Self: Sized,
|
||||
{
|
||||
let mut other = other.into_iter();
|
||||
|
||||
loop {
|
||||
match (self.next(), other.next()) {
|
||||
(None, None) => return true,
|
||||
(None, _ ) => return true,
|
||||
(_ , None) => return false,
|
||||
(Some(x), Some(y)) => {
|
||||
match x.partial_cmp(&y) {
|
||||
Some(Ordering::Less) => return true,
|
||||
Some(Ordering::Equal) => {}
|
||||
Some(Ordering::Greater) => return false,
|
||||
None => return false,
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Determines if the elements of this `Iterator` are lexicographically
|
||||
/// greater than those of another.
|
||||
#[unstable(feature = "iter_order", reason = "needs review and revision", issue = "27737")]
|
||||
fn gt<I>(mut self, other: I) -> bool where
|
||||
I: IntoIterator,
|
||||
Self::Item: PartialOrd<I::Item>,
|
||||
Self: Sized,
|
||||
{
|
||||
let mut other = other.into_iter();
|
||||
|
||||
loop {
|
||||
match (self.next(), other.next()) {
|
||||
(None, None) => return false,
|
||||
(None, _ ) => return false,
|
||||
(_ , None) => return true,
|
||||
(Some(x), Some(y)) => {
|
||||
match x.partial_cmp(&y) {
|
||||
Some(Ordering::Less) => return false,
|
||||
Some(Ordering::Equal) => {}
|
||||
Some(Ordering::Greater) => return true,
|
||||
None => return false,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Determines if the elements of this `Iterator` are lexicographically
|
||||
/// greater than or equal to those of another.
|
||||
#[unstable(feature = "iter_order", reason = "needs review and revision", issue = "27737")]
|
||||
fn ge<I>(mut self, other: I) -> bool where
|
||||
I: IntoIterator,
|
||||
Self::Item: PartialOrd<I::Item>,
|
||||
Self: Sized,
|
||||
{
|
||||
let mut other = other.into_iter();
|
||||
|
||||
loop {
|
||||
match (self.next(), other.next()) {
|
||||
(None, None) => return true,
|
||||
(None, _ ) => return false,
|
||||
(_ , None) => return true,
|
||||
(Some(x), Some(y)) => {
|
||||
match x.partial_cmp(&y) {
|
||||
Some(Ordering::Less) => return false,
|
||||
Some(Ordering::Equal) => {}
|
||||
Some(Ordering::Greater) => return true,
|
||||
None => return false,
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Select an element from an iterator based on the given projection
|
||||
@@ -2654,146 +2846,79 @@ pub fn once<T>(value: T) -> Once<T> {
|
||||
///
|
||||
/// If two sequences are equal up until the point where one ends,
|
||||
/// the shorter sequence compares less.
|
||||
#[deprecated(since = "1.4.0", reason = "use the equivalent methods on `Iterator` instead")]
|
||||
#[unstable(feature = "iter_order", reason = "needs review and revision",
|
||||
issue = "27737")]
|
||||
pub mod order {
|
||||
use cmp;
|
||||
use cmp::{Eq, Ord, PartialOrd, PartialEq};
|
||||
use cmp::Ordering::{Equal, Less, Greater};
|
||||
use option::Option;
|
||||
use option::Option::{Some, None};
|
||||
use super::Iterator;
|
||||
|
||||
/// Compare `a` and `b` for equality using `Eq`
|
||||
pub fn equals<A, L, R>(mut a: L, mut b: R) -> bool where
|
||||
pub fn equals<A, L, R>(a: L, b: R) -> bool where
|
||||
A: Eq,
|
||||
L: Iterator<Item=A>,
|
||||
R: Iterator<Item=A>,
|
||||
{
|
||||
loop {
|
||||
match (a.next(), b.next()) {
|
||||
(None, None) => return true,
|
||||
(None, _) | (_, None) => return false,
|
||||
(Some(x), Some(y)) => if x != y { return false },
|
||||
}
|
||||
}
|
||||
a.eq(b)
|
||||
}
|
||||
|
||||
/// Order `a` and `b` lexicographically using `Ord`
|
||||
pub fn cmp<A, L, R>(mut a: L, mut b: R) -> cmp::Ordering where
|
||||
pub fn cmp<A, L, R>(a: L, b: R) -> cmp::Ordering where
|
||||
A: Ord,
|
||||
L: Iterator<Item=A>,
|
||||
R: Iterator<Item=A>,
|
||||
{
|
||||
loop {
|
||||
match (a.next(), b.next()) {
|
||||
(None, None) => return Equal,
|
||||
(None, _ ) => return Less,
|
||||
(_ , None) => return Greater,
|
||||
(Some(x), Some(y)) => match x.cmp(&y) {
|
||||
Equal => (),
|
||||
non_eq => return non_eq,
|
||||
},
|
||||
}
|
||||
}
|
||||
a.cmp(b)
|
||||
}
|
||||
|
||||
/// Order `a` and `b` lexicographically using `PartialOrd`
|
||||
pub fn partial_cmp<L: Iterator, R: Iterator>(mut a: L, mut b: R) -> Option<cmp::Ordering> where
|
||||
pub fn partial_cmp<L: Iterator, R: Iterator>(a: L, b: R) -> Option<cmp::Ordering> where
|
||||
L::Item: PartialOrd<R::Item>
|
||||
{
|
||||
loop {
|
||||
match (a.next(), b.next()) {
|
||||
(None, None) => return Some(Equal),
|
||||
(None, _ ) => return Some(Less),
|
||||
(_ , None) => return Some(Greater),
|
||||
(Some(x), Some(y)) => match x.partial_cmp(&y) {
|
||||
Some(Equal) => (),
|
||||
non_eq => return non_eq,
|
||||
},
|
||||
}
|
||||
}
|
||||
a.partial_cmp(b)
|
||||
}
|
||||
|
||||
/// Compare `a` and `b` for equality (Using partial equality, `PartialEq`)
|
||||
pub fn eq<L: Iterator, R: Iterator>(mut a: L, mut b: R) -> bool where
|
||||
pub fn eq<L: Iterator, R: Iterator>(a: L, b: R) -> bool where
|
||||
L::Item: PartialEq<R::Item>,
|
||||
{
|
||||
loop {
|
||||
match (a.next(), b.next()) {
|
||||
(None, None) => return true,
|
||||
(None, _) | (_, None) => return false,
|
||||
(Some(x), Some(y)) => if !x.eq(&y) { return false },
|
||||
}
|
||||
}
|
||||
a.eq(b)
|
||||
}
|
||||
|
||||
/// Compares `a` and `b` for nonequality (Using partial equality, `PartialEq`)
|
||||
pub fn ne<L: Iterator, R: Iterator>(mut a: L, mut b: R) -> bool where
|
||||
pub fn ne<L: Iterator, R: Iterator>(a: L, b: R) -> bool where
|
||||
L::Item: PartialEq<R::Item>,
|
||||
{
|
||||
loop {
|
||||
match (a.next(), b.next()) {
|
||||
(None, None) => return false,
|
||||
(None, _) | (_, None) => return true,
|
||||
(Some(x), Some(y)) => if x.ne(&y) { return true },
|
||||
}
|
||||
}
|
||||
a.ne(b)
|
||||
}
|
||||
|
||||
/// Returns `a` < `b` lexicographically (Using partial order, `PartialOrd`)
|
||||
pub fn lt<L: Iterator, R: Iterator>(mut a: L, mut b: R) -> bool where
|
||||
pub fn lt<L: Iterator, R: Iterator>(a: L, b: R) -> bool where
|
||||
L::Item: PartialOrd<R::Item>,
|
||||
{
|
||||
loop {
|
||||
match (a.next(), b.next()) {
|
||||
(None, None) => return false,
|
||||
(None, _ ) => return true,
|
||||
(_ , None) => return false,
|
||||
(Some(x), Some(y)) => if x.ne(&y) { return x.lt(&y) },
|
||||
}
|
||||
}
|
||||
a.lt(b)
|
||||
}
|
||||
|
||||
/// Returns `a` <= `b` lexicographically (Using partial order, `PartialOrd`)
|
||||
pub fn le<L: Iterator, R: Iterator>(mut a: L, mut b: R) -> bool where
|
||||
pub fn le<L: Iterator, R: Iterator>(a: L, b: R) -> bool where
|
||||
L::Item: PartialOrd<R::Item>,
|
||||
{
|
||||
loop {
|
||||
match (a.next(), b.next()) {
|
||||
(None, None) => return true,
|
||||
(None, _ ) => return true,
|
||||
(_ , None) => return false,
|
||||
(Some(x), Some(y)) => if x.ne(&y) { return x.le(&y) },
|
||||
}
|
||||
}
|
||||
a.le(b)
|
||||
}
|
||||
|
||||
/// Returns `a` > `b` lexicographically (Using partial order, `PartialOrd`)
|
||||
pub fn gt<L: Iterator, R: Iterator>(mut a: L, mut b: R) -> bool where
|
||||
pub fn gt<L: Iterator, R: Iterator>(a: L, b: R) -> bool where
|
||||
L::Item: PartialOrd<R::Item>,
|
||||
{
|
||||
loop {
|
||||
match (a.next(), b.next()) {
|
||||
(None, None) => return false,
|
||||
(None, _ ) => return false,
|
||||
(_ , None) => return true,
|
||||
(Some(x), Some(y)) => if x.ne(&y) { return x.gt(&y) },
|
||||
}
|
||||
}
|
||||
a.gt(b)
|
||||
}
|
||||
|
||||
/// Returns `a` >= `b` lexicographically (Using partial order, `PartialOrd`)
|
||||
pub fn ge<L: Iterator, R: Iterator>(mut a: L, mut b: R) -> bool where
|
||||
pub fn ge<L: Iterator, R: Iterator>(a: L, b: R) -> bool where
|
||||
L::Item: PartialOrd<R::Item>,
|
||||
{
|
||||
loop {
|
||||
match (a.next(), b.next()) {
|
||||
(None, None) => return true,
|
||||
(None, _ ) => return false,
|
||||
(_ , None) => return true,
|
||||
(Some(x), Some(y)) => if x.ne(&y) { return x.ge(&y) },
|
||||
}
|
||||
}
|
||||
a.ge(b)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -448,12 +448,10 @@ macro_rules! define_bignum {
|
||||
impl ::cmp::Ord for $name {
|
||||
fn cmp(&self, other: &$name) -> ::cmp::Ordering {
|
||||
use cmp::max;
|
||||
use iter::order;
|
||||
|
||||
let sz = max(self.size, other.size);
|
||||
let lhs = self.base[..sz].iter().cloned().rev();
|
||||
let rhs = other.base[..sz].iter().cloned().rev();
|
||||
order::cmp(lhs, rhs)
|
||||
lhs.cmp(rhs)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1557,7 +1557,7 @@ impl<T: Eq> Eq for [T] {}
|
||||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
impl<T: Ord> Ord for [T] {
|
||||
fn cmp(&self, other: &[T]) -> Ordering {
|
||||
order::cmp(self.iter(), other.iter())
|
||||
self.iter().cmp(other.iter())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1565,22 +1565,22 @@ impl<T: Ord> Ord for [T] {
|
||||
impl<T: PartialOrd> PartialOrd for [T] {
|
||||
#[inline]
|
||||
fn partial_cmp(&self, other: &[T]) -> Option<Ordering> {
|
||||
order::partial_cmp(self.iter(), other.iter())
|
||||
self.iter().partial_cmp(other.iter())
|
||||
}
|
||||
#[inline]
|
||||
fn lt(&self, other: &[T]) -> bool {
|
||||
order::lt(self.iter(), other.iter())
|
||||
self.iter().lt(other.iter())
|
||||
}
|
||||
#[inline]
|
||||
fn le(&self, other: &[T]) -> bool {
|
||||
order::le(self.iter(), other.iter())
|
||||
self.iter().le(other.iter())
|
||||
}
|
||||
#[inline]
|
||||
fn ge(&self, other: &[T]) -> bool {
|
||||
order::ge(self.iter(), other.iter())
|
||||
self.iter().ge(other.iter())
|
||||
}
|
||||
#[inline]
|
||||
fn gt(&self, other: &[T]) -> bool {
|
||||
order::gt(self.iter(), other.iter())
|
||||
self.iter().gt(other.iter())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user