Skip to content

Commit fbc5eeb

Browse files
committed
reduce repetition with macros
1 parent c247336 commit fbc5eeb

File tree

1 file changed

+89
-181
lines changed

1 file changed

+89
-181
lines changed

library/core/src/cmp.rs

Lines changed: 89 additions & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -2069,193 +2069,101 @@ mod impls {
20692069
}
20702070
}
20712071

2072-
// & pointers
2072+
// reference types
20732073

2074-
#[stable(feature = "rust1", since = "1.0.0")]
2075-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2076-
impl<A: PointeeSized, B: PointeeSized> const PartialEq<&B> for &A
2077-
where
2078-
A: [const] PartialEq<B>,
2079-
{
2080-
#[inline]
2081-
fn eq(&self, other: &&B) -> bool {
2082-
PartialEq::eq(*self, *other)
2083-
}
2084-
// if <A as PartialEq<B>>::ne uses inline assembly or FFI, then
2085-
// this forwarding impl may be more efficient than the default impl
2086-
#[inline]
2087-
fn ne(&self, other: &&B) -> bool {
2088-
PartialEq::ne(*self, *other)
2089-
}
2090-
}
2091-
#[stable(feature = "rust1", since = "1.0.0")]
2092-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2093-
impl<A: PointeeSized, B: PointeeSized> const PartialOrd<&B> for &A
2094-
where
2095-
A: [const] PartialOrd<B>,
2096-
{
2097-
#[inline]
2098-
fn partial_cmp(&self, other: &&B) -> Option<Ordering> {
2099-
PartialOrd::partial_cmp(*self, *other)
2100-
}
2101-
#[inline]
2102-
fn lt(&self, other: &&B) -> bool {
2103-
PartialOrd::lt(*self, *other)
2104-
}
2105-
#[inline]
2106-
fn le(&self, other: &&B) -> bool {
2107-
PartialOrd::le(*self, *other)
2108-
}
2109-
#[inline]
2110-
fn gt(&self, other: &&B) -> bool {
2111-
PartialOrd::gt(*self, *other)
2112-
}
2113-
#[inline]
2114-
fn ge(&self, other: &&B) -> bool {
2115-
PartialOrd::ge(*self, *other)
2116-
}
2117-
#[inline]
2118-
fn __chaining_lt(&self, other: &&B) -> ControlFlow<bool> {
2119-
PartialOrd::__chaining_lt(*self, *other)
2120-
}
2121-
#[inline]
2122-
fn __chaining_le(&self, other: &&B) -> ControlFlow<bool> {
2123-
PartialOrd::__chaining_le(*self, *other)
2124-
}
2125-
#[inline]
2126-
fn __chaining_gt(&self, other: &&B) -> ControlFlow<bool> {
2127-
PartialOrd::__chaining_gt(*self, *other)
2128-
}
2129-
#[inline]
2130-
fn __chaining_ge(&self, other: &&B) -> ControlFlow<bool> {
2131-
PartialOrd::__chaining_ge(*self, *other)
2132-
}
2133-
}
2134-
#[stable(feature = "rust1", since = "1.0.0")]
2135-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2136-
impl<A: PointeeSized> const Ord for &A
2137-
where
2138-
A: [const] Ord,
2139-
{
2140-
#[inline]
2141-
fn cmp(&self, other: &Self) -> Ordering {
2142-
Ord::cmp(*self, *other)
2143-
}
2074+
macro_rules! partial_eq_impl {
2075+
($(($ref_A:ty => $A:ident, $ref_B:ty => $B:ident))*) => ($(
2076+
#[stable(feature = "rust1", since = "1.0.0")]
2077+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2078+
impl<$A, $B> const PartialEq<$ref_B> for $ref_A
2079+
where
2080+
$A: [const] PartialEq<$B> + PointeeSized,
2081+
$B: PointeeSized,
2082+
{
2083+
#[inline]
2084+
fn eq(&self, other: &$ref_B) -> bool {
2085+
PartialEq::eq(*self, *other)
2086+
}
2087+
// if <A as PartialEq<B>>::ne uses inline assembly or FFI, then
2088+
// this forwarding impl may be more efficient than the default impl
2089+
#[inline]
2090+
fn ne(&self, other: &$ref_B) -> bool {
2091+
PartialEq::ne(*self, *other)
2092+
}
2093+
}
2094+
)*)
21442095
}
2145-
#[stable(feature = "rust1", since = "1.0.0")]
2146-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2147-
impl<A: PointeeSized> const Eq for &A where A: [const] Eq {}
21482096

2149-
// &mut pointers
2097+
partial_eq_impl!((&A => A, &B => B) (&A => A, &mut B => B) (&mut A => A, &B => B) (&mut A => A, &mut B => B));
21502098

2151-
#[stable(feature = "rust1", since = "1.0.0")]
2152-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2153-
impl<A: PointeeSized, B: PointeeSized> const PartialEq<&mut B> for &mut A
2154-
where
2155-
A: [const] PartialEq<B>,
2156-
{
2157-
#[inline]
2158-
fn eq(&self, other: &&mut B) -> bool {
2159-
PartialEq::eq(*self, *other)
2160-
}
2161-
// if <A as PartialEq<B>>::ne uses inline assembly or FFI, then
2162-
// this forwarding impl may be more efficient than the default impl
2163-
#[inline]
2164-
fn ne(&self, other: &&mut B) -> bool {
2165-
PartialEq::ne(*self, *other)
2166-
}
2167-
}
2168-
#[stable(feature = "rust1", since = "1.0.0")]
2169-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2170-
impl<A: PointeeSized, B: PointeeSized> const PartialOrd<&mut B> for &mut A
2171-
where
2172-
A: [const] PartialOrd<B>,
2173-
{
2174-
#[inline]
2175-
fn partial_cmp(&self, other: &&mut B) -> Option<Ordering> {
2176-
PartialOrd::partial_cmp(*self, *other)
2177-
}
2178-
#[inline]
2179-
fn lt(&self, other: &&mut B) -> bool {
2180-
PartialOrd::lt(*self, *other)
2181-
}
2182-
#[inline]
2183-
fn le(&self, other: &&mut B) -> bool {
2184-
PartialOrd::le(*self, *other)
2185-
}
2186-
#[inline]
2187-
fn gt(&self, other: &&mut B) -> bool {
2188-
PartialOrd::gt(*self, *other)
2189-
}
2190-
#[inline]
2191-
fn ge(&self, other: &&mut B) -> bool {
2192-
PartialOrd::ge(*self, *other)
2193-
}
2194-
#[inline]
2195-
fn __chaining_lt(&self, other: &&mut B) -> ControlFlow<bool> {
2196-
PartialOrd::__chaining_lt(*self, *other)
2197-
}
2198-
#[inline]
2199-
fn __chaining_le(&self, other: &&mut B) -> ControlFlow<bool> {
2200-
PartialOrd::__chaining_le(*self, *other)
2201-
}
2202-
#[inline]
2203-
fn __chaining_gt(&self, other: &&mut B) -> ControlFlow<bool> {
2204-
PartialOrd::__chaining_gt(*self, *other)
2205-
}
2206-
#[inline]
2207-
fn __chaining_ge(&self, other: &&mut B) -> ControlFlow<bool> {
2208-
PartialOrd::__chaining_ge(*self, *other)
2209-
}
2210-
}
2211-
#[stable(feature = "rust1", since = "1.0.0")]
2212-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2213-
impl<A: PointeeSized> const Ord for &mut A
2214-
where
2215-
A: [const] Ord,
2216-
{
2217-
#[inline]
2218-
fn cmp(&self, other: &Self) -> Ordering {
2219-
Ord::cmp(*self, *other)
2220-
}
2099+
macro_rules! partial_ord_impl {
2100+
($(($ref_A:ty => $A:ident, $ref_B:ty => $B:ident))*) => ($(
2101+
#[stable(feature = "rust1", since = "1.0.0")]
2102+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2103+
impl<$A, $B> const PartialOrd<$ref_B> for $ref_A
2104+
where
2105+
$A: [const] PartialOrd<$B> + PointeeSized,
2106+
$B: PointeeSized,
2107+
{
2108+
#[inline]
2109+
fn partial_cmp(&self, other: &$ref_B) -> Option<Ordering> {
2110+
PartialOrd::partial_cmp(*self, *other)
2111+
}
2112+
#[inline]
2113+
fn lt(&self, other: &$ref_B) -> bool {
2114+
PartialOrd::lt(*self, *other)
2115+
}
2116+
#[inline]
2117+
fn le(&self, other: &$ref_B) -> bool {
2118+
PartialOrd::le(*self, *other)
2119+
}
2120+
#[inline]
2121+
fn gt(&self, other: &$ref_B) -> bool {
2122+
PartialOrd::gt(*self, *other)
2123+
}
2124+
#[inline]
2125+
fn ge(&self, other: &$ref_B) -> bool {
2126+
PartialOrd::ge(*self, *other)
2127+
}
2128+
#[inline]
2129+
fn __chaining_lt(&self, other: &$ref_B) -> ControlFlow<bool> {
2130+
PartialOrd::__chaining_lt(*self, *other)
2131+
}
2132+
#[inline]
2133+
fn __chaining_le(&self, other: &$ref_B) -> ControlFlow<bool> {
2134+
PartialOrd::__chaining_le(*self, *other)
2135+
}
2136+
#[inline]
2137+
fn __chaining_gt(&self, other: &$ref_B) -> ControlFlow<bool> {
2138+
PartialOrd::__chaining_gt(*self, *other)
2139+
}
2140+
#[inline]
2141+
fn __chaining_ge(&self, other: &$ref_B) -> ControlFlow<bool> {
2142+
PartialOrd::__chaining_ge(*self, *other)
2143+
}
2144+
}
2145+
)*)
22212146
}
2222-
#[stable(feature = "rust1", since = "1.0.0")]
2223-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2224-
impl<A: PointeeSized> const Eq for &mut A where A: [const] Eq {}
22252147

2226-
#[stable(feature = "rust1", since = "1.0.0")]
2227-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2228-
impl<A: PointeeSized, B: PointeeSized> const PartialEq<&mut B> for &A
2229-
where
2230-
A: [const] PartialEq<B>,
2231-
{
2232-
#[inline]
2233-
fn eq(&self, other: &&mut B) -> bool {
2234-
PartialEq::eq(*self, *other)
2235-
}
2236-
// if <A as PartialEq<B>>::ne uses inline assembly or FFI, then
2237-
// this forwarding impl may be more efficient than the default impl
2238-
#[inline]
2239-
fn ne(&self, other: &&mut B) -> bool {
2240-
PartialEq::ne(*self, *other)
2241-
}
2242-
}
2148+
partial_ord_impl!((&A => A, &B => B) /*(&A => A, &mut B => B) (&mut A => A, &B => B)*/ (&mut A => A, &mut B => B));
22432149

2244-
#[stable(feature = "rust1", since = "1.0.0")]
2245-
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2246-
impl<A: PointeeSized, B: PointeeSized> const PartialEq<&B> for &mut A
2247-
where
2248-
A: [const] PartialEq<B>,
2249-
{
2250-
#[inline]
2251-
fn eq(&self, other: &&B) -> bool {
2252-
PartialEq::eq(*self, *other)
2253-
}
2254-
// if <A as PartialEq<B>>::ne uses inline assembly or FFI, then
2255-
// this forwarding impl may be more efficient than the default impl
2256-
#[inline]
2257-
fn ne(&self, other: &&B) -> bool {
2258-
PartialEq::ne(*self, *other)
2259-
}
2150+
macro_rules! ord_eq_impl {
2151+
($($ref_A:ty => $A:ident),*) => ($(
2152+
#[stable(feature = "rust1", since = "1.0.0")]
2153+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2154+
impl<$A: [const] Ord + PointeeSized> const Ord for $ref_A
2155+
{
2156+
#[inline]
2157+
fn cmp(&self, other: &Self) -> Ordering {
2158+
Ord::cmp(*self, *other)
2159+
}
2160+
}
2161+
2162+
#[stable(feature = "rust1", since = "1.0.0")]
2163+
#[rustc_const_unstable(feature = "const_cmp", issue = "143800")]
2164+
impl<$A: [const] Eq + PointeeSized> const Eq for $ref_A {}
2165+
)*)
22602166
}
2167+
2168+
ord_eq_impl!(&A => A, &mut A => A);
22612169
}

0 commit comments

Comments
 (0)