about summary refs log tree commit diff
path: root/src/libsync
diff options
context:
space:
mode:
authorHuon Wilson <dbau.pp+github@gmail.com>2014-05-24 21:35:53 +1000
committerHuon Wilson <dbau.pp+github@gmail.com>2014-05-25 10:23:37 +1000
commit9698221f919a80f2a0810e17c8ee8e80da8cefeb (patch)
tree29b0bb162d7e94d1ebc91d44d6265f211914934b /src/libsync
parent9e244d708461d5028066a59e70866f52517e7b85 (diff)
downloadrust-9698221f919a80f2a0810e17c8ee8e80da8cefeb.tar.gz
rust-9698221f919a80f2a0810e17c8ee8e80da8cefeb.zip
Paper over privacy issues with Deref by changing field names.
Types that implement Deref can cause weird error messages due to their
private fields conflicting with a field of the type they deref to, e.g.,
previously

    struct Foo { x: int }

    let a: Arc<Foo> = ...;
    println!("{}", a.x);

would complain the the `x` field of `Arc` was private (since Arc has a
private field called `x`) rather than just ignoring it.

This patch doesn't fix that issue, but does mean one would have to write
`a._ptr` to hit the same error message, which seems far less
common. (This patch `_`-prefixes all private fields of
`Deref`-implementing types.)

cc #12808
Diffstat (limited to 'src/libsync')
-rw-r--r--src/libsync/lock.rs39
1 files changed, 22 insertions, 17 deletions
diff --git a/src/libsync/lock.rs b/src/libsync/lock.rs
index 9f59f587770..1b620185610 100644
--- a/src/libsync/lock.rs
+++ b/src/libsync/lock.rs
@@ -174,7 +174,9 @@ pub struct Mutex<T> {
 /// An guard which is created by locking a mutex. Through this guard the
 /// underlying data can be accessed.
 pub struct MutexGuard<'a, T> {
-    data: &'a mut T,
+    // FIXME #12808: strange name to try to avoid interfering with
+    // field accesses of the contained type via Deref
+    _data: &'a mut T,
     /// Inner condition variable connected to the locked mutex that this guard
     /// was created from. This can be used for atomic-unlock-and-deschedule.
     pub cond: Condvar<'a>,
@@ -221,7 +223,7 @@ impl<T: Send> Mutex<T> {
         let data = unsafe { &mut *self.data.get() };
 
         MutexGuard {
-            data: data,
+            _data: data,
             cond: Condvar {
                 name: "Mutex",
                 poison: PoisonOnFail::new(poison, "Mutex"),
@@ -232,10 +234,10 @@ impl<T: Send> Mutex<T> {
 }
 
 impl<'a, T: Send> Deref<T> for MutexGuard<'a, T> {
-    fn deref<'a>(&'a self) -> &'a T { &*self.data }
+    fn deref<'a>(&'a self) -> &'a T { &*self._data }
 }
 impl<'a, T: Send> DerefMut<T> for MutexGuard<'a, T> {
-    fn deref_mut<'a>(&'a mut self) -> &'a mut T { &mut *self.data }
+    fn deref_mut<'a>(&'a mut self) -> &'a mut T { &mut *self._data }
 }
 
 /****************************************************************************
@@ -272,7 +274,9 @@ pub struct RWLock<T> {
 /// A guard which is created by locking an rwlock in write mode. Through this
 /// guard the underlying data can be accessed.
 pub struct RWLockWriteGuard<'a, T> {
-    data: &'a mut T,
+    // FIXME #12808: strange name to try to avoid interfering with
+    // field accesses of the contained type via Deref
+    _data: &'a mut T,
     /// Inner condition variable that can be used to sleep on the write mode of
     /// this rwlock.
     pub cond: Condvar<'a>,
@@ -281,8 +285,10 @@ pub struct RWLockWriteGuard<'a, T> {
 /// A guard which is created by locking an rwlock in read mode. Through this
 /// guard the underlying data can be accessed.
 pub struct RWLockReadGuard<'a, T> {
-    data: &'a T,
-    guard: raw::RWLockReadGuard<'a>,
+    // FIXME #12808: strange names to try to avoid interfering with
+    // field accesses of the contained type via Deref
+    _data: &'a T,
+    _guard: raw::RWLockReadGuard<'a>,
 }
 
 impl<T: Send + Share> RWLock<T> {
@@ -320,7 +326,7 @@ impl<T: Send + Share> RWLock<T> {
         let data = unsafe { &mut *self.data.get() };
 
         RWLockWriteGuard {
-            data: data,
+            _data: data,
             cond: Condvar {
                 name: "RWLock",
                 poison: PoisonOnFail::new(poison, "RWLock"),
@@ -340,8 +346,8 @@ impl<T: Send + Share> RWLock<T> {
         let guard = self.lock.read();
         PoisonOnFail::check(unsafe { *self.failed.get() }, "RWLock");
         RWLockReadGuard {
-            guard: guard,
-            data: unsafe { &*self.data.get() },
+            _guard: guard,
+            _data: unsafe { &*self.data.get() },
         }
     }
 }
@@ -351,25 +357,25 @@ impl<'a, T: Send + Share> RWLockWriteGuard<'a, T> {
     ///
     /// This will allow pending readers to come into the lock.
     pub fn downgrade(self) -> RWLockReadGuard<'a, T> {
-        let RWLockWriteGuard { data, cond } = self;
+        let RWLockWriteGuard { _data, cond } = self;
         // convert the data to read-only explicitly
-        let data = &*data;
+        let data = &*_data;
         let guard = match cond.inner {
             InnerMutex(..) => unreachable!(),
             InnerRWLock(guard) => guard.downgrade()
         };
-        RWLockReadGuard { guard: guard, data: data }
+        RWLockReadGuard { _guard: guard, _data: data }
     }
 }
 
 impl<'a, T: Send + Share> Deref<T> for RWLockReadGuard<'a, T> {
-    fn deref<'a>(&'a self) -> &'a T { self.data }
+    fn deref<'a>(&'a self) -> &'a T { self._data }
 }
 impl<'a, T: Send + Share> Deref<T> for RWLockWriteGuard<'a, T> {
-    fn deref<'a>(&'a self) -> &'a T { &*self.data }
+    fn deref<'a>(&'a self) -> &'a T { &*self._data }
 }
 impl<'a, T: Send + Share> DerefMut<T> for RWLockWriteGuard<'a, T> {
-    fn deref_mut<'a>(&'a mut self) -> &'a mut T { &mut *self.data }
+    fn deref_mut<'a>(&'a mut self) -> &'a mut T { &mut *self._data }
 }
 
 /****************************************************************************
@@ -812,4 +818,3 @@ mod tests {
         }
     }
 }
-