Skip to content

Commit 3122db7

Browse files
committed
Implement SpecOptionPartialEq for cmp::Ordering
1 parent e08b379 commit 3122db7

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

library/core/src/option.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ use crate::marker::Destruct;
551551
use crate::panicking::{panic, panic_str};
552552
use crate::pin::Pin;
553553
use crate::{
554-
convert, hint, mem,
554+
cmp, convert, hint, mem,
555555
ops::{self, ControlFlow, Deref, DerefMut},
556556
};
557557

@@ -2146,6 +2146,14 @@ impl<T> SpecOptionPartialEq for crate::ptr::NonNull<T> {
21462146
}
21472147
}
21482148

2149+
#[stable(feature = "rust1", since = "1.0.0")]
2150+
impl SpecOptionPartialEq for cmp::Ordering {
2151+
#[inline]
2152+
fn eq(l: &Option<Self>, r: &Option<Self>) -> bool {
2153+
l.map_or(2, |x| x as i8) == r.map_or(2, |x| x as i8)
2154+
}
2155+
}
2156+
21492157
/////////////////////////////////////////////////////////////////////////////
21502158
// The Option Iterators
21512159
/////////////////////////////////////////////////////////////////////////////

tests/codegen/option-nonzero-eq.rs

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![crate_type = "lib"]
44

55
extern crate core;
6+
use core::cmp::Ordering;
67
use core::num::{NonZeroU32, NonZeroI64};
78
use core::ptr::NonNull;
89

@@ -32,3 +33,12 @@ pub fn non_null_eq(l: Option<NonNull<u8>>, r: Option<NonNull<u8>>) -> bool {
3233
// CHECK-NEXT: ret i1
3334
l == r
3435
}
36+
37+
// CHECK-lABEL: @ordering_eq
38+
#[no_mangle]
39+
pub fn ordering_eq(l: Option<Ordering>, r: Option<Ordering>) -> bool {
40+
// CHECK: start:
41+
// CHECK-NEXT: icmp eq i8
42+
// CHECK-NEXT: ret i1
43+
l == r
44+
}

0 commit comments

Comments
 (0)