提交 651c42f1 编写于 作者: S Steven Fackler

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
上级 63ba780f
......@@ -22,7 +22,7 @@
use core::hash::{Hash, Hasher};
use core::iter::{Map, FromIterator};
use core::ops::Index;
use core::{iter, fmt, mem, usize};
use core::{fmt, mem, usize};
use Bound::{self, Included, Excluded, Unbounded};
use borrow::Borrow;
......@@ -915,7 +915,7 @@ impl<K: Eq, V: Eq> Eq for BTreeMap<K, V> {}
impl<K: PartialOrd, V: PartialOrd> PartialOrd for BTreeMap<K, V> {
#[inline]
fn partial_cmp(&self, other: &BTreeMap<K, V>) -> Option<Ordering> {
iter::order::partial_cmp(self.iter(), other.iter())
self.iter().partial_cmp(other.iter())
}
}
......@@ -923,7 +923,7 @@ fn partial_cmp(&self, other: &BTreeMap<K, V>) -> Option<Ordering> {
impl<K: Ord, V: Ord> Ord for BTreeMap<K, V> {
#[inline]
fn cmp(&self, other: &BTreeMap<K, V>) -> Ordering {
iter::order::cmp(self.iter(), other.iter())
self.iter().cmp(other.iter())
}
}
......
......@@ -25,7 +25,7 @@
use core::cmp::Ordering;
use core::fmt;
use core::hash::{Hasher, Hash};
use core::iter::{self, FromIterator};
use core::iter::FromIterator;
use core::mem;
use core::ptr;
......@@ -917,12 +917,12 @@ fn extend<I: IntoIterator<Item=&'a T>>(&mut self, iter: I) {
impl<A: PartialEq> PartialEq for LinkedList<A> {
fn eq(&self, other: &LinkedList<A>) -> bool {
self.len() == other.len() &&
iter::order::eq(self.iter(), other.iter())
self.iter().eq(other.iter())
}
fn ne(&self, other: &LinkedList<A>) -> bool {
self.len() != other.len() ||
iter::order::ne(self.iter(), other.iter())
self.iter().ne(other.iter())
}
}
......@@ -932,7 +932,7 @@ impl<A: Eq> Eq for LinkedList<A> {}
#[stable(feature = "rust1", since = "1.0.0")]
impl<A: PartialOrd> PartialOrd for LinkedList<A> {
fn partial_cmp(&self, other: &LinkedList<A>) -> Option<Ordering> {
iter::order::partial_cmp(self.iter(), other.iter())
self.iter().partial_cmp(other.iter())
}
}
......@@ -940,7 +940,7 @@ fn partial_cmp(&self, other: &LinkedList<A>) -> Option<Ordering> {
impl<A: Ord> Ord for LinkedList<A> {
#[inline]
fn cmp(&self, other: &LinkedList<A>) -> Ordering {
iter::order::cmp(self.iter(), other.iter())
self.iter().cmp(other.iter())
}
}
......
......@@ -20,7 +20,7 @@
use core::cmp::Ordering;
use core::fmt;
use core::iter::{self, repeat, FromIterator};
use core::iter::{repeat, FromIterator};
use core::ops::{Index, IndexMut};
use core::ptr;
use core::slice;
......@@ -1676,7 +1676,7 @@ impl<A: Eq> Eq for VecDeque<A> {}
#[stable(feature = "rust1", since = "1.0.0")]
impl<A: PartialOrd> PartialOrd for VecDeque<A> {
fn partial_cmp(&self, other: &VecDeque<A>) -> Option<Ordering> {
iter::order::partial_cmp(self.iter(), other.iter())
self.iter().partial_cmp(other.iter())
}
}
......@@ -1684,7 +1684,7 @@ fn partial_cmp(&self, other: &VecDeque<A>) -> Option<Ordering> {
impl<A: Ord> Ord for VecDeque<A> {
#[inline]
fn cmp(&self, other: &VecDeque<A>) -> Ordering {
iter::order::cmp(self.iter(), other.iter())
self.iter().cmp(other.iter())
}
}
......
......@@ -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 @@ fn product<P=<Self as Iterator>::Item>(self) -> P where
{
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 @@ fn partial_cmp(&self, other: &$name) -> ::option::Option<::cmp::Ordering> {
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 @@ fn cmp(&self, other: &[T]) -> Ordering {
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())
}
}
......@@ -882,7 +882,7 @@ fn next_back(&mut self) -> Option<Component<'a>> {
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> cmp::PartialEq for Components<'a> {
fn eq(&self, other: &Components<'a>) -> bool {
iter::order::eq(self.clone(), other.clone())
Iterator::eq(self.clone(), other.clone())
}
}
......@@ -892,14 +892,14 @@ impl<'a> cmp::Eq for Components<'a> {}
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> cmp::PartialOrd for Components<'a> {
fn partial_cmp(&self, other: &Components<'a>) -> Option<cmp::Ordering> {
iter::order::partial_cmp(self.clone(), other.clone())
Iterator::partial_cmp(self.clone(), other.clone())
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a> cmp::Ord for Components<'a> {
fn cmp(&self, other: &Components<'a>) -> cmp::Ordering {
iter::order::cmp(self.clone(), other.clone())
Iterator::cmp(self.clone(), other.clone())
}
}
......@@ -1162,14 +1162,14 @@ impl cmp::Eq for PathBuf {}
#[stable(feature = "rust1", since = "1.0.0")]
impl cmp::PartialOrd for PathBuf {
fn partial_cmp(&self, other: &PathBuf) -> Option<cmp::Ordering> {
self.components().partial_cmp(&other.components())
self.components().partial_cmp(other.components())
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl cmp::Ord for PathBuf {
fn cmp(&self, other: &PathBuf) -> cmp::Ordering {
self.components().cmp(&other.components())
self.components().cmp(other.components())
}
}
......@@ -1691,7 +1691,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
#[stable(feature = "rust1", since = "1.0.0")]
impl cmp::PartialEq for Path {
fn eq(&self, other: &Path) -> bool {
iter::order::eq(self.components(), other.components())
self.components().eq(other.components())
}
}
......@@ -1701,14 +1701,14 @@ impl cmp::Eq for Path {}
#[stable(feature = "rust1", since = "1.0.0")]
impl cmp::PartialOrd for Path {
fn partial_cmp(&self, other: &Path) -> Option<cmp::Ordering> {
self.components().partial_cmp(&other.components())
self.components().partial_cmp(other.components())
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl cmp::Ord for Path {
fn cmp(&self, other: &Path) -> cmp::Ordering {
self.components().cmp(&other.components())
self.components().cmp(other.components())
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册