Skip to content

Commit fda176b

Browse files
committed
auto merge of #6337 : thestinger/rust/fix_region, r=thestinger
also removes unnecessary casts from the RcMut implementation
2 parents 936c07d + 044abef commit fda176b

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

doc/tutorial-ffi.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ pub struct Unique<T> {
157157
priv ptr: *mut T
158158
}
159159
160-
pub impl<'self, T: Owned> Unique<T> {
160+
pub impl<T: Owned> Unique<T> {
161161
fn new(value: T) -> Unique<T> {
162162
unsafe {
163163
let ptr = malloc(core::sys::size_of::<T>() as size_t) as *mut T;
@@ -168,14 +168,14 @@ pub impl<'self, T: Owned> Unique<T> {
168168
}
169169
}
170170
171-
// the 'self lifetime results in the same semantics as `&*x` with ~T
172-
fn borrow(&self) -> &'self T {
173-
unsafe { cast::transmute(self.ptr) }
171+
// the 'r lifetime results in the same semantics as `&*x` with ~T
172+
fn borrow<'r>(&'r self) -> &'r T {
173+
unsafe { cast::copy_lifetime(self, &*self.ptr) }
174174
}
175175
176-
// the 'self lifetime results in the same semantics as `&mut *x` with ~T
177-
fn borrow_mut(&mut self) -> &'self mut T {
178-
unsafe { cast::transmute(self.ptr) }
176+
// the 'r lifetime results in the same semantics as `&mut *x` with ~T
177+
fn borrow_mut<'r>(&'r mut self) -> &'r mut T {
178+
unsafe { cast::copy_mut_lifetime(self, &mut *self.ptr) }
179179
}
180180
}
181181

src/libcore/cast.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ pub unsafe fn copy_lifetime<'a,S,T>(_ptr: &'a S, ptr: &T) -> &'a T {
108108
transmute_region(ptr)
109109
}
110110

111+
/// Transforms lifetime of the second pointer to match the first.
112+
#[inline(always)]
113+
pub unsafe fn copy_mut_lifetime<'a,S,T>(_ptr: &'a mut S, ptr: &mut T) -> &'a mut T {
114+
transmute_mut_region(ptr)
115+
}
116+
111117
/// Transforms lifetime of the second pointer to match the first.
112118
#[inline(always)]
113119
pub unsafe fn copy_lifetime_vec<'a,S,T>(_ptr: &'a [S], ptr: &T) -> &'a T {

src/libstd/rc.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub struct Rc<T> {
2929
priv ptr: *mut RcBox<T>,
3030
}
3131

32-
pub impl<'self, T: Owned> Rc<T> {
32+
pub impl<T: Owned> Rc<T> {
3333
fn new(value: T) -> Rc<T> {
3434
unsafe {
3535
let ptr = malloc(sys::size_of::<RcBox<T>>() as size_t) as *mut RcBox<T>;
@@ -40,8 +40,8 @@ pub impl<'self, T: Owned> Rc<T> {
4040
}
4141

4242
#[inline(always)]
43-
fn borrow(&self) -> &'self T {
44-
unsafe { cast::transmute_region(&(*self.ptr).value) }
43+
fn borrow<'r>(&'r self) -> &'r T {
44+
unsafe { cast::copy_lifetime(self, &(*self.ptr).value) }
4545
}
4646
}
4747

@@ -119,7 +119,7 @@ pub struct RcMut<T> {
119119
priv ptr: *mut RcMutBox<T>,
120120
}
121121

122-
pub impl<'self, T: Owned> RcMut<T> {
122+
pub impl<T: Owned> RcMut<T> {
123123
fn new(value: T) -> RcMut<T> {
124124
unsafe {
125125
let ptr = malloc(sys::size_of::<RcMutBox<T>>() as size_t) as *mut RcMutBox<T>;
@@ -136,7 +136,7 @@ pub impl<'self, T: Owned> RcMut<T> {
136136
assert!((*self.ptr).borrow != Mutable);
137137
let previous = (*self.ptr).borrow;
138138
(*self.ptr).borrow = Immutable;
139-
f(cast::transmute_region(&(*self.ptr).value));
139+
f(&(*self.ptr).value);
140140
(*self.ptr).borrow = previous;
141141
}
142142
}
@@ -147,7 +147,7 @@ pub impl<'self, T: Owned> RcMut<T> {
147147
unsafe {
148148
assert!((*self.ptr).borrow == Nothing);
149149
(*self.ptr).borrow = Mutable;
150-
f(cast::transmute_mut_region(&mut (*self.ptr).value));
150+
f(&mut (*self.ptr).value);
151151
(*self.ptr).borrow = Nothing;
152152
}
153153
}

0 commit comments

Comments
 (0)