]> git.lizzy.rs Git - rust.git/commitdiff
Add issue number to guard map methods.
authorJonathan Reem <jonathan.reem@gmail.com>
Sun, 31 Jan 2016 00:39:03 +0000 (16:39 -0800)
committerJonathan Reem <jonathan.reem@gmail.com>
Wed, 3 Feb 2016 00:34:49 +0000 (16:34 -0800)
src/libstd/sync/mutex.rs
src/libstd/sync/rwlock.rs

index ab566f3f94583a222d5d6b005b4021cd72136f2b..a6ab54730762df6bf943df9cb53b8fd19a3db719 100644 (file)
@@ -211,8 +211,10 @@ impl<T: ?Sized> Mutex<T> {
     /// this call will return an error once the mutex is acquired.
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn lock(&self) -> LockResult<MutexGuard<T>> {
-        unsafe { self.inner.lock.lock() }
-        unsafe { MutexGuard::new(&*self.inner, &self.data) }
+        unsafe {
+            self.inner.lock.lock();
+            MutexGuard::new(&*self.inner, &self.data)
+        }
     }
 
     /// Attempts to acquire this lock.
@@ -230,10 +232,12 @@ pub fn lock(&self) -> LockResult<MutexGuard<T>> {
     /// acquired.
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn try_lock(&self) -> TryLockResult<MutexGuard<T>> {
-        if unsafe { self.inner.lock.try_lock() } {
-            Ok(try!(unsafe { MutexGuard::new(&*self.inner, &self.data) }))
-        } else {
-            Err(TryLockError::WouldBlock)
+        unsafe {
+            if self.inner.lock.try_lock() {
+                Ok(try!(MutexGuard::new(&*self.inner, &self.data)))
+            } else {
+                Err(TryLockError::WouldBlock)
+            }
         }
     }
 
@@ -338,17 +342,21 @@ pub const fn new() -> StaticMutex {
     /// Acquires this lock, see `Mutex::lock`
     #[inline]
     pub fn lock(&'static self) -> LockResult<MutexGuard<()>> {
-        unsafe { self.lock.lock() }
-        unsafe { MutexGuard::new(self, &DUMMY.0) }
+        unsafe {
+            self.lock.lock();
+            MutexGuard::new(self, &DUMMY.0)
+        }
     }
 
     /// Attempts to grab this lock, see `Mutex::try_lock`
     #[inline]
     pub fn try_lock(&'static self) -> TryLockResult<MutexGuard<()>> {
-        if unsafe { self.lock.try_lock() } {
-            Ok(try!(unsafe { MutexGuard::new(self, &DUMMY.0) }))
-        } else {
-            Err(TryLockError::WouldBlock)
+        unsafe {
+            if self.lock.try_lock() {
+                Ok(try!(MutexGuard::new(self, &DUMMY.0)))
+            } else {
+                Err(TryLockError::WouldBlock)
+            }
         }
     }
 
@@ -393,17 +401,18 @@ unsafe fn new(lock: &'mutex StaticMutex, data: &'mutex UnsafeCell<T>)
     /// let x = Mutex::new(vec![1, 2]);
     ///
     /// {
-    ///     let y = MutexGuard::map(x.lock().unwrap(), |v| &mut v[0]);
+    ///     let mut y = MutexGuard::map(x.lock().unwrap(), |v| &mut v[0]);
     ///     *y = 3;
     /// }
     ///
-    /// assert_eq!(&*x.lock(), &[3, 2]);
+    /// assert_eq!(&*x.lock().unwrap(), &[3, 2]);
     /// ```
     #[unstable(feature = "guard_map",
                reason = "recently added, needs RFC for stabilization",
-               issue = "0")]
+               issue = "27746")]
     pub fn map<U: ?Sized, F>(this: Self, cb: F) -> MutexGuard<'mutex, U>
-    where F: FnOnce(&'mutex mut T) -> &'mutex mut U {
+        where F: FnOnce(&'mutex mut T) -> &'mutex mut U
+    {
         // Compute the new data while still owning the original lock
         // in order to correctly poison if the callback panics.
         let data = unsafe { ptr::read(&this.__data) };
@@ -411,8 +420,9 @@ pub fn map<U: ?Sized, F>(this: Self, cb: F) -> MutexGuard<'mutex, U>
 
         // We don't want to unlock the lock by running the destructor of the
         // original lock, so just read the fields we need and forget it.
-        let poison = unsafe { ptr::read(&this.__poison) };
-        let lock = unsafe { ptr::read(&this.__lock) };
+        let (poison, lock) = unsafe {
+            (ptr::read(&this.__poison), ptr::read(&this.__lock))
+        };
         mem::forget(this);
 
         MutexGuard {
index 7c1fcd6dea7c72fcc4c82c9dfc00c48377314167..2b3233b2dabef26868a316e6e79e11d2b5433acb 100644 (file)
@@ -177,8 +177,10 @@ impl<T: ?Sized> RwLock<T> {
     #[inline]
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn read(&self) -> LockResult<RwLockReadGuard<T>> {
-        unsafe { self.inner.lock.read() }
-        unsafe { RwLockReadGuard::new(&*self.inner, &self.data) }
+        unsafe {
+            self.inner.lock.read();
+            RwLockReadGuard::new(&*self.inner, &self.data)
+        }
     }
 
     /// Attempts to acquire this rwlock with shared read access.
@@ -201,10 +203,12 @@ pub fn read(&self) -> LockResult<RwLockReadGuard<T>> {
     #[inline]
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn try_read(&self) -> TryLockResult<RwLockReadGuard<T>> {
-        if unsafe { self.inner.lock.try_read() } {
-            Ok(try!(unsafe { RwLockReadGuard::new(&*self.inner, &self.data) }))
-        } else {
-            Err(TryLockError::WouldBlock)
+        unsafe {
+            if self.inner.lock.try_read() {
+                Ok(try!(RwLockReadGuard::new(&*self.inner, &self.data)))
+            } else {
+                Err(TryLockError::WouldBlock)
+            }
         }
     }
 
@@ -225,8 +229,10 @@ pub fn try_read(&self) -> TryLockResult<RwLockReadGuard<T>> {
     #[inline]
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn write(&self) -> LockResult<RwLockWriteGuard<T>> {
-        unsafe { self.inner.lock.write() }
-        unsafe { RwLockWriteGuard::new(&*self.inner, &self.data) }
+        unsafe {
+            self.inner.lock.write();
+            RwLockWriteGuard::new(&*self.inner, &self.data)
+        }
     }
 
     /// Attempts to lock this rwlock with exclusive write access.
@@ -249,10 +255,12 @@ pub fn write(&self) -> LockResult<RwLockWriteGuard<T>> {
     #[inline]
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn try_write(&self) -> TryLockResult<RwLockWriteGuard<T>> {
-        if unsafe { self.inner.lock.try_write() } {
-            Ok(try!(unsafe { RwLockWriteGuard::new(&*self.inner, &self.data) }))
-        } else {
-            Err(TryLockError::WouldBlock)
+        unsafe {
+            if self.inner.lock.try_write() {
+                Ok(try!(RwLockWriteGuard::new(&*self.inner, &self.data)))
+            } else {
+                Err(TryLockError::WouldBlock)
+            }
         }
     }
 
@@ -360,8 +368,10 @@ pub const fn new() -> StaticRwLock {
     /// See `RwLock::read`.
     #[inline]
     pub fn read(&'static self) -> LockResult<RwLockReadGuard<'static, ()>> {
-        unsafe { self.lock.read() }
-        unsafe { RwLockReadGuard::new(self, &DUMMY.0) }
+        unsafe {
+            self.lock.read();
+            RwLockReadGuard::new(self, &DUMMY.0)
+        }
     }
 
     /// Attempts to acquire this lock with shared read access.
@@ -370,10 +380,12 @@ pub fn read(&'static self) -> LockResult<RwLockReadGuard<'static, ()>> {
     #[inline]
     pub fn try_read(&'static self)
                     -> TryLockResult<RwLockReadGuard<'static, ()>> {
-        if unsafe { self.lock.try_read() } {
-            unsafe { Ok(try!(RwLockReadGuard::new(self, &DUMMY.0))) }
-        } else {
-            Err(TryLockError::WouldBlock)
+        unsafe {
+            if self.lock.try_read(){
+                Ok(try!(RwLockReadGuard::new(self, &DUMMY.0)))
+            } else {
+                Err(TryLockError::WouldBlock)
+            }
         }
     }
 
@@ -383,8 +395,10 @@ pub fn try_read(&'static self)
     /// See `RwLock::write`.
     #[inline]
     pub fn write(&'static self) -> LockResult<RwLockWriteGuard<'static, ()>> {
-        unsafe { self.lock.write() }
-        unsafe { RwLockWriteGuard::new(self, &DUMMY.0) }
+        unsafe {
+            self.lock.write();
+            RwLockWriteGuard::new(self, &DUMMY.0)
+        }
     }
 
     /// Attempts to lock this rwlock with exclusive write access.
@@ -393,10 +407,12 @@ pub fn write(&'static self) -> LockResult<RwLockWriteGuard<'static, ()>> {
     #[inline]
     pub fn try_write(&'static self)
                      -> TryLockResult<RwLockWriteGuard<'static, ()>> {
-        if unsafe { self.lock.try_write() } {
-            Ok(unsafe { try!(RwLockWriteGuard::new(self, &DUMMY.0)) })
-        } else {
-            Err(TryLockError::WouldBlock)
+        unsafe {
+            if self.lock.try_write() {
+                Ok(try!(RwLockWriteGuard::new(self, &DUMMY.0)))
+            } else {
+                Err(TryLockError::WouldBlock)
+            }
         }
     }
 
@@ -439,9 +455,10 @@ unsafe fn new(lock: &'rwlock StaticRwLock, data: &'rwlock UnsafeCell<T>)
     /// ```
     #[unstable(feature = "guard_map",
                reason = "recently added, needs RFC for stabilization",
-               issue = "0")]
+               issue = "27746")]
     pub fn map<U: ?Sized, F>(this: Self, cb: F) -> RwLockReadGuard<'rwlock, U>
-    where F: FnOnce(&'rwlock T) -> &'rwlock U {
+        where F: FnOnce(&'rwlock T) -> &'rwlock U
+    {
         let new = RwLockReadGuard {
             __lock: this.__lock,
             __data: cb(this.__data)
@@ -478,7 +495,7 @@ unsafe fn new(lock: &'rwlock StaticRwLock, data: &'rwlock UnsafeCell<T>)
     /// let x = RwLock::new(vec![1, 2]);
     ///
     /// {
-    ///     let y = RwLockWriteGuard::map(x.write().unwrap(), |v| &mut v[0]);
+    ///     let mut y = RwLockWriteGuard::map(x.write().unwrap(), |v| &mut v[0]);
     ///     assert_eq!(*y, 1);
     ///
     ///     *y = 10;
@@ -488,9 +505,10 @@ unsafe fn new(lock: &'rwlock StaticRwLock, data: &'rwlock UnsafeCell<T>)
     /// ```
     #[unstable(feature = "guard_map",
                reason = "recently added, needs RFC for stabilization",
-               issue = "0")]
+               issue = "27746")]
     pub fn map<U: ?Sized, F>(this: Self, cb: F) -> RwLockWriteGuard<'rwlock, U>
-    where F: FnOnce(&'rwlock mut T) -> &'rwlock mut U {
+        where F: FnOnce(&'rwlock mut T) -> &'rwlock mut U
+    {
         // Compute the new data while still owning the original lock
         // in order to correctly poison if the callback panics.
         let data = unsafe { ptr::read(&this.__data) };
@@ -498,8 +516,9 @@ pub fn map<U: ?Sized, F>(this: Self, cb: F) -> RwLockWriteGuard<'rwlock, U>
 
         // We don't want to unlock the lock by running the destructor of the
         // original lock, so just read the fields we need and forget it.
-        let poison = unsafe { ptr::read(&this.__poison) };
-        let lock = unsafe { ptr::read(&this.__lock) };
+        let (poison, lock) = unsafe {
+            (ptr::read(&this.__poison), ptr::read(&this.__lock))
+        };
         mem::forget(this);
 
         RwLockWriteGuard {