Skip to content

Short-circuit Rc/Arc equality checking on equal pointers where T: Eq #42965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/liballoc/arc.rs
Original file line number Diff line number Diff line change
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 @@ -1018,6 +1019,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 @@ -1027,14 +1031,17 @@ impl<T: ?Sized + PartialEq> PartialEq for Arc<T> {
///
/// assert!(five == Arc::new(5));
/// ```
fn eq(&self, other: &Arc<T>) -> bool {
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 @@ -1044,10 +1051,23 @@ impl<T: ?Sized + PartialEq> PartialEq for Arc<T> {
///
/// assert!(five != Arc::new(6));
/// ```
fn ne(&self, other: &Arc<T>) -> bool {
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 {
!Arc::ptr_eq(self, other) && *(*self) != *(*other)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized + PartialOrd> PartialOrd for Arc<T> {
/// Partial comparison for two `Arc`s.
Expand Down
25 changes: 23 additions & 2 deletions src/liballoc/rc.rs
Original file line number Diff line number Diff line change
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 @@ -773,6 +774,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 @@ -783,14 +787,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 @@ -801,11 +808,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