1
Fork 0

Use private trait for Rc/Arc Eq specialization

This commit is contained in:
Thomas Heck 2018-12-05 21:43:44 +01:00
parent 2a916a617f
commit 40d60a4608
2 changed files with 74 additions and 37 deletions

View file

@ -8,7 +8,6 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
#![feature(specialization)]
#![allow(deprecated)] #![allow(deprecated)]
//! Single-threaded reference-counting pointers. 'Rc' stands for 'Reference //! Single-threaded reference-counting pointers. 'Rc' stands for 'Reference
@ -901,6 +900,38 @@ impl<T: Default> Default for Rc<T> {
} }
} }
#[stable(feature = "rust1", since = "1.0.0")]
trait RcEqIdent<T: ?Sized + PartialEq> {
fn eq(&self, other: &Rc<T>) -> bool;
fn ne(&self, other: &Rc<T>) -> bool;
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized + PartialEq> RcEqIdent<T> for Rc<T> {
#[inline]
default fn eq(&self, other: &Rc<T>) -> bool {
**self == **other
}
#[inline]
default fn ne(&self, other: &Rc<T>) -> bool {
**self != **other
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized + Eq> RcEqIdent<T> for Rc<T> {
#[inline]
fn eq(&self, other: &Rc<T>) -> bool {
Rc::ptr_eq(self, other) || **self == **other
}
#[inline]
fn ne(&self, other: &Rc<T>) -> bool {
!Rc::ptr_eq(self, other) && **self != **other
}
}
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized + PartialEq> PartialEq for Rc<T> { impl<T: ?Sized + PartialEq> PartialEq for Rc<T> {
/// Equality for two `Rc`s. /// Equality for two `Rc`s.
@ -919,9 +950,9 @@ impl<T: ?Sized + PartialEq> PartialEq for Rc<T> {
/// ///
/// assert!(five == Rc::new(5)); /// assert!(five == Rc::new(5));
/// ``` /// ```
#[inline(always)] #[inline]
default fn eq(&self, other: &Rc<T>) -> bool { fn eq(&self, other: &Rc<T>) -> bool {
**self == **other RcEqIdent::eq(self, other)
} }
/// Inequality for two `Rc`s. /// Inequality for two `Rc`s.
@ -940,23 +971,9 @@ impl<T: ?Sized + PartialEq> PartialEq for Rc<T> {
/// ///
/// assert!(five != Rc::new(6)); /// assert!(five != Rc::new(6));
/// ``` /// ```
#[inline(always)] #[inline]
default fn ne(&self, other: &Rc<T>) -> bool {
**self != **other
}
}
#[doc(hidden)]
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized + Eq> PartialEq for Rc<T> {
#[inline(always)]
fn eq(&self, other: &Rc<T>) -> bool {
Rc::ptr_eq(self, other) || **self == **other
}
#[inline(always)]
fn ne(&self, other: &Rc<T>) -> bool { fn ne(&self, other: &Rc<T>) -> bool {
!Rc::ptr_eq(self, other) && **self != **other RcEqIdent::ne(self, other)
} }
} }

View file

@ -8,7 +8,6 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
#![feature(specialization)]
#![stable(feature = "rust1", since = "1.0.0")] #![stable(feature = "rust1", since = "1.0.0")]
//! Thread-safe reference-counting pointers. //! Thread-safe reference-counting pointers.
@ -1288,6 +1287,37 @@ impl<T: ?Sized> Drop for Weak<T> {
} }
} }
#[stable(feature = "rust1", since = "1.0.0")]
trait ArcEqIdent<T: ?Sized + PartialEq> {
fn eq(&self, other: &Arc<T>) -> bool;
fn ne(&self, other: &Arc<T>) -> bool;
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized + PartialEq> ArcEqIdent<T> for Arc<T> {
#[inline]
default fn eq(&self, other: &Arc<T>) -> bool {
**self == **other
}
#[inline]
default fn ne(&self, other: &Arc<T>) -> bool {
**self != **other
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized + Eq> ArcEqIdent<T> for Arc<T> {
#[inline]
fn eq(&self, other: &Arc<T>) -> bool {
Arc::ptr_eq(self, other) || **self == **other
}
#[inline]
fn ne(&self, other: &Arc<T>) -> bool {
!Arc::ptr_eq(self, other) && **self != **other
}
}
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized + PartialEq> PartialEq for Arc<T> { impl<T: ?Sized + PartialEq> PartialEq for Arc<T> {
/// Equality for two `Arc`s. /// Equality for two `Arc`s.
@ -1306,8 +1336,9 @@ impl<T: ?Sized + PartialEq> PartialEq for Arc<T> {
/// ///
/// assert!(five == Arc::new(5)); /// assert!(five == Arc::new(5));
/// ``` /// ```
default fn eq(&self, other: &Arc<T>) -> bool { #[inline]
**self == **other fn eq(&self, other: &Arc<T>) -> bool {
ArcEqIdent::eq(self, other)
} }
/// Inequality for two `Arc`s. /// Inequality for two `Arc`s.
@ -1326,23 +1357,12 @@ impl<T: ?Sized + PartialEq> PartialEq for Arc<T> {
/// ///
/// assert!(five != Arc::new(6)); /// assert!(five != Arc::new(6));
/// ``` /// ```
default fn ne(&self, other: &Arc<T>) -> bool { #[inline]
**self != **other fn ne(&self, other: &Arc<T>) -> bool {
ArcEqIdent::ne(self, other)
} }
} }
#[doc(hidden)]
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized + Eq> PartialEq for Arc<T> {
#[inline(always)]
fn eq(&self, other: &Arc<T>) -> bool {
Arc::ptr_eq(self, other) || **self == **other
}
#[inline(always)]
fn ne(&self, other: &Arc<T>) -> bool {
!Arc::ptr_eq(self, other) && **self != **other
}
}
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized + PartialOrd> PartialOrd for Arc<T> { impl<T: ?Sized + PartialOrd> PartialOrd for Arc<T> {
/// Partial comparison for two `Arc`s. /// Partial comparison for two `Arc`s.