Skip to content

Commit 77c2123

Browse files
committed
Better docs for PartialEq
1 parent 2ebb126 commit 77c2123

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

library/core/src/cmp.rs

Lines changed: 25 additions & 8 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")]
@@ -1854,19 +1863,31 @@ mod impls {
18541863
use crate::marker::PointeeSized;
18551864
use crate::ops::ControlFlow::{self, Break, Continue};
18561865

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

1887+
impl_partial_eq_for_primitive! {
1888+
bool char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f16 f32 f64 f128
1889+
}
1890+
18701891
#[stable(feature = "rust1", since = "1.0.0")]
18711892
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
18721893
impl const PartialEq for () {
@@ -1880,10 +1901,6 @@ mod impls {
18801901
}
18811902
}
18821903

1883-
partial_eq_impl! {
1884-
bool char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f16 f32 f64 f128
1885-
}
1886-
18871904
macro_rules! eq_impl {
18881905
($($t:ty)*) => ($(
18891906
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)