Skip to content

Commit dd98d8e

Browse files
committed
Auto merge of rust-lang#103556 - clubby789:specialize-option-partial-eq, r=scottmcm
Manually implement PartialEq for Option<T> and specialize non-nullable types This PR manually implements `PartialEq` and `StructuralPartialEq` for `Option`, which seems to produce slightly better codegen than the automatically derived implementation. It also allows specializing on the `core::num::NonZero*` and `core::ptr::NonNull` types, taking advantage of the niche optimization by transmuting the `Option<T>` to `T` to be compared directly, which can be done in just two instructions. A comparison of the original, new and specialized code generation is available [here](https://godbolt.org/z/dE4jxdYsa).
2 parents 9b5d649 + 7d22190 commit dd98d8e

File tree

1 file changed

+67
-1
lines changed

1 file changed

+67
-1
lines changed

core/src/option.rs

Lines changed: 67 additions & 1 deletion
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", reason = "exposed only for rustc")]
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", reason = "exposed only for rustc")]
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
/////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)