diff options
| author | CAD97 <cad97@cad97.com> | 2021-01-07 13:39:32 -0500 |
|---|---|---|
| committer | CAD97 <cad97@cad97.com> | 2021-01-07 13:40:57 -0500 |
| commit | 4901c55af7ec98231653cd0c3a0b1a6937eac743 (patch) | |
| tree | 681781c63ec4ea965123839899294c6168398c0a | |
| parent | 1e578c9fb051ed3a4e608a43d840ad2877701333 (diff) | |
| download | rust-4901c55af7ec98231653cd0c3a0b1a6937eac743.tar.gz rust-4901c55af7ec98231653cd0c3a0b1a6937eac743.zip | |
Replace set_data_ptr with pointer::set_ptr_value
| -rw-r--r-- | library/alloc/src/rc.rs | 19 | ||||
| -rw-r--r-- | library/alloc/src/sync.rs | 18 |
2 files changed, 5 insertions, 32 deletions
diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index dd7710e5c05..ed22571409c 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -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) } } @@ -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), ) } } @@ -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` diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index 6241ee626f7..aa6acdbff34 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -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) } @@ -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>, ) } } @@ -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`. |
