about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCAD97 <cad97@cad97.com>2020-06-30 12:47:23 -0400
committerCAD97 <cad97@cad97.com>2020-06-30 15:03:04 -0400
commitaed88e18049f6be7d3c3b37683d05d777adb3c86 (patch)
treec963fe8c4f71fcb8a23bf0cc9eeabe7233d90c9f
parent0aecf3c74b7fd09460f453e7e95ae8cb65a92440 (diff)
downloadrust-aed88e18049f6be7d3c3b37683d05d777adb3c86.tar.gz
rust-aed88e18049f6be7d3c3b37683d05d777adb3c86.zip
Clarify when rc::data_offset is safe
-rw-r--r--src/liballoc/rc.rs10
-rw-r--r--src/liballoc/sync.rs11
2 files changed, 20 insertions, 1 deletions
diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs
index ab64d533087..24e7d5da7a6 100644
--- a/src/liballoc/rc.rs
+++ b/src/liballoc/rc.rs
@@ -2116,6 +2116,16 @@ impl<T: ?Sized> AsRef<T> for Rc<T> {
 #[stable(feature = "pin", since = "1.33.0")]
 impl<T: ?Sized> Unpin for Rc<T> {}
 
+/// Get the offset within an `ArcInner` for
+/// a payload of type described by a pointer.
+///
+/// # Safety
+///
+/// This has the same safety requirements as `align_of_val_raw`. In effect:
+///
+/// - This function is safe for any argument if `T` is sized, and
+/// - if `T` is unsized, the pointer must have appropriate pointer metadata
+///   aquired from the real instance that you are getting this offset for.
 unsafe fn data_offset<T: ?Sized>(ptr: *const T) -> isize {
     // Align the unsized value to the end of the `RcBox`.
     // Because it is ?Sized, it will always be the last field in memory.
diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs
index e9af80d326f..53ce47d023b 100644
--- a/src/liballoc/sync.rs
+++ b/src/liballoc/sync.rs
@@ -2273,7 +2273,16 @@ impl<T: ?Sized> AsRef<T> for Arc<T> {
 #[stable(feature = "pin", since = "1.33.0")]
 impl<T: ?Sized> Unpin for Arc<T> {}
 
-/// Computes the offset of the data field within `ArcInner`.
+/// Get the offset within an `ArcInner` for
+/// a payload of type described by a pointer.
+///
+/// # Safety
+///
+/// This has the same safety requirements as `align_of_val_raw`. In effect:
+///
+/// - This function is safe for any argument if `T` is sized, and
+/// - if `T` is unsized, the pointer must have appropriate pointer metadata
+///   aquired from the real instance that you are getting this offset for.
 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.