From 044abef0e56f02c36cf93fd9572813c2b27f98af Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Wed, 8 May 2013 15:03:39 -0400 Subject: [PATCH] fix incorrect region code based on the old 'self also removes unnecessary casts from the RcMut implementation --- doc/tutorial-ffi.md | 14 +++++++------- src/libcore/cast.rs | 6 ++++++ src/libstd/rc.rs | 12 ++++++------ 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/doc/tutorial-ffi.md b/doc/tutorial-ffi.md index b806df5dd20..2a3d8dc1481 100644 --- a/doc/tutorial-ffi.md +++ b/doc/tutorial-ffi.md @@ -157,7 +157,7 @@ pub struct Unique { priv ptr: *mut T } -pub impl<'self, T: Owned> Unique { +pub impl Unique { fn new(value: T) -> Unique { unsafe { let ptr = malloc(core::sys::size_of::() as size_t) as *mut T; @@ -168,14 +168,14 @@ pub impl<'self, T: Owned> Unique { } } - // the 'self lifetime results in the same semantics as `&*x` with ~T - fn borrow(&self) -> &'self T { - unsafe { cast::transmute(self.ptr) } + // the 'r lifetime results in the same semantics as `&*x` with ~T + fn borrow<'r>(&'r self) -> &'r T { + unsafe { cast::copy_lifetime(self, &*self.ptr) } } - // the 'self lifetime results in the same semantics as `&mut *x` with ~T - fn borrow_mut(&mut self) -> &'self mut T { - unsafe { cast::transmute(self.ptr) } + // the 'r lifetime results in the same semantics as `&mut *x` with ~T + fn borrow_mut<'r>(&'r mut self) -> &'r mut T { + unsafe { cast::copy_mut_lifetime(self, &mut *self.ptr) } } } diff --git a/src/libcore/cast.rs b/src/libcore/cast.rs index 96e1c3bd124..c648ec198e1 100644 --- a/src/libcore/cast.rs +++ b/src/libcore/cast.rs @@ -108,6 +108,12 @@ pub unsafe fn copy_lifetime<'a,S,T>(_ptr: &'a S, ptr: &T) -> &'a T { transmute_region(ptr) } +/// Transforms lifetime of the second pointer to match the first. +#[inline(always)] +pub unsafe fn copy_mut_lifetime<'a,S,T>(_ptr: &'a mut S, ptr: &mut T) -> &'a mut T { + transmute_mut_region(ptr) +} + /// Transforms lifetime of the second pointer to match the first. #[inline(always)] pub unsafe fn copy_lifetime_vec<'a,S,T>(_ptr: &'a [S], ptr: &T) -> &'a T { diff --git a/src/libstd/rc.rs b/src/libstd/rc.rs index 6f72d8dc16d..815f03f4269 100644 --- a/src/libstd/rc.rs +++ b/src/libstd/rc.rs @@ -29,7 +29,7 @@ pub struct Rc { priv ptr: *mut RcBox, } -pub impl<'self, T: Owned> Rc { +pub impl Rc { fn new(value: T) -> Rc { unsafe { let ptr = malloc(sys::size_of::>() as size_t) as *mut RcBox; @@ -40,8 +40,8 @@ pub impl<'self, T: Owned> Rc { } #[inline(always)] - fn borrow(&self) -> &'self T { - unsafe { cast::transmute_region(&(*self.ptr).value) } + fn borrow<'r>(&'r self) -> &'r T { + unsafe { cast::copy_lifetime(self, &(*self.ptr).value) } } } @@ -119,7 +119,7 @@ pub struct RcMut { priv ptr: *mut RcMutBox, } -pub impl<'self, T: Owned> RcMut { +pub impl RcMut { fn new(value: T) -> RcMut { unsafe { let ptr = malloc(sys::size_of::>() as size_t) as *mut RcMutBox; @@ -136,7 +136,7 @@ pub impl<'self, T: Owned> RcMut { assert!((*self.ptr).borrow != Mutable); let previous = (*self.ptr).borrow; (*self.ptr).borrow = Immutable; - f(cast::transmute_region(&(*self.ptr).value)); + f(&(*self.ptr).value); (*self.ptr).borrow = previous; } } @@ -147,7 +147,7 @@ pub impl<'self, T: Owned> RcMut { unsafe { assert!((*self.ptr).borrow == Nothing); (*self.ptr).borrow = Mutable; - f(cast::transmute_mut_region(&mut (*self.ptr).value)); + f(&mut (*self.ptr).value); (*self.ptr).borrow = Nothing; } }