Rc: refactor data_offset{_sized}.

This commit is contained in:
Mazdak Farrokhzad 2019-06-19 04:48:46 +02:00
parent 19982f5653
commit 2efbc9e5a2

View file

@ -2176,18 +2176,19 @@ impl<T: ?Sized> AsRef<T> for Rc<T> {
impl<T: ?Sized> Unpin for Rc<T> { }
unsafe fn data_offset<T: ?Sized>(ptr: *const T) -> isize {
// Align the unsized value to the end of the RcBox.
// Align the unsized value to the end of the `RcBox`.
// Because it is ?Sized, it will always be the last field in memory.
let align = align_of_val(&*ptr);
let layout = Layout::new::<RcBox<()>>();
(layout.size() + layout.padding_needed_for(align)) as isize
data_offset_align(align_of_val(&*ptr))
}
/// Computes the offset of the data field within ArcInner.
/// Computes the offset of the data field within `RcBox`.
///
/// Unlike [`data_offset`], this doesn't need the pointer, but it works only on `T: Sized`.
fn data_offset_sized<T>() -> isize {
let align = align_of::<T>();
data_offset_align(align_of::<T>())
}
fn data_offset_align(align: usize) -> isize {
let layout = Layout::new::<RcBox<()>>();
(layout.size() + layout.padding_needed_for(align)) as isize
}