23
23
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
24
24
25
25
use self :: Ordering :: * ;
26
+ use crate :: convert:: TryFrom ;
26
27
use crate :: ops;
27
28
28
29
/// Trait for equality comparisons which are [partial equivalence
@@ -680,16 +681,76 @@ impl PartialOrd for Ordering {
680
681
}
681
682
}
682
683
684
+ /// An `UnequalOrdering` is an [`Ordering`] which is either `Less` or `Greater`.
685
+ /// It can be obtained from an [`Ordering`] using [`TryFrom`] or [`TryInto`].
686
+ /// It can be converted back to an [`Ordering`] using [`From`] or [`Into`].
687
+ ///
688
+ /// # Examples
689
+ ///
690
+ /// ```
691
+ /// #![feature(try_trait)]
692
+ ///
693
+ /// use std::cmp::{Ordering, UnequalOrdering};
694
+ /// use std::convert::{TryFrom, TryInto};
695
+ ///
696
+ /// assert_eq!(Ordering::Less.try_into(), Ok(UnequalOrdering::Less));
697
+ /// assert_eq!(Ordering::Greater.try_into(), Ok(UnequalOrdering::Greater));
698
+ /// assert!(UnequalOrdering::try_from(Ordering::Equal).is_err());
699
+ ///
700
+ /// assert_eq!(Ordering::from(UnequalOrdering::Less), Ordering::Less);
701
+ /// assert_eq!(Ordering::from(UnequalOrdering::Greater), Ordering::Greater);
702
+ /// ```
703
+ ///
704
+ /// [`TryFrom`]: crate::convert::TryFrom
705
+ /// [`TryInto`]: crate::convert::TryInto
706
+ #[ derive( Clone , Copy , PartialEq , Eq , Hash , Debug ) ]
707
+ #[ unstable( feature = "try_trait" , issue = "42327" ) ]
708
+ pub enum UnequalOrdering {
709
+ /// An ordering where a compared value is less than another.
710
+ Less = -1 ,
711
+ /// An ordering where a compared value is greater than another.
712
+ Greater = 1 ,
713
+ }
714
+
715
+ /// The error type returned when conversion of [`Ordering::Equal`] into
716
+ /// [`UnequalOrdering`] fails.
717
+ #[ derive( Clone , Copy , PartialEq , Eq , Debug ) ]
718
+ #[ unstable( feature = "try_trait" , issue = "42327" ) ]
719
+ pub struct EqualOrderingError ;
720
+
721
+ #[ unstable( feature = "try_trait" , issue = "42327" ) ]
722
+ impl TryFrom < Ordering > for UnequalOrdering {
723
+ type Error = EqualOrderingError ;
724
+
725
+ fn try_from ( value : Ordering ) -> Result < Self , EqualOrderingError > {
726
+ match value {
727
+ Less => Ok ( UnequalOrdering :: Less ) ,
728
+ Equal => Err ( EqualOrderingError ) ,
729
+ Greater => Ok ( UnequalOrdering :: Greater ) ,
730
+ }
731
+ }
732
+ }
733
+
734
+ #[ unstable( feature = "try_trait" , issue = "42327" ) ]
735
+ impl From < UnequalOrdering > for Ordering {
736
+ fn from ( value : UnequalOrdering ) -> Self {
737
+ match value {
738
+ UnequalOrdering :: Less => Less ,
739
+ UnequalOrdering :: Greater => Greater ,
740
+ }
741
+ }
742
+ }
743
+
683
744
#[ unstable( feature = "try_trait" , issue = "42327" ) ]
684
745
impl ops:: Try for Ordering {
685
746
type Ok = ( ) ;
686
- type Error = Self ;
747
+ type Error = UnequalOrdering ;
687
748
688
749
#[ inline]
689
- fn into_result ( self ) -> Result < ( ) , Self > {
690
- match self {
691
- Equal => Ok ( ( ) ) ,
692
- _ => Err ( self ) ,
750
+ fn into_result ( self ) -> Result < ( ) , UnequalOrdering > {
751
+ match UnequalOrdering :: try_from ( self ) {
752
+ Ok ( unequal_ordering ) => Err ( unequal_ordering ) ,
753
+ Err ( _ ) => Ok ( ( ) ) ,
693
754
}
694
755
}
695
756
@@ -699,8 +760,8 @@ impl ops::Try for Ordering {
699
760
}
700
761
701
762
#[ inline]
702
- fn from_error ( v : Self ) -> Self {
703
- v
763
+ fn from_error ( v : UnequalOrdering ) -> Self {
764
+ Self :: from ( v )
704
765
}
705
766
}
706
767
0 commit comments