Skip to content

Commit a95a0d3

Browse files
committed
reduce repetition with macros
1 parent 77c2123 commit a95a0d3

File tree

1 file changed

+89
-173
lines changed

1 file changed

+89
-173
lines changed

library/core/src/cmp.rs

Lines changed: 89 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,185 +2092,101 @@ mod impls {
20922092
}
20932093
}
20942094

2095-
// & pointers
2095+
// reference types
20962096

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

2170-
// &mut pointers
2120+
impl_partial_eq!(<A, B> for (&A, &B) (&A, &mut B) (&mut A, &B) (&mut A, &mut B));
21712121

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

2245-
#[stable(feature = "rust1", since = "1.0.0")]
2246-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2247-
impl<A: PointeeSized, B: PointeeSized> const PartialEq<&mut B> for &A
2248-
where
2249-
A: [const] PartialEq<B>,
2250-
{
2251-
#[inline]
2252-
fn eq(&self, other: &&mut B) -> bool {
2253-
PartialEq::eq(*self, *other)
2254-
}
2255-
#[inline]
2256-
fn ne(&self, other: &&mut B) -> bool {
2257-
PartialEq::ne(*self, *other)
2258-
}
2259-
}
2171+
impl_partial_ord!(<A, B> for (&A, &B) /*(&A, &mut B) (&mut A, &B)*/ (&mut A, &mut B));
22602172

2261-
#[stable(feature = "rust1", since = "1.0.0")]
2262-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2263-
impl<A: PointeeSized, B: PointeeSized> const PartialEq<&B> for &mut A
2264-
where
2265-
A: [const] PartialEq<B>,
2266-
{
2267-
#[inline]
2268-
fn eq(&self, other: &&B) -> bool {
2269-
PartialEq::eq(*self, *other)
2270-
}
2271-
#[inline]
2272-
fn ne(&self, other: &&B) -> bool {
2273-
PartialEq::ne(*self, *other)
2274-
}
2173+
macro_rules! impl_ord_eq {
2174+
(<$A:ident> for $($ref_A:ty),*) => ($(
2175+
#[stable(feature = "rust1", since = "1.0.0")]
2176+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2177+
impl<$A: [const] Ord + PointeeSized> const Ord for $ref_A
2178+
{
2179+
#[inline]
2180+
fn cmp(&self, other: &Self) -> Ordering {
2181+
Ord::cmp(*self, *other)
2182+
}
2183+
}
2184+
2185+
#[stable(feature = "rust1", since = "1.0.0")]
2186+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2187+
impl<$A: [const] Eq + PointeeSized> const Eq for $ref_A {}
2188+
)*)
22752189
}
2190+
2191+
impl_ord_eq!(<A> for &A, &mut A);
22762192
}

0 commit comments

Comments
 (0)