Skip to content

Commit 187c020

Browse files
committed
reduce repetition with macros
1 parent fc78970 commit 187c020

File tree

1 file changed

+86
-175
lines changed

1 file changed

+86
-175
lines changed

library/core/src/cmp.rs

Lines changed: 86 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,187 +2019,98 @@ mod impls {
20192019
}
20202020
}
20212021

2022-
// & pointers
2022+
// reference types
20232023

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

2096-
// &mut pointers
2047+
partial_eq_impl!((&A => A, &B => B) (&A => A, &mut B => B) (&mut A => A, &B => B) (&mut A => A, &mut B => B));
20972048

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

2170-
#[stable(feature = "rust1", since = "1.0.0")]
2171-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2172-
impl<A: PointeeSized, B: PointeeSized> const PartialEq<&mut B> for &A
2173-
where
2174-
A: [const] PartialEq<B>,
2175-
{
2176-
#[inline]
2177-
fn eq(&self, other: &&mut B) -> bool {
2178-
PartialEq::eq(*self, *other)
2179-
}
2180-
// if <A as PartialEq<B>>::ne uses inline assembly or FFI, then
2181-
// this forwarding impl may be more efficient than the default impl
2182-
#[inline]
2183-
fn ne(&self, other: &&mut B) -> bool {
2184-
PartialEq::ne(*self, *other)
2185-
}
2186-
}
2097+
partial_ord_impl!((&A => A, &B => B) /*(&A => A, &mut B => B) (&mut A => A, &B => B)*/ (&mut A => A, &mut B => B));
21872098

2188-
#[stable(feature = "rust1", since = "1.0.0")]
2189-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2190-
impl<A: PointeeSized, B: PointeeSized> const PartialEq<&B> for &mut A
2191-
where
2192-
A: [const] PartialEq<B>,
2193-
{
2194-
#[inline]
2195-
fn eq(&self, other: &&B) -> bool {
2196-
PartialEq::eq(*self, *other)
2197-
}
2198-
// if <A as PartialEq<B>>::ne uses inline assembly or FFI, then
2199-
// this forwarding impl may be more efficient than the default impl
2200-
#[inline]
2201-
fn ne(&self, other: &&B) -> bool {
2202-
PartialEq::ne(*self, *other)
2203-
}
2099+
macro_rules! ord_eq_impl {
2100+
($($ref_A:ty => $A:ident),*) => ($(
2101+
#[stable(feature = "rust1", since = "1.0.0")]
2102+
impl<$A: Ord + PointeeSized> Ord for $ref_A
2103+
{
2104+
#[inline]
2105+
fn cmp(&self, other: &Self) -> Ordering {
2106+
Ord::cmp(*self, *other)
2107+
}
2108+
}
2109+
2110+
#[stable(feature = "rust1", since = "1.0.0")]
2111+
impl<$A: Eq + PointeeSized> Eq for $ref_A {}
2112+
)*)
22042113
}
2114+
2115+
ord_eq_impl!(&A => A, &mut A => A);
22052116
}

0 commit comments

Comments
 (0)