Skip to content

Commit 8e8fd02

Browse files
committed
Specialize PartialEq for Option<num::NonZero*> and Option<ptr::NonNull>
1 parent 2afca78 commit 8e8fd02

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

library/core/src/option.rs

+67-1
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ use crate::{
512512
};
513513

514514
/// The `Option` type. See [the module level documentation](self) for more.
515-
#[derive(Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
515+
#[derive(Copy, PartialOrd, Eq, Ord, Debug, Hash)]
516516
#[rustc_diagnostic_item = "Option"]
517517
#[stable(feature = "rust1", since = "1.0.0")]
518518
pub enum Option<T> {
@@ -2035,6 +2035,72 @@ impl<'a, T> const From<&'a mut Option<T>> for Option<&'a mut T> {
20352035
}
20362036
}
20372037

2038+
#[stable(feature = "rust1", since = "1.0.0")]
2039+
impl<T> crate::marker::StructuralPartialEq for Option<T> {}
2040+
#[stable(feature = "rust1", since = "1.0.0")]
2041+
impl<T: PartialEq> PartialEq for Option<T> {
2042+
#[inline]
2043+
fn eq(&self, other: &Self) -> bool {
2044+
SpecOptionPartialEq::eq(self, other)
2045+
}
2046+
}
2047+
2048+
#[unstable(feature = "spec_option_partial_eq", issue = "none")]
2049+
#[doc(hidden)]
2050+
pub trait SpecOptionPartialEq: Sized {
2051+
fn eq(l: &Option<Self>, other: &Option<Self>) -> bool;
2052+
}
2053+
2054+
#[unstable(feature = "spec_option_partial_eq", issue = "none")]
2055+
impl<T: PartialEq> SpecOptionPartialEq for T {
2056+
#[inline]
2057+
default fn eq(l: &Option<T>, r: &Option<T>) -> bool {
2058+
match (l, r) {
2059+
(Some(l), Some(r)) => *l == *r,
2060+
(None, None) => true,
2061+
_ => false,
2062+
}
2063+
}
2064+
}
2065+
2066+
macro_rules! non_zero_option {
2067+
( $( #[$stability: meta] $NZ:ty; )+ ) => {
2068+
$(
2069+
#[$stability]
2070+
impl SpecOptionPartialEq for $NZ {
2071+
#[inline]
2072+
fn eq(l: &Option<Self>, r: &Option<Self>) -> bool {
2073+
l.map(Self::get).unwrap_or(0) == r.map(Self::get).unwrap_or(0)
2074+
}
2075+
}
2076+
)+
2077+
};
2078+
}
2079+
2080+
non_zero_option! {
2081+
#[stable(feature = "nonzero", since = "1.28.0")] crate::num::NonZeroU8;
2082+
#[stable(feature = "nonzero", since = "1.28.0")] crate::num::NonZeroU16;
2083+
#[stable(feature = "nonzero", since = "1.28.0")] crate::num::NonZeroU32;
2084+
#[stable(feature = "nonzero", since = "1.28.0")] crate::num::NonZeroU64;
2085+
#[stable(feature = "nonzero", since = "1.28.0")] crate::num::NonZeroU128;
2086+
#[stable(feature = "nonzero", since = "1.28.0")] crate::num::NonZeroUsize;
2087+
#[stable(feature = "signed_nonzero", since = "1.34.0")] crate::num::NonZeroI8;
2088+
#[stable(feature = "signed_nonzero", since = "1.34.0")] crate::num::NonZeroI16;
2089+
#[stable(feature = "signed_nonzero", since = "1.34.0")] crate::num::NonZeroI32;
2090+
#[stable(feature = "signed_nonzero", since = "1.34.0")] crate::num::NonZeroI64;
2091+
#[stable(feature = "signed_nonzero", since = "1.34.0")] crate::num::NonZeroI128;
2092+
#[stable(feature = "signed_nonzero", since = "1.34.0")] crate::num::NonZeroIsize;
2093+
}
2094+
2095+
#[stable(feature = "nonnull", since = "1.25.0")]
2096+
impl<T> SpecOptionPartialEq for crate::ptr::NonNull<T> {
2097+
#[inline]
2098+
fn eq(l: &Option<Self>, r: &Option<Self>) -> bool {
2099+
l.map(Self::as_ptr).unwrap_or_else(|| crate::ptr::null_mut())
2100+
== r.map(Self::as_ptr).unwrap_or_else(|| crate::ptr::null_mut())
2101+
}
2102+
}
2103+
20382104
/////////////////////////////////////////////////////////////////////////////
20392105
// The Option Iterators
20402106
/////////////////////////////////////////////////////////////////////////////

src/test/codegen/option-nonzero-eq.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// compile-flags: -O -Zmerge-functions=disabled
2+
3+
#![crate_type = "lib"]
4+
5+
extern crate core;
6+
use core::num::{NonZeroU32, NonZeroI64};
7+
use core::ptr::NonNull;
8+
9+
// CHECK-lABEL: @non_zero_eq
10+
#[no_mangle]
11+
pub fn non_zero_eq(l: Option<NonZeroU32>, r: Option<NonZeroU32>) -> bool {
12+
// CHECK: start:
13+
// CHECK-NEXT: icmp eq i32
14+
// CHECK-NEXT: ret i1
15+
l == r
16+
}
17+
18+
// CHECK-lABEL: @non_zero_signed_eq
19+
#[no_mangle]
20+
pub fn non_zero_signed_eq(l: Option<NonZeroI64>, r: Option<NonZeroI64>) -> bool {
21+
// CHECK: start:
22+
// CHECK-NEXT: icmp eq i64
23+
// CHECK-NEXT: ret i1
24+
l == r
25+
}
26+
27+
// CHECK-lABEL: @non_null_eq
28+
#[no_mangle]
29+
pub fn non_null_eq(l: Option<NonNull<u8>>, r: Option<NonNull<u8>>) -> bool {
30+
// CHECK: start:
31+
// CHECK-NEXT: icmp eq {{(i8\*|ptr)}}
32+
// CHECK-NEXT: ret i1
33+
l == r
34+
}

0 commit comments

Comments
 (0)