Skip to content

Commit 88f2556

Browse files
committed
reduce repetition with macros
1 parent 48b0eff commit 88f2556

File tree

1 file changed

+88
-179
lines changed

1 file changed

+88
-179
lines changed

library/core/src/cmp.rs

Lines changed: 88 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,191 +2012,100 @@ mod impls {
20122012
}
20132013
}
20142014

2015-
// & pointers
2015+
// reference types
20162016

2017-
#[stable(feature = "rust1", since = "1.0.0")]
2018-
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
2019-
impl<A: PointeeSized, B: PointeeSized> const PartialEq<&B> for &A
2020-
where
2021-
A: ~const PartialEq<B>,
2022-
{
2023-
#[inline]
2024-
fn eq(&self, other: &&B) -> bool {
2025-
PartialEq::eq(*self, *other)
2026-
}
2027-
// if <A as PartialEq<B>>::ne uses inline assembly or FFI, then
2028-
// this forwarding impl may be more efficient than the default impl
2029-
#[expect(clippy::partialeq_ne_impl)]
2030-
#[inline]
2031-
fn ne(&self, other: &&B) -> bool {
2032-
PartialEq::ne(*self, *other)
2033-
}
2034-
}
2035-
#[stable(feature = "rust1", since = "1.0.0")]
2036-
impl<A: PointeeSized, B: PointeeSized> PartialOrd<&B> for &A
2037-
where
2038-
A: PartialOrd<B>,
2039-
{
2040-
#[inline]
2041-
fn partial_cmp(&self, other: &&B) -> Option<Ordering> {
2042-
PartialOrd::partial_cmp(*self, *other)
2043-
}
2044-
#[inline]
2045-
fn lt(&self, other: &&B) -> bool {
2046-
PartialOrd::lt(*self, *other)
2047-
}
2048-
#[inline]
2049-
fn le(&self, other: &&B) -> bool {
2050-
PartialOrd::le(*self, *other)
2051-
}
2052-
#[inline]
2053-
fn gt(&self, other: &&B) -> bool {
2054-
PartialOrd::gt(*self, *other)
2055-
}
2056-
#[inline]
2057-
fn ge(&self, other: &&B) -> bool {
2058-
PartialOrd::ge(*self, *other)
2059-
}
2060-
#[inline]
2061-
fn __chaining_lt(&self, other: &&B) -> ControlFlow<bool> {
2062-
PartialOrd::__chaining_lt(*self, *other)
2063-
}
2064-
#[inline]
2065-
fn __chaining_le(&self, other: &&B) -> ControlFlow<bool> {
2066-
PartialOrd::__chaining_le(*self, *other)
2067-
}
2068-
#[inline]
2069-
fn __chaining_gt(&self, other: &&B) -> ControlFlow<bool> {
2070-
PartialOrd::__chaining_gt(*self, *other)
2071-
}
2072-
#[inline]
2073-
fn __chaining_ge(&self, other: &&B) -> ControlFlow<bool> {
2074-
PartialOrd::__chaining_ge(*self, *other)
2075-
}
2076-
}
2077-
#[stable(feature = "rust1", since = "1.0.0")]
2078-
impl<A: PointeeSized> Ord for &A
2079-
where
2080-
A: Ord,
2081-
{
2082-
#[inline]
2083-
fn cmp(&self, other: &Self) -> Ordering {
2084-
Ord::cmp(*self, *other)
2085-
}
2017+
macro_rules! partial_eq_impl {
2018+
($(($ref_A:ty => $A:ident, $ref_B:ty => $B:ident))*) => ($(
2019+
#[stable(feature = "rust1", since = "1.0.0")]
2020+
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
2021+
impl<$A: PointeeSized, $B: PointeeSized> const PartialEq<$ref_B> for $ref_A
2022+
where
2023+
$A: ~const PartialEq<$B>,
2024+
{
2025+
#[inline]
2026+
fn eq(&self, other: &$ref_B) -> bool {
2027+
PartialEq::eq(*self, *other)
2028+
}
2029+
// if <A as PartialEq<B>>::ne uses inline assembly or FFI, then
2030+
// this forwarding impl may be more efficient than the default impl
2031+
#[expect(clippy::partialeq_ne_impl)]
2032+
#[inline]
2033+
fn ne(&self, other: &$ref_B) -> bool {
2034+
PartialEq::ne(*self, *other)
2035+
}
2036+
}
2037+
)*)
20862038
}
2087-
#[stable(feature = "rust1", since = "1.0.0")]
2088-
impl<A: PointeeSized> Eq for &A where A: Eq {}
20892039

2090-
// &mut pointers
2040+
partial_eq_impl!((&A => A, &B => B) (&A => A, &mut B => B) (&mut A => A, &B => B) (&mut A => A, &mut B => B));
20912041

2092-
#[stable(feature = "rust1", since = "1.0.0")]
2093-
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
2094-
impl<A: PointeeSized, B: PointeeSized> const PartialEq<&mut B> for &mut A
2095-
where
2096-
A: ~const PartialEq<B>,
2097-
{
2098-
#[inline]
2099-
fn eq(&self, other: &&mut B) -> bool {
2100-
PartialEq::eq(*self, *other)
2101-
}
2102-
// if <A as PartialEq<B>>::ne uses inline assembly or FFI, then
2103-
// this forwarding impl may be more efficient than the default impl
2104-
#[expect(clippy::partialeq_ne_impl)]
2105-
#[inline]
2106-
fn ne(&self, other: &&mut B) -> bool {
2107-
PartialEq::ne(*self, *other)
2108-
}
2109-
}
2110-
#[stable(feature = "rust1", since = "1.0.0")]
2111-
impl<A: PointeeSized, B: PointeeSized> PartialOrd<&mut B> for &mut A
2112-
where
2113-
A: PartialOrd<B>,
2114-
{
2115-
#[inline]
2116-
fn partial_cmp(&self, other: &&mut B) -> Option<Ordering> {
2117-
PartialOrd::partial_cmp(*self, *other)
2118-
}
2119-
#[inline]
2120-
fn lt(&self, other: &&mut B) -> bool {
2121-
PartialOrd::lt(*self, *other)
2122-
}
2123-
#[inline]
2124-
fn le(&self, other: &&mut B) -> bool {
2125-
PartialOrd::le(*self, *other)
2126-
}
2127-
#[inline]
2128-
fn gt(&self, other: &&mut B) -> bool {
2129-
PartialOrd::gt(*self, *other)
2130-
}
2131-
#[inline]
2132-
fn ge(&self, other: &&mut B) -> bool {
2133-
PartialOrd::ge(*self, *other)
2134-
}
2135-
#[inline]
2136-
fn __chaining_lt(&self, other: &&mut B) -> ControlFlow<bool> {
2137-
PartialOrd::__chaining_lt(*self, *other)
2138-
}
2139-
#[inline]
2140-
fn __chaining_le(&self, other: &&mut B) -> ControlFlow<bool> {
2141-
PartialOrd::__chaining_le(*self, *other)
2142-
}
2143-
#[inline]
2144-
fn __chaining_gt(&self, other: &&mut B) -> ControlFlow<bool> {
2145-
PartialOrd::__chaining_gt(*self, *other)
2146-
}
2147-
#[inline]
2148-
fn __chaining_ge(&self, other: &&mut B) -> ControlFlow<bool> {
2149-
PartialOrd::__chaining_ge(*self, *other)
2150-
}
2151-
}
2152-
#[stable(feature = "rust1", since = "1.0.0")]
2153-
impl<A: PointeeSized> Ord for &mut A
2154-
where
2155-
A: Ord,
2156-
{
2157-
#[inline]
2158-
fn cmp(&self, other: &Self) -> Ordering {
2159-
Ord::cmp(*self, *other)
2160-
}
2042+
macro_rules! partial_ord_impl {
2043+
($(($ref_A:ty => $A:ident, $ref_B:ty => $B:ident))*) => ($(
2044+
#[stable(feature = "rust1", since = "1.0.0")]
2045+
impl<$A: PointeeSized, $B: PointeeSized> PartialOrd<$ref_B> for $ref_A
2046+
where
2047+
$A: PartialOrd<$B>,
2048+
{
2049+
#[inline]
2050+
fn partial_cmp(&self, other: &$ref_B) -> Option<Ordering> {
2051+
PartialOrd::partial_cmp(*self, *other)
2052+
}
2053+
#[inline]
2054+
fn lt(&self, other: &$ref_B) -> bool {
2055+
PartialOrd::lt(*self, *other)
2056+
}
2057+
#[inline]
2058+
fn le(&self, other: &$ref_B) -> bool {
2059+
PartialOrd::le(*self, *other)
2060+
}
2061+
#[inline]
2062+
fn gt(&self, other: &$ref_B) -> bool {
2063+
PartialOrd::gt(*self, *other)
2064+
}
2065+
#[inline]
2066+
fn ge(&self, other: &$ref_B) -> bool {
2067+
PartialOrd::ge(*self, *other)
2068+
}
2069+
#[inline]
2070+
fn __chaining_lt(&self, other: &$ref_B) -> ControlFlow<bool> {
2071+
PartialOrd::__chaining_lt(*self, *other)
2072+
}
2073+
#[inline]
2074+
fn __chaining_le(&self, other: &$ref_B) -> ControlFlow<bool> {
2075+
PartialOrd::__chaining_le(*self, *other)
2076+
}
2077+
#[inline]
2078+
fn __chaining_gt(&self, other: &$ref_B) -> ControlFlow<bool> {
2079+
PartialOrd::__chaining_gt(*self, *other)
2080+
}
2081+
#[inline]
2082+
fn __chaining_ge(&self, other: &$ref_B) -> ControlFlow<bool> {
2083+
PartialOrd::__chaining_ge(*self, *other)
2084+
}
2085+
}
2086+
)*)
21612087
}
2162-
#[stable(feature = "rust1", since = "1.0.0")]
2163-
impl<A: PointeeSized> Eq for &mut A where A: Eq {}
21642088

2165-
#[stable(feature = "rust1", since = "1.0.0")]
2166-
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
2167-
impl<A: PointeeSized, B: PointeeSized> const PartialEq<&mut B> for &A
2168-
where
2169-
A: ~const PartialEq<B>,
2170-
{
2171-
#[inline]
2172-
fn eq(&self, other: &&mut B) -> bool {
2173-
PartialEq::eq(*self, *other)
2174-
}
2175-
// if <A as PartialEq<B>>::ne uses inline assembly or FFI, then
2176-
// this forwarding impl may be more efficient than the default impl
2177-
#[expect(clippy::partialeq_ne_impl)]
2178-
#[inline]
2179-
fn ne(&self, other: &&mut B) -> bool {
2180-
PartialEq::ne(*self, *other)
2181-
}
2182-
}
2089+
partial_ord_impl!((&A => A, &B => B) /*(&A => A, &mut B => B) (&mut A => A, &B => B)*/ (&mut A => A, &mut B => B));
21832090

2184-
#[stable(feature = "rust1", since = "1.0.0")]
2185-
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
2186-
impl<A: PointeeSized, B: PointeeSized> const PartialEq<&B> for &mut A
2187-
where
2188-
A: ~const PartialEq<B>,
2189-
{
2190-
#[inline]
2191-
fn eq(&self, other: &&B) -> bool {
2192-
PartialEq::eq(*self, *other)
2193-
}
2194-
// if <A as PartialEq<B>>::ne uses inline assembly or FFI, then
2195-
// this forwarding impl may be more efficient than the default impl
2196-
#[expect(clippy::partialeq_ne_impl)]
2197-
#[inline]
2198-
fn ne(&self, other: &&B) -> bool {
2199-
PartialEq::ne(*self, *other)
2200-
}
2091+
macro_rules! ord_eq_impl {
2092+
($($ref_A:ty => $A:ident),*) => ($(
2093+
#[stable(feature = "rust1", since = "1.0.0")]
2094+
impl<$A: PointeeSized> Ord for $ref_A
2095+
where
2096+
$A: Ord,
2097+
{
2098+
#[inline]
2099+
fn cmp(&self, other: &Self) -> Ordering {
2100+
Ord::cmp(*self, *other)
2101+
}
2102+
}
2103+
2104+
#[stable(feature = "rust1", since = "1.0.0")]
2105+
impl<$A: PointeeSized> Eq for $ref_A where $A: Eq {}
2106+
2107+
)*)
22012108
}
2109+
2110+
ord_eq_impl!(&A => A, &mut A => A);
22022111
}

0 commit comments

Comments
 (0)