Skip to content

Implement more traits for guard types #44780

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
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
115 changes: 113 additions & 2 deletions src/libcore/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -872,7 +873,7 @@ impl<T: ?Sized + Eq> Eq for RefCell<T> {}
impl<T: ?Sized + PartialOrd> PartialOrd for RefCell<T> {
#[inline]
fn partial_cmp(&self, other: &RefCell<T>) -> Option<Ordering> {
self.borrow().partial_cmp(&*other.borrow())
self.borrow().partial_cmp(&other.borrow())
}

#[inline]
Expand Down Expand Up @@ -900,7 +901,7 @@ impl<T: ?Sized + PartialOrd> PartialOrd for RefCell<T> {
impl<T: ?Sized + Ord> Ord for RefCell<T> {
#[inline]
fn cmp(&self, other: &RefCell<T>) -> Ordering {
self.borrow().cmp(&*other.borrow())
self.borrow().cmp(&other.borrow())
}
}

Expand Down Expand Up @@ -1032,6 +1033,61 @@ impl<'a, T: ?Sized + fmt::Display> fmt::Display for Ref<'a, T> {
}
}

#[stable(feature = "std_guard_impls_ext", since = "1.22.0")]
impl<'a, T: ?Sized + Hash> Hash for Ref<'a, T> {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self.value.hash(state);
}
}

#[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<T>) -> bool {
self.value == other.value
}
}

#[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.22.0")]
impl<'a, T: ?Sized + Ord> Ord for Ref<'a, T> {
#[inline]
fn cmp(&self, other: &Ref<T>) -> Ordering {
self.value.cmp(other.value)
}
}

#[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<T>) -> Option<Ordering> {
self.value.partial_cmp(&other.value)
}

#[inline]
fn lt(&self, other: &Ref<T>) -> bool {
self.value < other.value
}

#[inline]
fn le(&self, other: &Ref<T>) -> bool {
self.value <= other.value
}

#[inline]
fn gt(&self, other: &Ref<T>) -> bool {
self.value > other.value
}

#[inline]
fn ge(&self, other: &Ref<T>) -> 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.
Expand Down Expand Up @@ -1131,6 +1187,61 @@ impl<'a, T: ?Sized + fmt::Display> fmt::Display for RefMut<'a, T> {
}
}

#[stable(feature = "std_guard_impls_ext", since = "1.22.0")]
impl<'a, T: ?Sized + Hash> Hash for RefMut<'a, T> {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self.value.hash(state);
}
}

#[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<T>) -> bool {
self.value == other.value
}
}

#[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.22.0")]
impl<'a, T: ?Sized + Ord> Ord for RefMut<'a, T> {
#[inline]
fn cmp(&self, other: &RefMut<T>) -> Ordering {
self.value.cmp(&other.value)
}
}

#[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<T>) -> Option<Ordering> {
self.value.partial_cmp(&other.value)
}

#[inline]
fn lt(&self, other: &RefMut<T>) -> bool {
*self.value < *other.value
}

#[inline]
fn le(&self, other: &RefMut<T>) -> bool {
*self.value <= *other.value
}

#[inline]
fn gt(&self, other: &RefMut<T>) -> bool {
*self.value > *other.value
}

#[inline]
fn ge(&self, other: &RefMut<T>) -> bool {
*self.value >= *other.value
}
}

/// The core primitive for interior mutability in Rust.
///
/// `UnsafeCell<T>` is a type that wraps some `T` and indicates unsafe interior operations on the
Expand Down
57 changes: 57 additions & 0 deletions src/libstd/sync/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -447,6 +449,61 @@ impl<'a, T: ?Sized + fmt::Display> fmt::Display for MutexGuard<'a, T> {
}
}

#[stable(feature = "std_guard_impls_ext", since = "1.22.0")]
impl<'a, T: ?Sized + Hash> Hash for MutexGuard<'a, T> {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
(**self).hash(state);
}
}

#[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<T>) -> bool {
**self == **other
}
}

#[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.22.0")]
impl<'a, T: ?Sized + Ord> Ord for MutexGuard<'a, T> {
#[inline]
fn cmp(&self, other: &MutexGuard<T>) -> Ordering {
self.deref().cmp(&other.deref())
}
}

#[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<T>) -> Option<Ordering> {
self.deref().partial_cmp(&other.deref())
}

#[inline]
fn lt(&self, other: &MutexGuard<T>) -> bool {
**self < **other
}

#[inline]
fn le(&self, other: &MutexGuard<T>) -> bool {
**self <= **other
}

#[inline]
fn gt(&self, other: &MutexGuard<T>) -> bool {
**self > **other
}

#[inline]
fn ge(&self, other: &MutexGuard<T>) -> bool {
**self >= **other
}
}

pub fn guard_lock<'a, T: ?Sized>(guard: &MutexGuard<'a, T>) -> &'a sys::Mutex {
&guard.__lock.inner
}
Expand Down
112 changes: 112 additions & 0 deletions src/libstd/sync/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -377,6 +379,61 @@ impl<'a, T: ?Sized + fmt::Display> fmt::Display for RwLockReadGuard<'a, T> {
}
}

#[stable(feature = "std_guard_impls_ext", since = "1.22.0")]
impl<'a, T: ?Sized + Hash> Hash for RwLockReadGuard<'a, T> {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
(**self).hash(state);
}
}

#[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<T>) -> bool {
**self == **other
}
}

#[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.22.0")]
impl<'a, T: ?Sized + Ord> Ord for RwLockReadGuard<'a, T> {
#[inline]
fn cmp(&self, other: &RwLockReadGuard<T>) -> Ordering {
self.deref().cmp(&other.deref())
}
}

#[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<T>) -> Option<Ordering> {
self.deref().partial_cmp(&other.deref())
}

#[inline]
fn lt(&self, other: &RwLockReadGuard<T>) -> bool {
**self < **other
}

#[inline]
fn le(&self, other: &RwLockReadGuard<T>) -> bool {
**self <= **other
}

#[inline]
fn gt(&self, other: &RwLockReadGuard<T>) -> bool {
**self > **other
}

#[inline]
fn ge(&self, other: &RwLockReadGuard<T>) -> 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 {
Expand All @@ -393,6 +450,61 @@ impl<'a, T: ?Sized + fmt::Display> fmt::Display for RwLockWriteGuard<'a, T> {
}
}

#[stable(feature = "std_guard_impls_ext", since = "1.22.0")]
impl<'a, T: ?Sized + Hash> Hash for RwLockWriteGuard<'a, T> {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
(**self).hash(state);
}
}

#[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<T>) -> bool {
**self == **other
}
}

#[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.22.0")]
impl<'a, T: ?Sized + Ord> Ord for RwLockWriteGuard<'a, T> {
#[inline]
fn cmp(&self, other: &RwLockWriteGuard<T>) -> Ordering {
self.deref().cmp(&other.deref())
}
}

#[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<T>) -> Option<Ordering> {
self.deref().partial_cmp(&other.deref())
}

#[inline]
fn lt(&self, other: &RwLockWriteGuard<T>) -> bool {
**self < **other
}

#[inline]
fn le(&self, other: &RwLockWriteGuard<T>) -> bool {
**self <= **other
}

#[inline]
fn gt(&self, other: &RwLockWriteGuard<T>) -> bool {
**self > **other
}

#[inline]
fn ge(&self, other: &RwLockWriteGuard<T>) -> bool {
**self >= **other
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'rwlock, T: ?Sized> Deref for RwLockReadGuard<'rwlock, T> {
type Target = T;
Expand Down