From 84a6c790176225dad09c06a41045cfd9b56decbe Mon Sep 17 00:00:00 2001 From: Marcus Buffett Date: Fri, 22 Sep 2017 17:59:46 -0700 Subject: [PATCH 1/2] Implement more traits for guard types Implements Hash, PartialEq, Eq, PartialOrd, and Ord for Ref, RefMut, MutexGuard, RwLockReadGuard, RwLockWriteGuard Fixes #24372 --- src/libcore/cell.rs | 115 +++++++++++++++++++++++++++++++++++++- src/libstd/sync/mutex.rs | 57 +++++++++++++++++++ src/libstd/sync/rwlock.rs | 112 +++++++++++++++++++++++++++++++++++++ 3 files changed, 282 insertions(+), 2 deletions(-) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index b9c5ff10f87b9..5b1b57c2a305e 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -181,6 +181,7 @@ use cmp::Ordering; use fmt::{self, Debug, Display}; +use hash::{Hash, Hasher}; use marker::Unsize; use mem; use ops::{Deref, DerefMut, CoerceUnsized}; @@ -872,7 +873,7 @@ impl Eq for RefCell {} impl PartialOrd for RefCell { #[inline] fn partial_cmp(&self, other: &RefCell) -> Option { - self.borrow().partial_cmp(&*other.borrow()) + self.borrow().partial_cmp(&other.borrow()) } #[inline] @@ -900,7 +901,7 @@ impl PartialOrd for RefCell { impl Ord for RefCell { #[inline] fn cmp(&self, other: &RefCell) -> Ordering { - self.borrow().cmp(&*other.borrow()) + self.borrow().cmp(&other.borrow()) } } @@ -1032,6 +1033,61 @@ impl<'a, T: ?Sized + fmt::Display> fmt::Display for Ref<'a, T> { } } +#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +impl<'a, T: ?Sized + Hash> Hash for Ref<'a, T> { + #[inline] + fn hash(&self, state: &mut H) { + self.value.hash(state); + } +} + +#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +impl<'a, T: ?Sized + PartialEq> PartialEq for Ref<'a, T> { + #[inline] + fn eq(&self, other: &Ref) -> bool { + self.value == other.value + } +} + +#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +impl<'a, T: ?Sized + Eq> Eq for Ref<'a, T> { } + +#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +impl<'a, T: ?Sized + Ord> Ord for Ref<'a, T> { + #[inline] + fn cmp(&self, other: &Ref) -> Ordering { + self.value.cmp(other.value) + } +} + +#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +impl<'a, T: ?Sized + PartialOrd> PartialOrd for Ref<'a, T> { + #[inline] + fn partial_cmp(&self, other: &Ref) -> Option { + self.value.partial_cmp(&other.value) + } + + #[inline] + fn lt(&self, other: &Ref) -> bool { + self.value < other.value + } + + #[inline] + fn le(&self, other: &Ref) -> bool { + self.value <= other.value + } + + #[inline] + fn gt(&self, other: &Ref) -> bool { + self.value > other.value + } + + #[inline] + fn ge(&self, other: &Ref) -> bool { + self.value >= other.value + } +} + impl<'b, T: ?Sized> RefMut<'b, T> { /// Make a new `RefMut` for a component of the borrowed data, e.g. an enum /// variant. @@ -1131,6 +1187,61 @@ impl<'a, T: ?Sized + fmt::Display> fmt::Display for RefMut<'a, T> { } } +#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +impl<'a, T: ?Sized + Hash> Hash for RefMut<'a, T> { + #[inline] + fn hash(&self, state: &mut H) { + self.value.hash(state); + } +} + +#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +impl<'a, T: ?Sized + PartialEq> PartialEq for RefMut<'a, T> { + #[inline] + fn eq(&self, other: &RefMut) -> bool { + self.value == other.value + } +} + +#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +impl<'a, T: ?Sized + Eq> Eq for RefMut<'a, T> { } + +#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +impl<'a, T: ?Sized + Ord> Ord for RefMut<'a, T> { + #[inline] + fn cmp(&self, other: &RefMut) -> Ordering { + self.value.cmp(&other.value) + } +} + +#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +impl<'a, T: ?Sized + PartialOrd> PartialOrd for RefMut<'a, T> { + #[inline] + fn partial_cmp(&self, other: &RefMut) -> Option { + self.value.partial_cmp(&other.value) + } + + #[inline] + fn lt(&self, other: &RefMut) -> bool { + *self.value < *other.value + } + + #[inline] + fn le(&self, other: &RefMut) -> bool { + *self.value <= *other.value + } + + #[inline] + fn gt(&self, other: &RefMut) -> bool { + *self.value > *other.value + } + + #[inline] + fn ge(&self, other: &RefMut) -> bool { + *self.value >= *other.value + } +} + /// The core primitive for interior mutability in Rust. /// /// `UnsafeCell` is a type that wraps some `T` and indicates unsafe interior operations on the diff --git a/src/libstd/sync/mutex.rs b/src/libstd/sync/mutex.rs index 62d8de18f4b45..ca0e0a526d928 100644 --- a/src/libstd/sync/mutex.rs +++ b/src/libstd/sync/mutex.rs @@ -9,7 +9,9 @@ // except according to those terms. use cell::UnsafeCell; +use cmp::{Ordering}; use fmt; +use hash::{Hash, Hasher}; use mem; use ops::{Deref, DerefMut}; use ptr; @@ -447,6 +449,61 @@ impl<'a, T: ?Sized + fmt::Display> fmt::Display for MutexGuard<'a, T> { } } +#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +impl<'a, T: ?Sized + Hash> Hash for MutexGuard<'a, T> { + #[inline] + fn hash(&self, state: &mut H) { + (**self).hash(state); + } +} + +#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +impl<'a, T: ?Sized + PartialEq> PartialEq for MutexGuard<'a, T> { + #[inline] + fn eq(&self, other: &MutexGuard) -> bool { + **self == **other + } +} + +#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +impl<'a, T: ?Sized + Eq> Eq for MutexGuard<'a, T> { } + +#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +impl<'a, T: ?Sized + Ord> Ord for MutexGuard<'a, T> { + #[inline] + fn cmp(&self, other: &MutexGuard) -> Ordering { + self.deref().cmp(&other.deref()) + } +} + +#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +impl<'a, T: ?Sized + PartialOrd> PartialOrd for MutexGuard<'a, T> { + #[inline] + fn partial_cmp(&self, other: &MutexGuard) -> Option { + self.deref().partial_cmp(&other.deref()) + } + + #[inline] + fn lt(&self, other: &MutexGuard) -> bool { + **self < **other + } + + #[inline] + fn le(&self, other: &MutexGuard) -> bool { + **self <= **other + } + + #[inline] + fn gt(&self, other: &MutexGuard) -> bool { + **self > **other + } + + #[inline] + fn ge(&self, other: &MutexGuard) -> bool { + **self >= **other + } +} + pub fn guard_lock<'a, T: ?Sized>(guard: &MutexGuard<'a, T>) -> &'a sys::Mutex { &guard.__lock.inner } diff --git a/src/libstd/sync/rwlock.rs b/src/libstd/sync/rwlock.rs index 5c5231f4e84a3..1bfa21d473b95 100644 --- a/src/libstd/sync/rwlock.rs +++ b/src/libstd/sync/rwlock.rs @@ -9,7 +9,9 @@ // except according to those terms. use cell::UnsafeCell; +use cmp::Ordering; use fmt; +use hash::{Hash, Hasher}; use marker; use mem; use ops::{Deref, DerefMut}; @@ -377,6 +379,61 @@ impl<'a, T: ?Sized + fmt::Display> fmt::Display for RwLockReadGuard<'a, T> { } } +#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +impl<'a, T: ?Sized + Hash> Hash for RwLockReadGuard<'a, T> { + #[inline] + fn hash(&self, state: &mut H) { + (**self).hash(state); + } +} + +#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +impl<'a, T: ?Sized + PartialEq> PartialEq for RwLockReadGuard<'a, T> { + #[inline] + fn eq(&self, other: &RwLockReadGuard) -> bool { + **self == **other + } +} + +#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +impl<'a, T: ?Sized + Eq> Eq for RwLockReadGuard<'a, T> { } + +#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +impl<'a, T: ?Sized + Ord> Ord for RwLockReadGuard<'a, T> { + #[inline] + fn cmp(&self, other: &RwLockReadGuard) -> Ordering { + self.deref().cmp(&other.deref()) + } +} + +#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +impl<'a, T: ?Sized + PartialOrd> PartialOrd for RwLockReadGuard<'a, T> { + #[inline] + fn partial_cmp(&self, other: &RwLockReadGuard) -> Option { + self.deref().partial_cmp(&other.deref()) + } + + #[inline] + fn lt(&self, other: &RwLockReadGuard) -> bool { + **self < **other + } + + #[inline] + fn le(&self, other: &RwLockReadGuard) -> bool { + **self <= **other + } + + #[inline] + fn gt(&self, other: &RwLockReadGuard) -> bool { + **self > **other + } + + #[inline] + fn ge(&self, other: &RwLockReadGuard) -> bool { + **self >= **other + } +} + #[stable(feature = "std_debug", since = "1.16.0")] impl<'a, T: fmt::Debug> fmt::Debug for RwLockWriteGuard<'a, T> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { @@ -393,6 +450,61 @@ impl<'a, T: ?Sized + fmt::Display> fmt::Display for RwLockWriteGuard<'a, T> { } } +#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +impl<'a, T: ?Sized + Hash> Hash for RwLockWriteGuard<'a, T> { + #[inline] + fn hash(&self, state: &mut H) { + (**self).hash(state); + } +} + +#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +impl<'a, T: ?Sized + PartialEq> PartialEq for RwLockWriteGuard<'a, T> { + #[inline] + fn eq(&self, other: &RwLockWriteGuard) -> bool { + **self == **other + } +} + +#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +impl<'a, T: ?Sized + Eq> Eq for RwLockWriteGuard<'a, T> { } + +#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +impl<'a, T: ?Sized + Ord> Ord for RwLockWriteGuard<'a, T> { + #[inline] + fn cmp(&self, other: &RwLockWriteGuard) -> Ordering { + self.deref().cmp(&other.deref()) + } +} + +#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +impl<'a, T: ?Sized + PartialOrd> PartialOrd for RwLockWriteGuard<'a, T> { + #[inline] + fn partial_cmp(&self, other: &RwLockWriteGuard) -> Option { + self.deref().partial_cmp(&other.deref()) + } + + #[inline] + fn lt(&self, other: &RwLockWriteGuard) -> bool { + **self < **other + } + + #[inline] + fn le(&self, other: &RwLockWriteGuard) -> bool { + **self <= **other + } + + #[inline] + fn gt(&self, other: &RwLockWriteGuard) -> bool { + **self > **other + } + + #[inline] + fn ge(&self, other: &RwLockWriteGuard) -> bool { + **self >= **other + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl<'rwlock, T: ?Sized> Deref for RwLockReadGuard<'rwlock, T> { type Target = T; From 48f93754a2d76e56d889fd0e516b2e9f759138f4 Mon Sep 17 00:00:00 2001 From: Marcus Buffett Date: Mon, 25 Sep 2017 15:46:18 -0700 Subject: [PATCH 2/2] Update version from 1.23 to 1.22 --- src/libcore/cell.rs | 20 ++++++++++---------- src/libstd/sync/mutex.rs | 10 +++++----- src/libstd/sync/rwlock.rs | 20 ++++++++++---------- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 5b1b57c2a305e..38198d8f862a9 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -1033,7 +1033,7 @@ impl<'a, T: ?Sized + fmt::Display> fmt::Display for Ref<'a, T> { } } -#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +#[stable(feature = "std_guard_impls_ext", since = "1.22.0")] impl<'a, T: ?Sized + Hash> Hash for Ref<'a, T> { #[inline] fn hash(&self, state: &mut H) { @@ -1041,7 +1041,7 @@ impl<'a, T: ?Sized + Hash> Hash for Ref<'a, T> { } } -#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +#[stable(feature = "std_guard_impls_ext", since = "1.22.0")] impl<'a, T: ?Sized + PartialEq> PartialEq for Ref<'a, T> { #[inline] fn eq(&self, other: &Ref) -> bool { @@ -1049,10 +1049,10 @@ impl<'a, T: ?Sized + PartialEq> PartialEq for Ref<'a, T> { } } -#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +#[stable(feature = "std_guard_impls_ext", since = "1.22.0")] impl<'a, T: ?Sized + Eq> Eq for Ref<'a, T> { } -#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +#[stable(feature = "std_guard_impls_ext", since = "1.22.0")] impl<'a, T: ?Sized + Ord> Ord for Ref<'a, T> { #[inline] fn cmp(&self, other: &Ref) -> Ordering { @@ -1060,7 +1060,7 @@ impl<'a, T: ?Sized + Ord> Ord for Ref<'a, T> { } } -#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +#[stable(feature = "std_guard_impls_ext", since = "1.22.0")] impl<'a, T: ?Sized + PartialOrd> PartialOrd for Ref<'a, T> { #[inline] fn partial_cmp(&self, other: &Ref) -> Option { @@ -1187,7 +1187,7 @@ impl<'a, T: ?Sized + fmt::Display> fmt::Display for RefMut<'a, T> { } } -#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +#[stable(feature = "std_guard_impls_ext", since = "1.22.0")] impl<'a, T: ?Sized + Hash> Hash for RefMut<'a, T> { #[inline] fn hash(&self, state: &mut H) { @@ -1195,7 +1195,7 @@ impl<'a, T: ?Sized + Hash> Hash for RefMut<'a, T> { } } -#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +#[stable(feature = "std_guard_impls_ext", since = "1.22.0")] impl<'a, T: ?Sized + PartialEq> PartialEq for RefMut<'a, T> { #[inline] fn eq(&self, other: &RefMut) -> bool { @@ -1203,10 +1203,10 @@ impl<'a, T: ?Sized + PartialEq> PartialEq for RefMut<'a, T> { } } -#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +#[stable(feature = "std_guard_impls_ext", since = "1.22.0")] impl<'a, T: ?Sized + Eq> Eq for RefMut<'a, T> { } -#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +#[stable(feature = "std_guard_impls_ext", since = "1.22.0")] impl<'a, T: ?Sized + Ord> Ord for RefMut<'a, T> { #[inline] fn cmp(&self, other: &RefMut) -> Ordering { @@ -1214,7 +1214,7 @@ impl<'a, T: ?Sized + Ord> Ord for RefMut<'a, T> { } } -#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +#[stable(feature = "std_guard_impls_ext", since = "1.22.0")] impl<'a, T: ?Sized + PartialOrd> PartialOrd for RefMut<'a, T> { #[inline] fn partial_cmp(&self, other: &RefMut) -> Option { diff --git a/src/libstd/sync/mutex.rs b/src/libstd/sync/mutex.rs index ca0e0a526d928..07eb3117b1d9e 100644 --- a/src/libstd/sync/mutex.rs +++ b/src/libstd/sync/mutex.rs @@ -449,7 +449,7 @@ impl<'a, T: ?Sized + fmt::Display> fmt::Display for MutexGuard<'a, T> { } } -#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +#[stable(feature = "std_guard_impls_ext", since = "1.22.0")] impl<'a, T: ?Sized + Hash> Hash for MutexGuard<'a, T> { #[inline] fn hash(&self, state: &mut H) { @@ -457,7 +457,7 @@ impl<'a, T: ?Sized + Hash> Hash for MutexGuard<'a, T> { } } -#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +#[stable(feature = "std_guard_impls_ext", since = "1.22.0")] impl<'a, T: ?Sized + PartialEq> PartialEq for MutexGuard<'a, T> { #[inline] fn eq(&self, other: &MutexGuard) -> bool { @@ -465,10 +465,10 @@ impl<'a, T: ?Sized + PartialEq> PartialEq for MutexGuard<'a, T> { } } -#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +#[stable(feature = "std_guard_impls_ext", since = "1.22.0")] impl<'a, T: ?Sized + Eq> Eq for MutexGuard<'a, T> { } -#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +#[stable(feature = "std_guard_impls_ext", since = "1.22.0")] impl<'a, T: ?Sized + Ord> Ord for MutexGuard<'a, T> { #[inline] fn cmp(&self, other: &MutexGuard) -> Ordering { @@ -476,7 +476,7 @@ impl<'a, T: ?Sized + Ord> Ord for MutexGuard<'a, T> { } } -#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +#[stable(feature = "std_guard_impls_ext", since = "1.22.0")] impl<'a, T: ?Sized + PartialOrd> PartialOrd for MutexGuard<'a, T> { #[inline] fn partial_cmp(&self, other: &MutexGuard) -> Option { diff --git a/src/libstd/sync/rwlock.rs b/src/libstd/sync/rwlock.rs index 1bfa21d473b95..553613e07534a 100644 --- a/src/libstd/sync/rwlock.rs +++ b/src/libstd/sync/rwlock.rs @@ -379,7 +379,7 @@ impl<'a, T: ?Sized + fmt::Display> fmt::Display for RwLockReadGuard<'a, T> { } } -#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +#[stable(feature = "std_guard_impls_ext", since = "1.22.0")] impl<'a, T: ?Sized + Hash> Hash for RwLockReadGuard<'a, T> { #[inline] fn hash(&self, state: &mut H) { @@ -387,7 +387,7 @@ impl<'a, T: ?Sized + Hash> Hash for RwLockReadGuard<'a, T> { } } -#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +#[stable(feature = "std_guard_impls_ext", since = "1.22.0")] impl<'a, T: ?Sized + PartialEq> PartialEq for RwLockReadGuard<'a, T> { #[inline] fn eq(&self, other: &RwLockReadGuard) -> bool { @@ -395,10 +395,10 @@ impl<'a, T: ?Sized + PartialEq> PartialEq for RwLockReadGuard<'a, T> { } } -#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +#[stable(feature = "std_guard_impls_ext", since = "1.22.0")] impl<'a, T: ?Sized + Eq> Eq for RwLockReadGuard<'a, T> { } -#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +#[stable(feature = "std_guard_impls_ext", since = "1.22.0")] impl<'a, T: ?Sized + Ord> Ord for RwLockReadGuard<'a, T> { #[inline] fn cmp(&self, other: &RwLockReadGuard) -> Ordering { @@ -406,7 +406,7 @@ impl<'a, T: ?Sized + Ord> Ord for RwLockReadGuard<'a, T> { } } -#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +#[stable(feature = "std_guard_impls_ext", since = "1.22.0")] impl<'a, T: ?Sized + PartialOrd> PartialOrd for RwLockReadGuard<'a, T> { #[inline] fn partial_cmp(&self, other: &RwLockReadGuard) -> Option { @@ -450,7 +450,7 @@ impl<'a, T: ?Sized + fmt::Display> fmt::Display for RwLockWriteGuard<'a, T> { } } -#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +#[stable(feature = "std_guard_impls_ext", since = "1.22.0")] impl<'a, T: ?Sized + Hash> Hash for RwLockWriteGuard<'a, T> { #[inline] fn hash(&self, state: &mut H) { @@ -458,7 +458,7 @@ impl<'a, T: ?Sized + Hash> Hash for RwLockWriteGuard<'a, T> { } } -#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +#[stable(feature = "std_guard_impls_ext", since = "1.22.0")] impl<'a, T: ?Sized + PartialEq> PartialEq for RwLockWriteGuard<'a, T> { #[inline] fn eq(&self, other: &RwLockWriteGuard) -> bool { @@ -466,10 +466,10 @@ impl<'a, T: ?Sized + PartialEq> PartialEq for RwLockWriteGuard<'a, T> { } } -#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +#[stable(feature = "std_guard_impls_ext", since = "1.22.0")] impl<'a, T: ?Sized + Eq> Eq for RwLockWriteGuard<'a, T> { } -#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +#[stable(feature = "std_guard_impls_ext", since = "1.22.0")] impl<'a, T: ?Sized + Ord> Ord for RwLockWriteGuard<'a, T> { #[inline] fn cmp(&self, other: &RwLockWriteGuard) -> Ordering { @@ -477,7 +477,7 @@ impl<'a, T: ?Sized + Ord> Ord for RwLockWriteGuard<'a, T> { } } -#[stable(feature = "std_guard_impls_ext", since = "1.23.0")] +#[stable(feature = "std_guard_impls_ext", since = "1.22.0")] impl<'a, T: ?Sized + PartialOrd> PartialOrd for RwLockWriteGuard<'a, T> { #[inline] fn partial_cmp(&self, other: &RwLockWriteGuard) -> Option {