mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-08 09:38:26 +03:00
Arc: refactor data_offset{_sized}.
This commit is contained in:
@@ -2298,20 +2298,21 @@ fn as_ref(&self) -> &T {
|
||||
#[stable(feature = "pin", since = "1.33.0")]
|
||||
impl<T: ?Sized> Unpin for Arc<T> { }
|
||||
|
||||
/// Computes the offset of the data field within ArcInner.
|
||||
/// Computes the offset of the data field within `ArcInner`.
|
||||
unsafe fn data_offset<T: ?Sized>(ptr: *const T) -> isize {
|
||||
// Align the unsized value to the end of the ArcInner.
|
||||
// Because it is ?Sized, it will always be the last field in memory.
|
||||
let align = align_of_val(&*ptr);
|
||||
let layout = Layout::new::<ArcInner<()>>();
|
||||
(layout.size() + layout.padding_needed_for(align)) as isize
|
||||
// Align the unsized value to the end of the `ArcInner`.
|
||||
// Because it is `?Sized`, it will always be the last field in memory.
|
||||
data_offset_align(align_of_val(&*ptr))
|
||||
}
|
||||
|
||||
/// Computes the offset of the data field within ArcInner.
|
||||
/// Computes the offset of the data field within `ArcInner`.
|
||||
///
|
||||
/// 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::<ArcInner<()>>();
|
||||
(layout.size() + layout.padding_needed_for(align)) as isize
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user