Skip to content

fix incorrect region code based on the old 'self #6337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions doc/tutorial-ffi.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ pub struct Unique<T> {
priv ptr: *mut T
}

pub impl<'self, T: Owned> Unique<T> {
pub impl<T: Owned> Unique<T> {
fn new(value: T) -> Unique<T> {
unsafe {
let ptr = malloc(core::sys::size_of::<T>() as size_t) as *mut T;
Expand All @@ -168,14 +168,14 @@ pub impl<'self, T: Owned> Unique<T> {
}
}

// 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) }
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/libcore/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions src/libstd/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct Rc<T> {
priv ptr: *mut RcBox<T>,
}

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

#[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) }
}
}

Expand Down Expand Up @@ -119,7 +119,7 @@ pub struct RcMut<T> {
priv ptr: *mut RcMutBox<T>,
}

pub impl<'self, T: Owned> RcMut<T> {
pub impl<T: Owned> RcMut<T> {
fn new(value: T) -> RcMut<T> {
unsafe {
let ptr = malloc(sys::size_of::<RcMutBox<T>>() as size_t) as *mut RcMutBox<T>;
Expand All @@ -136,7 +136,7 @@ pub impl<'self, T: Owned> RcMut<T> {
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;
}
}
Expand All @@ -147,7 +147,7 @@ pub impl<'self, T: Owned> RcMut<T> {
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;
}
}
Expand Down