Skip to content

Commit

Permalink
Add internal safety docs to (A)Rc::into_raw
Browse files Browse the repository at this point in the history
  • Loading branch information
CAD97 committed Dec 17, 2019
1 parent c842f02 commit eb77f7e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/liballoc/rc.rs
Expand Up @@ -575,6 +575,11 @@ impl<T: ?Sized> Rc<T> {
let fake_ptr = ptr as *mut T;
mem::forget(this);

// SAFETY: This cannot go through Deref::deref.
// Instead, we manually offset the pointer rather than manifesting a reference.
// This is so that the returned pointer retains the same provenance as our pointer.
// This is required so that e.g. `get_mut` can write through the pointer
// after the Rc is recovered through `from_raw`.
unsafe {
let offset = data_offset(&(*ptr).value);
set_data_ptr(fake_ptr, (ptr as *mut u8).offset(offset))
Expand Down
5 changes: 5 additions & 0 deletions src/liballoc/sync.rs
Expand Up @@ -555,6 +555,11 @@ impl<T: ?Sized> Arc<T> {
let fake_ptr = ptr as *mut T;
mem::forget(this);

// SAFETY: This cannot go through Deref::deref.
// Instead, we manually offset the pointer rather than manifesting a reference.
// This is so that the returned pointer retains the same provenance as our pointer.
// This is required so that e.g. `get_mut` can write through the pointer
// after the Arc is recovered through `from_raw`.
unsafe {
let offset = data_offset(&(*ptr).data);
set_data_ptr(fake_ptr, (ptr as *mut u8).offset(offset))
Expand Down

0 comments on commit eb77f7e

Please sign in to comment.