Skip to content

Commit

Permalink
Short-circuit Rc/Arc equality checking on equal pointers where T: Eq
Browse files Browse the repository at this point in the history
Closes #42655
  • Loading branch information
joliss authored and chpio committed Dec 8, 2018
1 parent 9772d02 commit 2a916a6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
25 changes: 23 additions & 2 deletions src/liballoc/rc.rs
Expand Up @@ -8,6 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(specialization)]
#![allow(deprecated)]

//! Single-threaded reference-counting pointers. 'Rc' stands for 'Reference
Expand Down Expand Up @@ -906,6 +907,9 @@ impl<T: ?Sized + PartialEq> PartialEq for Rc<T> {
///
/// Two `Rc`s are equal if their inner values are equal.
///
/// If `T` also implements `Eq`, two `Rc`s that point to the same value are
/// always equal.
///
/// # Examples
///
/// ```
Expand All @@ -916,14 +920,17 @@ impl<T: ?Sized + PartialEq> PartialEq for Rc<T> {
/// assert!(five == Rc::new(5));
/// ```
#[inline(always)]
fn eq(&self, other: &Rc<T>) -> bool {
default fn eq(&self, other: &Rc<T>) -> bool {
**self == **other
}

/// Inequality for two `Rc`s.
///
/// Two `Rc`s are unequal if their inner values are unequal.
///
/// If `T` also implements `Eq`, two `Rc`s that point to the same value are
/// never unequal.
///
/// # Examples
///
/// ```
Expand All @@ -934,11 +941,25 @@ impl<T: ?Sized + PartialEq> PartialEq for Rc<T> {
/// assert!(five != Rc::new(6));
/// ```
#[inline(always)]
fn ne(&self, other: &Rc<T>) -> bool {
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 {
!Rc::ptr_eq(self, other) && **self != **other
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized + Eq> Eq for Rc<T> {}

Expand Down
26 changes: 23 additions & 3 deletions src/liballoc/sync.rs
Expand Up @@ -8,6 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(specialization)]
#![stable(feature = "rust1", since = "1.0.0")]

//! Thread-safe reference-counting pointers.
Expand Down Expand Up @@ -1293,6 +1294,9 @@ impl<T: ?Sized + PartialEq> PartialEq for Arc<T> {
///
/// Two `Arc`s are equal if their inner values are equal.
///
/// If `T` also implements `Eq`, two `Arc`s that point to the same value are
/// always equal.
///
/// # Examples
///
/// ```
Expand All @@ -1302,14 +1306,17 @@ impl<T: ?Sized + PartialEq> PartialEq for Arc<T> {
///
/// assert!(five == Arc::new(5));
/// ```
fn eq(&self, other: &Arc<T>) -> bool {
*(*self) == *(*other)
default fn eq(&self, other: &Arc<T>) -> bool {
**self == **other
}

/// Inequality for two `Arc`s.
///
/// Two `Arc`s are unequal if their inner values are unequal.
///
/// If `T` also implements `Eq`, two `Arc`s that point to the same value are
/// never unequal.
///
/// # Examples
///
/// ```
Expand All @@ -1319,8 +1326,21 @@ impl<T: ?Sized + PartialEq> PartialEq for Arc<T> {
///
/// assert!(five != Arc::new(6));
/// ```
default fn ne(&self, other: &Arc<T>) -> bool {
**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 {
*(*self) != *(*other)
!Arc::ptr_eq(self, other) && **self != **other
}
}
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down

0 comments on commit 2a916a6

Please sign in to comment.