diff options
| author | Huon Wilson <dbau.pp+github@gmail.com> | 2014-05-24 21:35:53 +1000 |
|---|---|---|
| committer | Huon Wilson <dbau.pp+github@gmail.com> | 2014-05-25 10:23:37 +1000 |
| commit | 9698221f919a80f2a0810e17c8ee8e80da8cefeb (patch) | |
| tree | 29b0bb162d7e94d1ebc91d44d6265f211914934b /src/libcore | |
| parent | 9e244d708461d5028066a59e70866f52517e7b85 (diff) | |
| download | rust-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/libcore')
| -rw-r--r-- | src/libcore/cell.rs | 32 |
1 files changed, 18 insertions, 14 deletions
diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 3b1322cdc1b..3a9894a4c65 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -251,7 +251,7 @@ impl<T> RefCell<T> { WRITING => None, borrow => { self.borrow.set(borrow + 1); - Some(Ref { parent: self }) + Some(Ref { _parent: self }) } } } @@ -281,7 +281,7 @@ impl<T> RefCell<T> { match self.borrow.get() { UNUSED => { self.borrow.set(WRITING); - Some(RefMut { parent: self }) + Some(RefMut { _parent: self }) }, _ => None } @@ -317,22 +317,24 @@ impl<T: Eq> Eq for RefCell<T> { /// Wraps a borrowed reference to a value in a `RefCell` box. pub struct Ref<'b, T> { - parent: &'b RefCell<T> + // FIXME #12808: strange name to try to avoid interfering with + // field accesses of the contained type via Deref + _parent: &'b RefCell<T> } #[unsafe_destructor] impl<'b, T> Drop for Ref<'b, T> { fn drop(&mut self) { - let borrow = self.parent.borrow.get(); + let borrow = self._parent.borrow.get(); debug_assert!(borrow != WRITING && borrow != UNUSED); - self.parent.borrow.set(borrow - 1); + self._parent.borrow.set(borrow - 1); } } impl<'b, T> Deref<T> for Ref<'b, T> { #[inline] fn deref<'a>(&'a self) -> &'a T { - unsafe { &*self.parent.value.get() } + unsafe { &*self._parent.value.get() } } } @@ -346,40 +348,42 @@ impl<'b, T> Deref<T> for Ref<'b, T> { pub fn clone_ref<'b, T>(orig: &Ref<'b, T>) -> Ref<'b, T> { // Since this Ref exists, we know the borrow flag // is not set to WRITING. - let borrow = orig.parent.borrow.get(); + let borrow = orig._parent.borrow.get(); debug_assert!(borrow != WRITING && borrow != UNUSED); - orig.parent.borrow.set(borrow + 1); + orig._parent.borrow.set(borrow + 1); Ref { - parent: orig.parent, + _parent: orig._parent, } } /// Wraps a mutable borrowed reference to a value in a `RefCell` box. pub struct RefMut<'b, T> { - parent: &'b RefCell<T> + // FIXME #12808: strange name to try to avoid interfering with + // field accesses of the contained type via Deref + _parent: &'b RefCell<T> } #[unsafe_destructor] impl<'b, T> Drop for RefMut<'b, T> { fn drop(&mut self) { - let borrow = self.parent.borrow.get(); + let borrow = self._parent.borrow.get(); debug_assert!(borrow == WRITING); - self.parent.borrow.set(UNUSED); + self._parent.borrow.set(UNUSED); } } impl<'b, T> Deref<T> for RefMut<'b, T> { #[inline] fn deref<'a>(&'a self) -> &'a T { - unsafe { &*self.parent.value.get() } + unsafe { &*self._parent.value.get() } } } impl<'b, T> DerefMut<T> for RefMut<'b, T> { #[inline] fn deref_mut<'a>(&'a mut self) -> &'a mut T { - unsafe { &mut *self.parent.value.get() } + unsafe { &mut *self._parent.value.get() } } } |
