Skip to content

Commit c842f02

Browse files
committed
Use pointer offset instead of deref for A/Rc::into_raw
1 parent 7dbfb0a commit c842f02

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/liballoc/rc.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -571,9 +571,14 @@ impl<T: ?Sized> Rc<T> {
571571
/// ```
572572
#[stable(feature = "rc_raw", since = "1.17.0")]
573573
pub fn into_raw(this: Self) -> *const T {
574-
let ptr: *const T = &*this;
574+
let ptr: *mut RcBox<T> = NonNull::as_ptr(this.ptr);
575+
let fake_ptr = ptr as *mut T;
575576
mem::forget(this);
576-
ptr
577+
578+
unsafe {
579+
let offset = data_offset(&(*ptr).value);
580+
set_data_ptr(fake_ptr, (ptr as *mut u8).offset(offset))
581+
}
577582
}
578583

579584
/// Constructs an `Rc` from a raw pointer.

src/liballoc/sync.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -551,9 +551,14 @@ impl<T: ?Sized> Arc<T> {
551551
/// ```
552552
#[stable(feature = "rc_raw", since = "1.17.0")]
553553
pub fn into_raw(this: Self) -> *const T {
554-
let ptr: *const T = &*this;
554+
let ptr: *mut ArcInner<T> = NonNull::as_ptr(this.ptr);
555+
let fake_ptr = ptr as *mut T;
555556
mem::forget(this);
556-
ptr
557+
558+
unsafe {
559+
let offset = data_offset(&(*ptr).data);
560+
set_data_ptr(fake_ptr, (ptr as *mut u8).offset(offset))
561+
}
557562
}
558563

559564
/// Constructs an `Arc` from a raw pointer.

0 commit comments

Comments
 (0)