Skip to content

Commit fc875b0

Browse files
committed
Add issue number to guard map methods.
1 parent bf60078 commit fc875b0

File tree

2 files changed

+78
-49
lines changed

2 files changed

+78
-49
lines changed

src/libstd/sync/mutex.rs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,10 @@ impl<T: ?Sized> Mutex<T> {
211211
/// this call will return an error once the mutex is acquired.
212212
#[stable(feature = "rust1", since = "1.0.0")]
213213
pub fn lock(&self) -> LockResult<MutexGuard<T>> {
214-
unsafe { self.inner.lock.lock() }
215-
unsafe { MutexGuard::new(&*self.inner, &self.data) }
214+
unsafe {
215+
self.inner.lock.lock();
216+
MutexGuard::new(&*self.inner, &self.data)
217+
}
216218
}
217219

218220
/// Attempts to acquire this lock.
@@ -230,10 +232,12 @@ impl<T: ?Sized> Mutex<T> {
230232
/// acquired.
231233
#[stable(feature = "rust1", since = "1.0.0")]
232234
pub fn try_lock(&self) -> TryLockResult<MutexGuard<T>> {
233-
if unsafe { self.inner.lock.try_lock() } {
234-
Ok(try!(unsafe { MutexGuard::new(&*self.inner, &self.data) }))
235-
} else {
236-
Err(TryLockError::WouldBlock)
235+
unsafe {
236+
if self.inner.lock.try_lock() {
237+
Ok(try!(MutexGuard::new(&*self.inner, &self.data)))
238+
} else {
239+
Err(TryLockError::WouldBlock)
240+
}
237241
}
238242
}
239243

@@ -338,17 +342,21 @@ impl StaticMutex {
338342
/// Acquires this lock, see `Mutex::lock`
339343
#[inline]
340344
pub fn lock(&'static self) -> LockResult<MutexGuard<()>> {
341-
unsafe { self.lock.lock() }
342-
unsafe { MutexGuard::new(self, &DUMMY.0) }
345+
unsafe {
346+
self.lock.lock();
347+
MutexGuard::new(self, &DUMMY.0)
348+
}
343349
}
344350

345351
/// Attempts to grab this lock, see `Mutex::try_lock`
346352
#[inline]
347353
pub fn try_lock(&'static self) -> TryLockResult<MutexGuard<()>> {
348-
if unsafe { self.lock.try_lock() } {
349-
Ok(try!(unsafe { MutexGuard::new(self, &DUMMY.0) }))
350-
} else {
351-
Err(TryLockError::WouldBlock)
354+
unsafe {
355+
if self.lock.try_lock() {
356+
Ok(try!(MutexGuard::new(self, &DUMMY.0)))
357+
} else {
358+
Err(TryLockError::WouldBlock)
359+
}
352360
}
353361
}
354362

@@ -393,26 +401,28 @@ impl<'mutex, T: ?Sized> MutexGuard<'mutex, T> {
393401
/// let x = Mutex::new(vec![1, 2]);
394402
///
395403
/// {
396-
/// let y = MutexGuard::map(x.lock().unwrap(), |v| &mut v[0]);
404+
/// let mut y = MutexGuard::map(x.lock().unwrap(), |v| &mut v[0]);
397405
/// *y = 3;
398406
/// }
399407
///
400-
/// assert_eq!(&*x.lock(), &[3, 2]);
408+
/// assert_eq!(&*x.lock().unwrap(), &[3, 2]);
401409
/// ```
402410
#[unstable(feature = "guard_map",
403411
reason = "recently added, needs RFC for stabilization",
404-
issue = "0")]
412+
issue = "27746")]
405413
pub fn map<U: ?Sized, F>(this: Self, cb: F) -> MutexGuard<'mutex, U>
406-
where F: FnOnce(&'mutex mut T) -> &'mutex mut U {
414+
where F: FnOnce(&'mutex mut T) -> &'mutex mut U
415+
{
407416
// Compute the new data while still owning the original lock
408417
// in order to correctly poison if the callback panics.
409418
let data = unsafe { ptr::read(&this.__data) };
410419
let new_data = cb(data);
411420

412421
// We don't want to unlock the lock by running the destructor of the
413422
// original lock, so just read the fields we need and forget it.
414-
let poison = unsafe { ptr::read(&this.__poison) };
415-
let lock = unsafe { ptr::read(&this.__lock) };
423+
let (poison, lock) = unsafe {
424+
(ptr::read(&this.__poison), ptr::read(&this.__lock))
425+
};
416426
mem::forget(this);
417427

418428
MutexGuard {

src/libstd/sync/rwlock.rs

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,10 @@ impl<T: ?Sized> RwLock<T> {
177177
#[inline]
178178
#[stable(feature = "rust1", since = "1.0.0")]
179179
pub fn read(&self) -> LockResult<RwLockReadGuard<T>> {
180-
unsafe { self.inner.lock.read() }
181-
unsafe { RwLockReadGuard::new(&*self.inner, &self.data) }
180+
unsafe {
181+
self.inner.lock.read();
182+
RwLockReadGuard::new(&*self.inner, &self.data)
183+
}
182184
}
183185

184186
/// Attempts to acquire this rwlock with shared read access.
@@ -201,10 +203,12 @@ impl<T: ?Sized> RwLock<T> {
201203
#[inline]
202204
#[stable(feature = "rust1", since = "1.0.0")]
203205
pub fn try_read(&self) -> TryLockResult<RwLockReadGuard<T>> {
204-
if unsafe { self.inner.lock.try_read() } {
205-
Ok(try!(unsafe { RwLockReadGuard::new(&*self.inner, &self.data) }))
206-
} else {
207-
Err(TryLockError::WouldBlock)
206+
unsafe {
207+
if self.inner.lock.try_read() {
208+
Ok(try!(RwLockReadGuard::new(&*self.inner, &self.data)))
209+
} else {
210+
Err(TryLockError::WouldBlock)
211+
}
208212
}
209213
}
210214

@@ -225,8 +229,10 @@ impl<T: ?Sized> RwLock<T> {
225229
#[inline]
226230
#[stable(feature = "rust1", since = "1.0.0")]
227231
pub fn write(&self) -> LockResult<RwLockWriteGuard<T>> {
228-
unsafe { self.inner.lock.write() }
229-
unsafe { RwLockWriteGuard::new(&*self.inner, &self.data) }
232+
unsafe {
233+
self.inner.lock.write();
234+
RwLockWriteGuard::new(&*self.inner, &self.data)
235+
}
230236
}
231237

232238
/// Attempts to lock this rwlock with exclusive write access.
@@ -249,10 +255,12 @@ impl<T: ?Sized> RwLock<T> {
249255
#[inline]
250256
#[stable(feature = "rust1", since = "1.0.0")]
251257
pub fn try_write(&self) -> TryLockResult<RwLockWriteGuard<T>> {
252-
if unsafe { self.inner.lock.try_write() } {
253-
Ok(try!(unsafe { RwLockWriteGuard::new(&*self.inner, &self.data) }))
254-
} else {
255-
Err(TryLockError::WouldBlock)
258+
unsafe {
259+
if self.inner.lock.try_write() {
260+
Ok(try!(RwLockWriteGuard::new(&*self.inner, &self.data)))
261+
} else {
262+
Err(TryLockError::WouldBlock)
263+
}
256264
}
257265
}
258266

@@ -360,8 +368,10 @@ impl StaticRwLock {
360368
/// See `RwLock::read`.
361369
#[inline]
362370
pub fn read(&'static self) -> LockResult<RwLockReadGuard<'static, ()>> {
363-
unsafe { self.lock.read() }
364-
unsafe { RwLockReadGuard::new(self, &DUMMY.0) }
371+
unsafe {
372+
self.lock.read();
373+
RwLockReadGuard::new(self, &DUMMY.0)
374+
}
365375
}
366376

367377
/// Attempts to acquire this lock with shared read access.
@@ -370,10 +380,12 @@ impl StaticRwLock {
370380
#[inline]
371381
pub fn try_read(&'static self)
372382
-> TryLockResult<RwLockReadGuard<'static, ()>> {
373-
if unsafe { self.lock.try_read() } {
374-
unsafe { Ok(try!(RwLockReadGuard::new(self, &DUMMY.0))) }
375-
} else {
376-
Err(TryLockError::WouldBlock)
383+
unsafe {
384+
if self.lock.try_read(){
385+
Ok(try!(RwLockReadGuard::new(self, &DUMMY.0)))
386+
} else {
387+
Err(TryLockError::WouldBlock)
388+
}
377389
}
378390
}
379391

@@ -383,8 +395,10 @@ impl StaticRwLock {
383395
/// See `RwLock::write`.
384396
#[inline]
385397
pub fn write(&'static self) -> LockResult<RwLockWriteGuard<'static, ()>> {
386-
unsafe { self.lock.write() }
387-
unsafe { RwLockWriteGuard::new(self, &DUMMY.0) }
398+
unsafe {
399+
self.lock.write();
400+
RwLockWriteGuard::new(self, &DUMMY.0)
401+
}
388402
}
389403

390404
/// Attempts to lock this rwlock with exclusive write access.
@@ -393,10 +407,12 @@ impl StaticRwLock {
393407
#[inline]
394408
pub fn try_write(&'static self)
395409
-> TryLockResult<RwLockWriteGuard<'static, ()>> {
396-
if unsafe { self.lock.try_write() } {
397-
Ok(unsafe { try!(RwLockWriteGuard::new(self, &DUMMY.0)) })
398-
} else {
399-
Err(TryLockError::WouldBlock)
410+
unsafe {
411+
if self.lock.try_write() {
412+
Ok(try!(RwLockWriteGuard::new(self, &DUMMY.0)))
413+
} else {
414+
Err(TryLockError::WouldBlock)
415+
}
400416
}
401417
}
402418

@@ -439,9 +455,10 @@ impl<'rwlock, T: ?Sized> RwLockReadGuard<'rwlock, T> {
439455
/// ```
440456
#[unstable(feature = "guard_map",
441457
reason = "recently added, needs RFC for stabilization",
442-
issue = "0")]
458+
issue = "27746")]
443459
pub fn map<U: ?Sized, F>(this: Self, cb: F) -> RwLockReadGuard<'rwlock, U>
444-
where F: FnOnce(&'rwlock T) -> &'rwlock U {
460+
where F: FnOnce(&'rwlock T) -> &'rwlock U
461+
{
445462
let new = RwLockReadGuard {
446463
__lock: this.__lock,
447464
__data: cb(this.__data)
@@ -478,7 +495,7 @@ impl<'rwlock, T: ?Sized> RwLockWriteGuard<'rwlock, T> {
478495
/// let x = RwLock::new(vec![1, 2]);
479496
///
480497
/// {
481-
/// let y = RwLockWriteGuard::map(x.write().unwrap(), |v| &mut v[0]);
498+
/// let mut y = RwLockWriteGuard::map(x.write().unwrap(), |v| &mut v[0]);
482499
/// assert_eq!(*y, 1);
483500
///
484501
/// *y = 10;
@@ -488,18 +505,20 @@ impl<'rwlock, T: ?Sized> RwLockWriteGuard<'rwlock, T> {
488505
/// ```
489506
#[unstable(feature = "guard_map",
490507
reason = "recently added, needs RFC for stabilization",
491-
issue = "0")]
508+
issue = "27746")]
492509
pub fn map<U: ?Sized, F>(this: Self, cb: F) -> RwLockWriteGuard<'rwlock, U>
493-
where F: FnOnce(&'rwlock mut T) -> &'rwlock mut U {
510+
where F: FnOnce(&'rwlock mut T) -> &'rwlock mut U
511+
{
494512
// Compute the new data while still owning the original lock
495513
// in order to correctly poison if the callback panics.
496514
let data = unsafe { ptr::read(&this.__data) };
497515
let new_data = cb(data);
498516

499517
// We don't want to unlock the lock by running the destructor of the
500518
// original lock, so just read the fields we need and forget it.
501-
let poison = unsafe { ptr::read(&this.__poison) };
502-
let lock = unsafe { ptr::read(&this.__lock) };
519+
let (poison, lock) = unsafe {
520+
(ptr::read(&this.__poison), ptr::read(&this.__lock))
521+
};
503522
mem::forget(this);
504523

505524
RwLockWriteGuard {

0 commit comments

Comments
 (0)