Skip to content

Commit aed88e1

Browse files
committed
Clarify when rc::data_offset is safe
1 parent 0aecf3c commit aed88e1

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/liballoc/rc.rs

+10
Original file line numberDiff line numberDiff line change
@@ -2116,6 +2116,16 @@ impl<T: ?Sized> AsRef<T> for Rc<T> {
21162116
#[stable(feature = "pin", since = "1.33.0")]
21172117
impl<T: ?Sized> Unpin for Rc<T> {}
21182118

2119+
/// Get the offset within an `ArcInner` for
2120+
/// a payload of type described by a pointer.
2121+
///
2122+
/// # Safety
2123+
///
2124+
/// This has the same safety requirements as `align_of_val_raw`. In effect:
2125+
///
2126+
/// - This function is safe for any argument if `T` is sized, and
2127+
/// - if `T` is unsized, the pointer must have appropriate pointer metadata
2128+
/// aquired from the real instance that you are getting this offset for.
21192129
unsafe fn data_offset<T: ?Sized>(ptr: *const T) -> isize {
21202130
// Align the unsized value to the end of the `RcBox`.
21212131
// Because it is ?Sized, it will always be the last field in memory.

src/liballoc/sync.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -2273,7 +2273,16 @@ impl<T: ?Sized> AsRef<T> for Arc<T> {
22732273
#[stable(feature = "pin", since = "1.33.0")]
22742274
impl<T: ?Sized> Unpin for Arc<T> {}
22752275

2276-
/// Computes the offset of the data field within `ArcInner`.
2276+
/// Get the offset within an `ArcInner` for
2277+
/// a payload of type described by a pointer.
2278+
///
2279+
/// # Safety
2280+
///
2281+
/// This has the same safety requirements as `align_of_val_raw`. In effect:
2282+
///
2283+
/// - This function is safe for any argument if `T` is sized, and
2284+
/// - if `T` is unsized, the pointer must have appropriate pointer metadata
2285+
/// aquired from the real instance that you are getting this offset for.
22772286
unsafe fn data_offset<T: ?Sized>(ptr: *const T) -> isize {
22782287
// Align the unsized value to the end of the `ArcInner`.
22792288
// Because it is `?Sized`, it will always be the last field in memory.

0 commit comments

Comments
 (0)