Skip to content

Commit 0c9ef56

Browse files
committed
Auto merge of #79656 - jnqnfe:ordering, r=sfackler
Add some core::cmp::Ordering helpers ...to allow easier equal-to-or-greater-than and less-than-or-equal-to comparisons. Prior to Rust 1.42 a greater-than-or-equal-to comparison might be written either as a match block, or a traditional conditional check like this: ```rust if cmp == Ordering::Equal || cmp == Ordering::Greater { // Do something } ``` Which requires two instances of `cmp`. Don't forget that while `cmp` here is very short, it could be something much longer in real use cases. From Rust 1.42 a nicer alternative is possible: ```rust if matches!(cmp, Ordering::Equal | Ordering::Greater) { // Do something } ``` The commit adds another alternative which may be even better in some cases: ```rust if cmp.is_equal_or_greater() { // Do something } ``` The earlier examples could be cleaner than they are if the variants of `Ordering` are imported such that `Equal`, `Greater` and `Less` can be referred to directly, but not everyone will want to do that. The new solution can shorten lines, help avoid logic mistakes, and avoids having to import `Ordering` / `Ordering::*`.
2 parents 8cef65f + 169c59f commit 0c9ef56

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

library/core/src/cmp.rs

+114
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,120 @@ pub enum Ordering {
325325
}
326326

327327
impl Ordering {
328+
/// Returns `true` if the ordering is the `Equal` variant.
329+
///
330+
/// # Examples
331+
///
332+
/// ```
333+
/// #![feature(ordering_helpers)]
334+
/// use std::cmp::Ordering;
335+
///
336+
/// assert_eq!(Ordering::Less.is_eq(), false);
337+
/// assert_eq!(Ordering::Equal.is_eq(), true);
338+
/// assert_eq!(Ordering::Greater.is_eq(), false);
339+
/// ```
340+
#[inline]
341+
#[must_use]
342+
#[unstable(feature = "ordering_helpers", issue = "79885")]
343+
pub const fn is_eq(self) -> bool {
344+
matches!(self, Equal)
345+
}
346+
347+
/// Returns `true` if the ordering is not the `Equal` variant.
348+
///
349+
/// # Examples
350+
///
351+
/// ```
352+
/// #![feature(ordering_helpers)]
353+
/// use std::cmp::Ordering;
354+
///
355+
/// assert_eq!(Ordering::Less.is_ne(), true);
356+
/// assert_eq!(Ordering::Equal.is_ne(), false);
357+
/// assert_eq!(Ordering::Greater.is_ne(), true);
358+
/// ```
359+
#[inline]
360+
#[must_use]
361+
#[unstable(feature = "ordering_helpers", issue = "79885")]
362+
pub const fn is_ne(self) -> bool {
363+
!matches!(self, Equal)
364+
}
365+
366+
/// Returns `true` if the ordering is the `Less` variant.
367+
///
368+
/// # Examples
369+
///
370+
/// ```
371+
/// #![feature(ordering_helpers)]
372+
/// use std::cmp::Ordering;
373+
///
374+
/// assert_eq!(Ordering::Less.is_lt(), true);
375+
/// assert_eq!(Ordering::Equal.is_lt(), false);
376+
/// assert_eq!(Ordering::Greater.is_lt(), false);
377+
/// ```
378+
#[inline]
379+
#[must_use]
380+
#[unstable(feature = "ordering_helpers", issue = "79885")]
381+
pub const fn is_lt(self) -> bool {
382+
matches!(self, Less)
383+
}
384+
385+
/// Returns `true` if the ordering is the `Greater` variant.
386+
///
387+
/// # Examples
388+
///
389+
/// ```
390+
/// #![feature(ordering_helpers)]
391+
/// use std::cmp::Ordering;
392+
///
393+
/// assert_eq!(Ordering::Less.is_gt(), false);
394+
/// assert_eq!(Ordering::Equal.is_gt(), false);
395+
/// assert_eq!(Ordering::Greater.is_gt(), true);
396+
/// ```
397+
#[inline]
398+
#[must_use]
399+
#[unstable(feature = "ordering_helpers", issue = "79885")]
400+
pub const fn is_gt(self) -> bool {
401+
matches!(self, Greater)
402+
}
403+
404+
/// Returns `true` if the ordering is either the `Less` or `Equal` variant.
405+
///
406+
/// # Examples
407+
///
408+
/// ```
409+
/// #![feature(ordering_helpers)]
410+
/// use std::cmp::Ordering;
411+
///
412+
/// assert_eq!(Ordering::Less.is_le(), true);
413+
/// assert_eq!(Ordering::Equal.is_le(), true);
414+
/// assert_eq!(Ordering::Greater.is_le(), false);
415+
/// ```
416+
#[inline]
417+
#[must_use]
418+
#[unstable(feature = "ordering_helpers", issue = "79885")]
419+
pub const fn is_le(self) -> bool {
420+
!matches!(self, Greater)
421+
}
422+
423+
/// Returns `true` if the ordering is either the `Greater` or `Equal` variant.
424+
///
425+
/// # Examples
426+
///
427+
/// ```
428+
/// #![feature(ordering_helpers)]
429+
/// use std::cmp::Ordering;
430+
///
431+
/// assert_eq!(Ordering::Less.is_ge(), false);
432+
/// assert_eq!(Ordering::Equal.is_ge(), true);
433+
/// assert_eq!(Ordering::Greater.is_ge(), true);
434+
/// ```
435+
#[inline]
436+
#[must_use]
437+
#[unstable(feature = "ordering_helpers", issue = "79885")]
438+
pub const fn is_ge(self) -> bool {
439+
!matches!(self, Less)
440+
}
441+
328442
/// Reverses the `Ordering`.
329443
///
330444
/// * `Less` becomes `Greater`.

0 commit comments

Comments
 (0)