Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 25d070f

Browse files
committed
Remove unneeded Send/Sync bounds from Mutex/RwLock.
The requirements `T: Send` and `T: Send + Sync` for `Mutex` and `RwLock` respectively only matter if those types are shared/sent across thread boundaries, and that is adequately controlled by the impls of `Send`/`Sync` for them. If `T` doesn't satisfy the bounds, then the types cannot cross thread boundaries and so everything is still safe (the two types just act like an expensive `RefCell`).
1 parent 3527507 commit 25d070f

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/libstd/sync/mutex.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ pub struct Mutex<T> {
121121
data: UnsafeCell<T>,
122122
}
123123

124+
// these are the only places where `T: Send` matters; all other
125+
// functionality works fine on a single thread.
124126
unsafe impl<T: Send> Send for Mutex<T> { }
125127

126128
unsafe impl<T: Send> Sync for Mutex<T> { }
@@ -179,7 +181,7 @@ pub const MUTEX_INIT: StaticMutex = StaticMutex {
179181
poison: poison::FLAG_INIT,
180182
};
181183

182-
impl<T: Send> Mutex<T> {
184+
impl<T> Mutex<T> {
183185
/// Creates a new mutex in an unlocked state ready for use.
184186
#[stable(feature = "rust1", since = "1.0.0")]
185187
pub fn new(t: T) -> Mutex<T> {
@@ -242,7 +244,7 @@ impl<T: Send> Mutex<T> {
242244

243245
#[unsafe_destructor]
244246
#[stable(feature = "rust1", since = "1.0.0")]
245-
impl<T: Send> Drop for Mutex<T> {
247+
impl<T> Drop for Mutex<T> {
246248
fn drop(&mut self) {
247249
// This is actually safe b/c we know that there is no further usage of
248250
// this mutex (it's up to the user to arrange for a mutex to get
@@ -252,7 +254,7 @@ impl<T: Send> Drop for Mutex<T> {
252254
}
253255

254256
#[stable(feature = "rust1", since = "1.0.0")]
255-
impl<T: fmt::Debug + Send + 'static> fmt::Debug for Mutex<T> {
257+
impl<T: fmt::Debug + 'static> fmt::Debug for Mutex<T> {
256258
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
257259
match self.try_lock() {
258260
Ok(guard) => write!(f, "Mutex {{ data: {:?} }}", *guard),

src/libstd/sync/rwlock.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ pub struct RwLockWriteGuard<'a, T: 'a> {
129129

130130
impl<'a, T> !marker::Send for RwLockWriteGuard<'a, T> {}
131131

132-
impl<T: Send + Sync> RwLock<T> {
132+
impl<T> RwLock<T> {
133133
/// Creates a new instance of an `RwLock<T>` which is unlocked.
134134
///
135135
/// # Examples
@@ -257,7 +257,7 @@ impl<T> Drop for RwLock<T> {
257257
}
258258

259259
#[stable(feature = "rust1", since = "1.0.0")]
260-
impl<T: fmt::Debug + Send + Sync> fmt::Debug for RwLock<T> {
260+
impl<T: fmt::Debug> fmt::Debug for RwLock<T> {
261261
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
262262
match self.try_read() {
263263
Ok(guard) => write!(f, "RwLock {{ data: {:?} }}", *guard),

0 commit comments

Comments
 (0)