Skip to content

Commit

Permalink
Replace set_data_ptr with pointer::set_ptr_value
Browse files Browse the repository at this point in the history
  • Loading branch information
CAD97 committed Jan 7, 2021
1 parent 1e578c9 commit 4901c55
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 32 deletions.
19 changes: 3 additions & 16 deletions library/alloc/src/rc.rs
Expand Up @@ -827,8 +827,8 @@ impl<T: ?Sized> Rc<T> {
let offset = unsafe { data_offset(ptr) };

// Reverse the offset to find the original RcBox.
let fake_ptr = ptr as *mut RcBox<T>;
let rc_ptr = unsafe { set_data_ptr(fake_ptr, (ptr as *mut u8).offset(-offset)) };
let rc_ptr =
unsafe { (ptr as *mut RcBox<T>).set_ptr_value((ptr as *mut u8).offset(-offset)) };

unsafe { Self::from_ptr(rc_ptr) }
}
Expand Down Expand Up @@ -1154,7 +1154,7 @@ impl<T: ?Sized> Rc<T> {
Self::allocate_for_layout(
Layout::for_value(&*ptr),
|layout| Global.allocate(layout),
|mem| set_data_ptr(ptr as *mut T, mem) as *mut RcBox<T>,
|mem| (ptr as *mut RcBox<T>).set_ptr_value(mem),
)
}
}
Expand Down Expand Up @@ -1193,20 +1193,7 @@ impl<T> Rc<[T]> {
)
}
}
}

/// Sets the data pointer of a `?Sized` raw pointer.
///
/// For a slice/trait object, this sets the `data` field and leaves the rest
/// unchanged. For a sized raw pointer, this simply sets the pointer.
unsafe fn set_data_ptr<T: ?Sized, U>(mut ptr: *mut T, data: *mut U) -> *mut T {
unsafe {
ptr::write(&mut ptr as *mut _ as *mut *mut u8, data as *mut u8);
}
ptr
}

impl<T> Rc<[T]> {
/// Copy elements from slice into newly allocated Rc<\[T\]>
///
/// Unsafe because the caller must either take ownership or bind `T: Copy`
Expand Down
18 changes: 2 additions & 16 deletions library/alloc/src/sync.rs
Expand Up @@ -844,8 +844,7 @@ impl<T: ?Sized> Arc<T> {
let offset = data_offset(ptr);

// Reverse the offset to find the original ArcInner.
let fake_ptr = ptr as *mut ArcInner<T>;
let arc_ptr = set_data_ptr(fake_ptr, (ptr as *mut u8).offset(-offset));
let arc_ptr = (ptr as *mut ArcInner<T>).set_ptr_value((ptr as *mut u8).offset(-offset));

Self::from_ptr(arc_ptr)
}
Expand Down Expand Up @@ -1129,7 +1128,7 @@ impl<T: ?Sized> Arc<T> {
Self::allocate_for_layout(
Layout::for_value(&*ptr),
|layout| Global.allocate(layout),
|mem| set_data_ptr(ptr as *mut T, mem) as *mut ArcInner<T>,
|mem| (ptr as *mut ArcInner<T>).set_ptr_value(mem) as *mut ArcInner<T>,
)
}
}
Expand Down Expand Up @@ -1168,20 +1167,7 @@ impl<T> Arc<[T]> {
)
}
}
}

/// Sets the data pointer of a `?Sized` raw pointer.
///
/// For a slice/trait object, this sets the `data` field and leaves the rest
/// unchanged. For a sized raw pointer, this simply sets the pointer.
unsafe fn set_data_ptr<T: ?Sized, U>(mut ptr: *mut T, data: *mut U) -> *mut T {
unsafe {
ptr::write(&mut ptr as *mut _ as *mut *mut u8, data as *mut u8);
}
ptr
}

impl<T> Arc<[T]> {
/// Copy elements from slice into newly allocated Arc<\[T\]>
///
/// Unsafe because the caller must either take ownership or bind `T: Copy`.
Expand Down

0 comments on commit 4901c55

Please sign in to comment.