Skip to content

Commit 9c60f35

Browse files
committed
reduce repetition with macros
1 parent 2642b81 commit 9c60f35

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
@@ -2008,187 +2008,98 @@ mod impls {
20082008
}
20092009
}
20102010

2011-
// & pointers
2011+
// reference types
20122012

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

2085-
// &mut pointers
2036+
partial_eq_impl!((&A => A, &B => B) (&A => A, &mut B => B) (&mut A => A, &B => B) (&mut A => A, &mut B => B));
20862037

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

2159-
#[stable(feature = "rust1", since = "1.0.0")]
2160-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2161-
impl<A: PointeeSized, B: PointeeSized> const PartialEq<&mut B> for &A
2162-
where
2163-
A: ~const PartialEq<B>,
2164-
{
2165-
#[inline]
2166-
fn eq(&self, other: &&mut B) -> bool {
2167-
PartialEq::eq(*self, *other)
2168-
}
2169-
// if <A as PartialEq<B>>::ne uses inline assembly or FFI, then
2170-
// this forwarding impl may be more efficient than the default impl
2171-
#[inline]
2172-
fn ne(&self, other: &&mut B) -> bool {
2173-
PartialEq::ne(*self, *other)
2174-
}
2175-
}
2086+
partial_ord_impl!((&A => A, &B => B) /*(&A => A, &mut B => B) (&mut A => A, &B => B)*/ (&mut A => A, &mut B => B));
21762087

2177-
#[stable(feature = "rust1", since = "1.0.0")]
2178-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2179-
impl<A: PointeeSized, B: PointeeSized> const PartialEq<&B> for &mut A
2180-
where
2181-
A: ~const PartialEq<B>,
2182-
{
2183-
#[inline]
2184-
fn eq(&self, other: &&B) -> bool {
2185-
PartialEq::eq(*self, *other)
2186-
}
2187-
// if <A as PartialEq<B>>::ne uses inline assembly or FFI, then
2188-
// this forwarding impl may be more efficient than the default impl
2189-
#[inline]
2190-
fn ne(&self, other: &&B) -> bool {
2191-
PartialEq::ne(*self, *other)
2192-
}
2088+
macro_rules! ord_eq_impl {
2089+
($($ref_A:ty => $A:ident),*) => ($(
2090+
#[stable(feature = "rust1", since = "1.0.0")]
2091+
impl<$A: Ord + PointeeSized> Ord for $ref_A
2092+
{
2093+
#[inline]
2094+
fn cmp(&self, other: &Self) -> Ordering {
2095+
Ord::cmp(*self, *other)
2096+
}
2097+
}
2098+
2099+
#[stable(feature = "rust1", since = "1.0.0")]
2100+
impl<$A: Eq + PointeeSized> Eq for $ref_A {}
2101+
)*)
21932102
}
2103+
2104+
ord_eq_impl!(&A => A, &mut A => A);
21942105
}

0 commit comments

Comments
 (0)