Skip to content

Commit 4558600

Browse files
committed
Use UnequalOrdering as Error type of ops::Try
1 parent 7c57420 commit 4558600

File tree

1 file changed

+68
-7
lines changed

1 file changed

+68
-7
lines changed

library/core/src/cmp.rs

+68-7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#![stable(feature = "rust1", since = "1.0.0")]
2424

2525
use self::Ordering::*;
26+
use crate::convert::TryFrom;
2627
use crate::ops;
2728

2829
/// Trait for equality comparisons which are [partial equivalence
@@ -680,16 +681,76 @@ impl PartialOrd for Ordering {
680681
}
681682
}
682683

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+
683744
#[unstable(feature = "try_trait", issue = "42327")]
684745
impl ops::Try for Ordering {
685746
type Ok = ();
686-
type Error = Self;
747+
type Error = UnequalOrdering;
687748

688749
#[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(()),
693754
}
694755
}
695756

@@ -699,8 +760,8 @@ impl ops::Try for Ordering {
699760
}
700761

701762
#[inline]
702-
fn from_error(v: Self) -> Self {
703-
v
763+
fn from_error(v: UnequalOrdering) -> Self {
764+
Self::from(v)
704765
}
705766
}
706767

0 commit comments

Comments
 (0)