Skip to content

Commit 69f8eb1

Browse files
committed
Use byte_sub in [a]rc impl
1 parent 95e7764 commit 69f8eb1

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

library/alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
#![cfg_attr(test, feature(new_uninit))]
126126
#![feature(nonnull_slice_from_raw_parts)]
127127
#![feature(pattern)]
128+
#![feature(pointer_byte_offsets)]
128129
#![feature(ptr_internals)]
129130
#![feature(ptr_metadata)]
130131
#![feature(ptr_sub_ptr)]

library/alloc/src/rc.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -907,8 +907,7 @@ impl<T: ?Sized> Rc<T> {
907907
let offset = unsafe { data_offset(ptr) };
908908

909909
// Reverse the offset to find the original RcBox.
910-
let rc_ptr =
911-
unsafe { (ptr as *mut u8).offset(-offset).with_metadata_of(ptr as *mut RcBox<T>) };
910+
let rc_ptr = unsafe { ptr.byte_sub(offset) as *mut RcBox<T> };
912911

913912
unsafe { Self::from_ptr(rc_ptr) }
914913
}
@@ -2331,7 +2330,7 @@ impl<T: ?Sized> Weak<T> {
23312330
let offset = unsafe { data_offset(ptr) };
23322331
// Thus, we reverse the offset to get the whole RcBox.
23332332
// SAFETY: the pointer originated from a Weak, so this offset is safe.
2334-
unsafe { (ptr as *mut u8).offset(-offset).with_metadata_of(ptr as *mut RcBox<T>) }
2333+
unsafe { ptr.byte_sub(offset) as *mut RcBox<T> }
23352334
};
23362335

23372336
// SAFETY: we now have recovered the original Weak pointer, so can create the Weak.
@@ -2684,7 +2683,7 @@ impl<T: ?Sized> Unpin for Rc<T> {}
26842683
///
26852684
/// The pointer must point to (and have valid metadata for) a previously
26862685
/// valid instance of T, but the T is allowed to be dropped.
2687-
unsafe fn data_offset<T: ?Sized>(ptr: *const T) -> isize {
2686+
unsafe fn data_offset<T: ?Sized>(ptr: *const T) -> usize {
26882687
// Align the unsized value to the end of the RcBox.
26892688
// Because RcBox is repr(C), it will always be the last field in memory.
26902689
// SAFETY: since the only unsized types possible are slices, trait objects,
@@ -2695,7 +2694,7 @@ unsafe fn data_offset<T: ?Sized>(ptr: *const T) -> isize {
26952694
}
26962695

26972696
#[inline]
2698-
fn data_offset_align(align: usize) -> isize {
2697+
fn data_offset_align(align: usize) -> usize {
26992698
let layout = Layout::new::<RcBox<()>>();
2700-
(layout.size() + layout.padding_needed_for(align)) as isize
2699+
layout.size() + layout.padding_needed_for(align)
27012700
}

library/alloc/src/sync.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -908,8 +908,7 @@ impl<T: ?Sized> Arc<T> {
908908
let offset = data_offset(ptr);
909909

910910
// Reverse the offset to find the original ArcInner.
911-
let arc_ptr =
912-
(ptr as *mut u8).offset(-offset).with_metadata_of(ptr as *mut ArcInner<T>);
911+
let arc_ptr = ptr.byte_sub(offset) as *mut ArcInner<T>;
913912

914913
Self::from_ptr(arc_ptr)
915914
}
@@ -1942,7 +1941,7 @@ impl<T: ?Sized> Weak<T> {
19421941
let offset = unsafe { data_offset(ptr) };
19431942
// Thus, we reverse the offset to get the whole RcBox.
19441943
// SAFETY: the pointer originated from a Weak, so this offset is safe.
1945-
unsafe { (ptr as *mut u8).offset(-offset).with_metadata_of(ptr as *mut ArcInner<T>) }
1944+
unsafe { ptr.byte_sub(offset) as *mut ArcInner<T> }
19461945
};
19471946

19481947
// SAFETY: we now have recovered the original Weak pointer, so can create the Weak.
@@ -2749,7 +2748,7 @@ impl<T: ?Sized> Unpin for Arc<T> {}
27492748
///
27502749
/// The pointer must point to (and have valid metadata for) a previously
27512750
/// valid instance of T, but the T is allowed to be dropped.
2752-
unsafe fn data_offset<T: ?Sized>(ptr: *const T) -> isize {
2751+
unsafe fn data_offset<T: ?Sized>(ptr: *const T) -> usize {
27532752
// Align the unsized value to the end of the ArcInner.
27542753
// Because RcBox is repr(C), it will always be the last field in memory.
27552754
// SAFETY: since the only unsized types possible are slices, trait objects,
@@ -2760,7 +2759,7 @@ unsafe fn data_offset<T: ?Sized>(ptr: *const T) -> isize {
27602759
}
27612760

27622761
#[inline]
2763-
fn data_offset_align(align: usize) -> isize {
2762+
fn data_offset_align(align: usize) -> usize {
27642763
let layout = Layout::new::<ArcInner<()>>();
2765-
(layout.size() + layout.padding_needed_for(align)) as isize
2764+
layout.size() + layout.padding_needed_for(align)
27662765
}

0 commit comments

Comments
 (0)