@@ -2557,10 +2557,40 @@ pub trait Iterator {
25572557 /// assert_eq!([1, 2].iter().cmp([1].iter()), Ordering::Greater);
25582558 /// ```
25592559 #[ stable( feature = "iter_order" , since = "1.5.0" ) ]
2560- fn cmp < I > ( mut self , other : I ) -> Ordering where
2560+ fn cmp < I > ( self , other : I ) -> Ordering
2561+ where
25612562 I : IntoIterator < Item = Self :: Item > ,
25622563 Self :: Item : Ord ,
25632564 Self : Sized ,
2565+ {
2566+ self . cmp_by ( other, |x, y| x. cmp ( & y) )
2567+ }
2568+
2569+ /// Lexicographically compares the elements of this `Iterator` with those
2570+ /// of another with respect to the specified comparison function.
2571+ ///
2572+ /// # Examples
2573+ ///
2574+ /// Basic usage:
2575+ ///
2576+ /// ```
2577+ /// #![feature(iter_order_by)]
2578+ ///
2579+ /// use std::cmp::Ordering;
2580+ ///
2581+ /// let xs = [1, 2, 3, 4];
2582+ /// let ys = [1, 4, 9, 16];
2583+ ///
2584+ /// assert_eq!(xs.iter().cmp_by(&ys, |&x, &y| x.cmp(&y)), Ordering::Less);
2585+ /// assert_eq!(xs.iter().cmp_by(&ys, |&x, &y| (x * x).cmp(&y)), Ordering::Equal);
2586+ /// assert_eq!(xs.iter().cmp_by(&ys, |&x, &y| (2 * x).cmp(&y)), Ordering::Greater);
2587+ /// ```
2588+ #[ unstable( feature = "iter_order_by" , issue = "0" ) ]
2589+ fn cmp_by < I , F > ( mut self , other : I , mut cmp : F ) -> Ordering
2590+ where
2591+ Self : Sized ,
2592+ I : IntoIterator ,
2593+ F : FnMut ( Self :: Item , I :: Item ) -> Ordering ,
25642594 {
25652595 let mut other = other. into_iter ( ) ;
25662596
@@ -2579,7 +2609,7 @@ pub trait Iterator {
25792609 Some ( val) => val,
25802610 } ;
25812611
2582- match x . cmp ( & y) {
2612+ match cmp ( x , y) {
25832613 Ordering :: Equal => ( ) ,
25842614 non_eq => return non_eq,
25852615 }
@@ -2601,10 +2631,49 @@ pub trait Iterator {
26012631 /// assert_eq!([std::f64::NAN].iter().partial_cmp([1.].iter()), None);
26022632 /// ```
26032633 #[ stable( feature = "iter_order" , since = "1.5.0" ) ]
2604- fn partial_cmp < I > ( mut self , other : I ) -> Option < Ordering > where
2634+ fn partial_cmp < I > ( self , other : I ) -> Option < Ordering >
2635+ where
26052636 I : IntoIterator ,
26062637 Self :: Item : PartialOrd < I :: Item > ,
26072638 Self : Sized ,
2639+ {
2640+ self . partial_cmp_by ( other, |x, y| x. partial_cmp ( & y) )
2641+ }
2642+
2643+ /// Lexicographically compares the elements of this `Iterator` with those
2644+ /// of another with respect to the specified comparison function.
2645+ ///
2646+ /// # Examples
2647+ ///
2648+ /// Basic usage:
2649+ ///
2650+ /// ```
2651+ /// #![feature(iter_order_by)]
2652+ ///
2653+ /// use std::cmp::Ordering;
2654+ ///
2655+ /// let xs = [1.0, 2.0, 3.0, 4.0];
2656+ /// let ys = [1.0, 4.0, 9.0, 16.0];
2657+ ///
2658+ /// assert_eq!(
2659+ /// xs.iter().partial_cmp_by(&ys, |&x, &y| x.partial_cmp(&y)),
2660+ /// Some(Ordering::Less)
2661+ /// );
2662+ /// assert_eq!(
2663+ /// xs.iter().partial_cmp_by(&ys, |&x, &y| (x * x).partial_cmp(&y)),
2664+ /// Some(Ordering::Equal)
2665+ /// );
2666+ /// assert_eq!(
2667+ /// xs.iter().partial_cmp_by(&ys, |&x, &y| (2.0 * x).partial_cmp(&y)),
2668+ /// Some(Ordering::Greater)
2669+ /// );
2670+ /// ```
2671+ #[ unstable( feature = "iter_order_by" , issue = "0" ) ]
2672+ fn partial_cmp_by < I , F > ( mut self , other : I , mut partial_cmp : F ) -> Option < Ordering >
2673+ where
2674+ Self : Sized ,
2675+ I : IntoIterator ,
2676+ F : FnMut ( Self :: Item , I :: Item ) -> Option < Ordering > ,
26082677 {
26092678 let mut other = other. into_iter ( ) ;
26102679
@@ -2623,7 +2692,7 @@ pub trait Iterator {
26232692 Some ( val) => val,
26242693 } ;
26252694
2626- match x . partial_cmp ( & y) {
2695+ match partial_cmp ( x , y) {
26272696 Some ( Ordering :: Equal ) => ( ) ,
26282697 non_eq => return non_eq,
26292698 }
@@ -2640,10 +2709,36 @@ pub trait Iterator {
26402709 /// assert_eq!([1].iter().eq([1, 2].iter()), false);
26412710 /// ```
26422711 #[ stable( feature = "iter_order" , since = "1.5.0" ) ]
2643- fn eq < I > ( mut self , other : I ) -> bool where
2712+ fn eq < I > ( self , other : I ) -> bool
2713+ where
26442714 I : IntoIterator ,
26452715 Self :: Item : PartialEq < I :: Item > ,
26462716 Self : Sized ,
2717+ {
2718+ self . eq_by ( other, |x, y| x == y)
2719+ }
2720+
2721+ /// Determines if the elements of this `Iterator` are equal to those of
2722+ /// another with respect to the specified equality function.
2723+ ///
2724+ /// # Examples
2725+ ///
2726+ /// Basic usage:
2727+ ///
2728+ /// ```
2729+ /// #![feature(iter_order_by)]
2730+ ///
2731+ /// let xs = [1, 2, 3, 4];
2732+ /// let ys = [1, 4, 9, 16];
2733+ ///
2734+ /// assert!(xs.iter().eq_by(&ys, |&x, &y| x * x == y));
2735+ /// ```
2736+ #[ unstable( feature = "iter_order_by" , issue = "0" ) ]
2737+ fn eq_by < I , F > ( mut self , other : I , mut eq : F ) -> bool
2738+ where
2739+ Self : Sized ,
2740+ I : IntoIterator ,
2741+ F : FnMut ( Self :: Item , I :: Item ) -> bool ,
26472742 {
26482743 let mut other = other. into_iter ( ) ;
26492744
@@ -2658,7 +2753,9 @@ pub trait Iterator {
26582753 Some ( val) => val,
26592754 } ;
26602755
2661- if x != y { return false }
2756+ if !eq ( x, y) {
2757+ return false ;
2758+ }
26622759 }
26632760 }
26642761
0 commit comments