@@ -2557,10 +2557,40 @@ pub trait Iterator {
2557
2557
/// assert_eq!([1, 2].iter().cmp([1].iter()), Ordering::Greater);
2558
2558
/// ```
2559
2559
#[ 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
2561
2562
I : IntoIterator < Item = Self :: Item > ,
2562
2563
Self :: Item : Ord ,
2563
2564
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 ,
2564
2594
{
2565
2595
let mut other = other. into_iter ( ) ;
2566
2596
@@ -2579,7 +2609,7 @@ pub trait Iterator {
2579
2609
Some ( val) => val,
2580
2610
} ;
2581
2611
2582
- match x . cmp ( & y) {
2612
+ match cmp ( x , y) {
2583
2613
Ordering :: Equal => ( ) ,
2584
2614
non_eq => return non_eq,
2585
2615
}
@@ -2601,10 +2631,49 @@ pub trait Iterator {
2601
2631
/// assert_eq!([std::f64::NAN].iter().partial_cmp([1.].iter()), None);
2602
2632
/// ```
2603
2633
#[ 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
2605
2636
I : IntoIterator ,
2606
2637
Self :: Item : PartialOrd < I :: Item > ,
2607
2638
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 > ,
2608
2677
{
2609
2678
let mut other = other. into_iter ( ) ;
2610
2679
@@ -2623,7 +2692,7 @@ pub trait Iterator {
2623
2692
Some ( val) => val,
2624
2693
} ;
2625
2694
2626
- match x . partial_cmp ( & y) {
2695
+ match partial_cmp ( x , y) {
2627
2696
Some ( Ordering :: Equal ) => ( ) ,
2628
2697
non_eq => return non_eq,
2629
2698
}
@@ -2640,10 +2709,36 @@ pub trait Iterator {
2640
2709
/// assert_eq!([1].iter().eq([1, 2].iter()), false);
2641
2710
/// ```
2642
2711
#[ 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
2644
2714
I : IntoIterator ,
2645
2715
Self :: Item : PartialEq < I :: Item > ,
2646
2716
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 ,
2647
2742
{
2648
2743
let mut other = other. into_iter ( ) ;
2649
2744
@@ -2658,7 +2753,9 @@ pub trait Iterator {
2658
2753
Some ( val) => val,
2659
2754
} ;
2660
2755
2661
- if x != y { return false }
2756
+ if !eq ( x, y) {
2757
+ return false ;
2758
+ }
2662
2759
}
2663
2760
}
2664
2761
0 commit comments