Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some core::cmp::Ordering helpers #79656

Merged
merged 1 commit into from
Dec 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions library/core/src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,120 @@ pub enum Ordering {
}

impl Ordering {
/// Returns `true` if the ordering is the `Equal` variant.
///
/// # Examples
///
/// ```
/// #![feature(ordering_helpers)]
/// use std::cmp::Ordering;
///
/// assert_eq!(Ordering::Less.is_eq(), false);
/// assert_eq!(Ordering::Equal.is_eq(), true);
/// assert_eq!(Ordering::Greater.is_eq(), false);
/// ```
#[inline]
#[must_use]
#[unstable(feature = "ordering_helpers", issue = "79885")]
pub const fn is_eq(self) -> bool {
matches!(self, Equal)
}

/// Returns `true` if the ordering is not the `Equal` variant.
///
/// # Examples
///
/// ```
/// #![feature(ordering_helpers)]
/// use std::cmp::Ordering;
///
/// assert_eq!(Ordering::Less.is_ne(), true);
/// assert_eq!(Ordering::Equal.is_ne(), false);
/// assert_eq!(Ordering::Greater.is_ne(), true);
/// ```
#[inline]
#[must_use]
#[unstable(feature = "ordering_helpers", issue = "79885")]
pub const fn is_ne(self) -> bool {
!matches!(self, Equal)
}

/// Returns `true` if the ordering is the `Less` variant.
///
/// # Examples
///
/// ```
/// #![feature(ordering_helpers)]
/// use std::cmp::Ordering;
///
/// assert_eq!(Ordering::Less.is_lt(), true);
/// assert_eq!(Ordering::Equal.is_lt(), false);
/// assert_eq!(Ordering::Greater.is_lt(), false);
/// ```
#[inline]
#[must_use]
#[unstable(feature = "ordering_helpers", issue = "79885")]
pub const fn is_lt(self) -> bool {
matches!(self, Less)
}

/// Returns `true` if the ordering is the `Greater` variant.
///
/// # Examples
///
/// ```
/// #![feature(ordering_helpers)]
/// use std::cmp::Ordering;
///
/// assert_eq!(Ordering::Less.is_gt(), false);
/// assert_eq!(Ordering::Equal.is_gt(), false);
/// assert_eq!(Ordering::Greater.is_gt(), true);
/// ```
#[inline]
#[must_use]
#[unstable(feature = "ordering_helpers", issue = "79885")]
pub const fn is_gt(self) -> bool {
matches!(self, Greater)
}

/// Returns `true` if the ordering is either the `Less` or `Equal` variant.
///
/// # Examples
///
/// ```
/// #![feature(ordering_helpers)]
/// use std::cmp::Ordering;
///
/// assert_eq!(Ordering::Less.is_le(), true);
/// assert_eq!(Ordering::Equal.is_le(), true);
/// assert_eq!(Ordering::Greater.is_le(), false);
/// ```
#[inline]
#[must_use]
#[unstable(feature = "ordering_helpers", issue = "79885")]
pub const fn is_le(self) -> bool {
!matches!(self, Greater)
}

/// Returns `true` if the ordering is either the `Greater` or `Equal` variant.
///
/// # Examples
///
/// ```
/// #![feature(ordering_helpers)]
/// use std::cmp::Ordering;
///
/// assert_eq!(Ordering::Less.is_ge(), false);
/// assert_eq!(Ordering::Equal.is_ge(), true);
/// assert_eq!(Ordering::Greater.is_ge(), true);
/// ```
#[inline]
#[must_use]
#[unstable(feature = "ordering_helpers", issue = "79885")]
pub const fn is_ge(self) -> bool {
!matches!(self, Less)
}

/// Reverses the `Ordering`.
///
/// * `Less` becomes `Greater`.
Expand Down