From 9698221f919a80f2a0810e17c8ee8e80da8cefeb Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Sat, 24 May 2014 21:35:53 +1000 Subject: 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 = ...; 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 --- src/libsync/lock.rs | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) (limited to 'src/libsync') 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 { /// 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 Mutex { 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 Mutex { } impl<'a, T: Send> Deref 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 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 { /// 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 RWLock { @@ -320,7 +326,7 @@ impl RWLock { 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 RWLock { 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 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 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 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 { } } } - -- cgit 1.4.1-3-g733a5