Skip to content

Commit f8fe32f

Browse files
authored
Rollup merge of #143377 - hkBst:clippy-fix-5, r=workingjubilee
document overrides of the default impl of PartialEq::ne <del>Removes manual impls of PartialEq::ne which are equivalent to the default.</del> - Cleans up documentation of `PartialEq::ne` and documents overrides of the default impl. - Uses macros to remove duplication.
2 parents 5f1173b + 6912f15 commit f8fe32f

File tree

1 file changed

+114
-185
lines changed

1 file changed

+114
-185
lines changed

library/core/src/cmp.rs

Lines changed: 114 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,23 @@ use crate::ops::ControlFlow;
249249
#[rustc_diagnostic_item = "PartialEq"]
250250
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
251251
pub const trait PartialEq<Rhs: PointeeSized = Self>: PointeeSized {
252-
/// Tests for `self` and `other` values to be equal, and is used by `==`.
252+
/// Equality operator `==`.
253+
///
254+
/// Implementation of the "is equal to" operator `==`:
255+
/// tests whether its arguments are equal.
253256
#[must_use]
254257
#[stable(feature = "rust1", since = "1.0.0")]
255258
#[rustc_diagnostic_item = "cmp_partialeq_eq"]
256259
fn eq(&self, other: &Rhs) -> bool;
257260

258-
/// Tests for `!=`. The default implementation is almost always sufficient,
259-
/// and should not be overridden without very good reason.
261+
/// Inequality operator `!=`.
262+
///
263+
/// Implementation of the "is not equal to" or "is different from" operator `!=`:
264+
/// tests whether its arguments are different.
265+
///
266+
/// # Default implementation
267+
/// The default implementation of the inequality operator simply calls
268+
/// the implementation of the equality operator and negates the result.
260269
#[inline]
261270
#[must_use]
262271
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1857,34 +1866,38 @@ mod impls {
18571866
use crate::ops::ControlFlow::{self, Break, Continue};
18581867
use crate::panic::const_assert;
18591868

1860-
macro_rules! partial_eq_impl {
1869+
/// Implements `PartialEq` for primitive types.
1870+
///
1871+
/// Primitive types have a compiler-defined primitive implementation of `==` and `!=`.
1872+
/// This implements the `PartialEq` trait is terms of those primitive implementations.
1873+
///
1874+
/// NOTE: Calling this on a non-primitive type (such as `()`)
1875+
/// leads to infinitely recursive implementations.
1876+
macro_rules! impl_partial_eq_for_primitive {
18611877
($($t:ty)*) => ($(
18621878
#[stable(feature = "rust1", since = "1.0.0")]
18631879
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
18641880
impl const PartialEq for $t {
18651881
#[inline]
18661882
fn eq(&self, other: &Self) -> bool { *self == *other }
1883+
// Override the default to use the primitive implementation for `!=`.
18671884
#[inline]
18681885
fn ne(&self, other: &Self) -> bool { *self != *other }
18691886
}
18701887
)*)
18711888
}
18721889

1890+
impl_partial_eq_for_primitive! {
1891+
bool char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f16 f32 f64 f128
1892+
}
1893+
18731894
#[stable(feature = "rust1", since = "1.0.0")]
18741895
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
18751896
impl const PartialEq for () {
18761897
#[inline]
18771898
fn eq(&self, _other: &()) -> bool {
18781899
true
18791900
}
1880-
#[inline]
1881-
fn ne(&self, _other: &()) -> bool {
1882-
false
1883-
}
1884-
}
1885-
1886-
partial_eq_impl! {
1887-
bool char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f16 f32 f64 f128
18881901
}
18891902

18901903
macro_rules! eq_impl {
@@ -2098,185 +2111,101 @@ mod impls {
20982111
}
20992112
}
21002113

2101-
// & pointers
2114+
// reference types
21022115

2103-
#[stable(feature = "rust1", since = "1.0.0")]
2104-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2105-
impl<A: PointeeSized, B: PointeeSized> const PartialEq<&B> for &A
2106-
where
2107-
A: [const] PartialEq<B>,
2108-
{
2109-
#[inline]
2110-
fn eq(&self, other: &&B) -> bool {
2111-
PartialEq::eq(*self, *other)
2112-
}
2113-
#[inline]
2114-
fn ne(&self, other: &&B) -> bool {
2115-
PartialEq::ne(*self, *other)
2116-
}
2117-
}
2118-
#[stable(feature = "rust1", since = "1.0.0")]
2119-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2120-
impl<A: PointeeSized, B: PointeeSized> const PartialOrd<&B> for &A
2121-
where
2122-
A: [const] PartialOrd<B>,
2123-
{
2124-
#[inline]
2125-
fn partial_cmp(&self, other: &&B) -> Option<Ordering> {
2126-
PartialOrd::partial_cmp(*self, *other)
2127-
}
2128-
#[inline]
2129-
fn lt(&self, other: &&B) -> bool {
2130-
PartialOrd::lt(*self, *other)
2131-
}
2132-
#[inline]
2133-
fn le(&self, other: &&B) -> bool {
2134-
PartialOrd::le(*self, *other)
2135-
}
2136-
#[inline]
2137-
fn gt(&self, other: &&B) -> bool {
2138-
PartialOrd::gt(*self, *other)
2139-
}
2140-
#[inline]
2141-
fn ge(&self, other: &&B) -> bool {
2142-
PartialOrd::ge(*self, *other)
2143-
}
2144-
#[inline]
2145-
fn __chaining_lt(&self, other: &&B) -> ControlFlow<bool> {
2146-
PartialOrd::__chaining_lt(*self, *other)
2147-
}
2148-
#[inline]
2149-
fn __chaining_le(&self, other: &&B) -> ControlFlow<bool> {
2150-
PartialOrd::__chaining_le(*self, *other)
2151-
}
2152-
#[inline]
2153-
fn __chaining_gt(&self, other: &&B) -> ControlFlow<bool> {
2154-
PartialOrd::__chaining_gt(*self, *other)
2155-
}
2156-
#[inline]
2157-
fn __chaining_ge(&self, other: &&B) -> ControlFlow<bool> {
2158-
PartialOrd::__chaining_ge(*self, *other)
2159-
}
2160-
}
2161-
#[stable(feature = "rust1", since = "1.0.0")]
2162-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2163-
impl<A: PointeeSized> const Ord for &A
2164-
where
2165-
A: [const] Ord,
2166-
{
2167-
#[inline]
2168-
fn cmp(&self, other: &Self) -> Ordering {
2169-
Ord::cmp(*self, *other)
2170-
}
2116+
macro_rules! impl_partial_eq {
2117+
(<$A:ident, $B:ident> for $(($ref_A:ty, $ref_B:ty))*) => ($(
2118+
#[stable(feature = "rust1", since = "1.0.0")]
2119+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2120+
impl<$A, $B> const PartialEq<$ref_B> for $ref_A
2121+
where
2122+
$A: [const] PartialEq<$B> + PointeeSized,
2123+
$B: PointeeSized,
2124+
{
2125+
#[inline]
2126+
fn eq(&self, other: &$ref_B) -> bool {
2127+
PartialEq::eq(*self, *other)
2128+
}
2129+
// if <A as PartialEq<B>>::ne uses inline assembly or FFI, then
2130+
// this forwarding impl may be more efficient than the default impl
2131+
#[inline]
2132+
fn ne(&self, other: &$ref_B) -> bool {
2133+
PartialEq::ne(*self, *other)
2134+
}
2135+
}
2136+
)*)
21712137
}
2172-
#[stable(feature = "rust1", since = "1.0.0")]
2173-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2174-
impl<A: PointeeSized> const Eq for &A where A: [const] Eq {}
21752138

2176-
// &mut pointers
2139+
impl_partial_eq!(<A, B> for (&A, &B) (&A, &mut B) (&mut A, &B) (&mut A, &mut B));
21772140

2178-
#[stable(feature = "rust1", since = "1.0.0")]
2179-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2180-
impl<A: PointeeSized, B: PointeeSized> const PartialEq<&mut B> for &mut A
2181-
where
2182-
A: [const] PartialEq<B>,
2183-
{
2184-
#[inline]
2185-
fn eq(&self, other: &&mut B) -> bool {
2186-
PartialEq::eq(*self, *other)
2187-
}
2188-
#[inline]
2189-
fn ne(&self, other: &&mut B) -> bool {
2190-
PartialEq::ne(*self, *other)
2191-
}
2192-
}
2193-
#[stable(feature = "rust1", since = "1.0.0")]
2194-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2195-
impl<A: PointeeSized, B: PointeeSized> const PartialOrd<&mut B> for &mut A
2196-
where
2197-
A: [const] PartialOrd<B>,
2198-
{
2199-
#[inline]
2200-
fn partial_cmp(&self, other: &&mut B) -> Option<Ordering> {
2201-
PartialOrd::partial_cmp(*self, *other)
2202-
}
2203-
#[inline]
2204-
fn lt(&self, other: &&mut B) -> bool {
2205-
PartialOrd::lt(*self, *other)
2206-
}
2207-
#[inline]
2208-
fn le(&self, other: &&mut B) -> bool {
2209-
PartialOrd::le(*self, *other)
2210-
}
2211-
#[inline]
2212-
fn gt(&self, other: &&mut B) -> bool {
2213-
PartialOrd::gt(*self, *other)
2214-
}
2215-
#[inline]
2216-
fn ge(&self, other: &&mut B) -> bool {
2217-
PartialOrd::ge(*self, *other)
2218-
}
2219-
#[inline]
2220-
fn __chaining_lt(&self, other: &&mut B) -> ControlFlow<bool> {
2221-
PartialOrd::__chaining_lt(*self, *other)
2222-
}
2223-
#[inline]
2224-
fn __chaining_le(&self, other: &&mut B) -> ControlFlow<bool> {
2225-
PartialOrd::__chaining_le(*self, *other)
2226-
}
2227-
#[inline]
2228-
fn __chaining_gt(&self, other: &&mut B) -> ControlFlow<bool> {
2229-
PartialOrd::__chaining_gt(*self, *other)
2230-
}
2231-
#[inline]
2232-
fn __chaining_ge(&self, other: &&mut B) -> ControlFlow<bool> {
2233-
PartialOrd::__chaining_ge(*self, *other)
2234-
}
2235-
}
2236-
#[stable(feature = "rust1", since = "1.0.0")]
2237-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2238-
impl<A: PointeeSized> const Ord for &mut A
2239-
where
2240-
A: [const] Ord,
2241-
{
2242-
#[inline]
2243-
fn cmp(&self, other: &Self) -> Ordering {
2244-
Ord::cmp(*self, *other)
2245-
}
2141+
macro_rules! impl_partial_ord {
2142+
(<$A:ident, $B:ident> for $(($ref_A:ty, $ref_B:ty))*) => ($(
2143+
#[stable(feature = "rust1", since = "1.0.0")]
2144+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2145+
impl<$A, $B> const PartialOrd<$ref_B> for $ref_A
2146+
where
2147+
$A: [const] PartialOrd<$B> + PointeeSized,
2148+
$B: PointeeSized,
2149+
{
2150+
#[inline]
2151+
fn partial_cmp(&self, other: &$ref_B) -> Option<Ordering> {
2152+
PartialOrd::partial_cmp(*self, *other)
2153+
}
2154+
#[inline]
2155+
fn lt(&self, other: &$ref_B) -> bool {
2156+
PartialOrd::lt(*self, *other)
2157+
}
2158+
#[inline]
2159+
fn le(&self, other: &$ref_B) -> bool {
2160+
PartialOrd::le(*self, *other)
2161+
}
2162+
#[inline]
2163+
fn gt(&self, other: &$ref_B) -> bool {
2164+
PartialOrd::gt(*self, *other)
2165+
}
2166+
#[inline]
2167+
fn ge(&self, other: &$ref_B) -> bool {
2168+
PartialOrd::ge(*self, *other)
2169+
}
2170+
#[inline]
2171+
fn __chaining_lt(&self, other: &$ref_B) -> ControlFlow<bool> {
2172+
PartialOrd::__chaining_lt(*self, *other)
2173+
}
2174+
#[inline]
2175+
fn __chaining_le(&self, other: &$ref_B) -> ControlFlow<bool> {
2176+
PartialOrd::__chaining_le(*self, *other)
2177+
}
2178+
#[inline]
2179+
fn __chaining_gt(&self, other: &$ref_B) -> ControlFlow<bool> {
2180+
PartialOrd::__chaining_gt(*self, *other)
2181+
}
2182+
#[inline]
2183+
fn __chaining_ge(&self, other: &$ref_B) -> ControlFlow<bool> {
2184+
PartialOrd::__chaining_ge(*self, *other)
2185+
}
2186+
}
2187+
)*)
22462188
}
2247-
#[stable(feature = "rust1", since = "1.0.0")]
2248-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2249-
impl<A: PointeeSized> const Eq for &mut A where A: [const] Eq {}
22502189

2251-
#[stable(feature = "rust1", since = "1.0.0")]
2252-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2253-
impl<A: PointeeSized, B: PointeeSized> const PartialEq<&mut B> for &A
2254-
where
2255-
A: [const] PartialEq<B>,
2256-
{
2257-
#[inline]
2258-
fn eq(&self, other: &&mut B) -> bool {
2259-
PartialEq::eq(*self, *other)
2260-
}
2261-
#[inline]
2262-
fn ne(&self, other: &&mut B) -> bool {
2263-
PartialEq::ne(*self, *other)
2264-
}
2265-
}
2190+
impl_partial_ord!(<A, B> for (&A, &B) /*(&A, &mut B) (&mut A, &B)*/ (&mut A, &mut B));
22662191

2267-
#[stable(feature = "rust1", since = "1.0.0")]
2268-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2269-
impl<A: PointeeSized, B: PointeeSized> const PartialEq<&B> for &mut A
2270-
where
2271-
A: [const] PartialEq<B>,
2272-
{
2273-
#[inline]
2274-
fn eq(&self, other: &&B) -> bool {
2275-
PartialEq::eq(*self, *other)
2276-
}
2277-
#[inline]
2278-
fn ne(&self, other: &&B) -> bool {
2279-
PartialEq::ne(*self, *other)
2280-
}
2192+
macro_rules! impl_ord_eq {
2193+
(<$A:ident> for $($ref_A:ty),*) => ($(
2194+
#[stable(feature = "rust1", since = "1.0.0")]
2195+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2196+
impl<$A: [const] Ord + PointeeSized> const Ord for $ref_A
2197+
{
2198+
#[inline]
2199+
fn cmp(&self, other: &Self) -> Ordering {
2200+
Ord::cmp(*self, *other)
2201+
}
2202+
}
2203+
2204+
#[stable(feature = "rust1", since = "1.0.0")]
2205+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2206+
impl<$A: [const] Eq + PointeeSized> const Eq for $ref_A {}
2207+
)*)
22812208
}
2209+
2210+
impl_ord_eq!(<A> for &A, &mut A);
22822211
}

0 commit comments

Comments
 (0)