about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2022-06-03 16:45:35 -0400
committerRalf Jung <post@ralfj.de>2022-06-03 16:45:35 -0400
commit4291332175d12e79e6061cdc3f5dccac2e28b969 (patch)
tree2ea3814602b8f2ff68af95105cd40f1f326c751f
parent5e6bb83268518dcd74c96b5504f485b71e604e4c (diff)
downloadrust-4291332175d12e79e6061cdc3f5dccac2e28b969.tar.gz
rust-4291332175d12e79e6061cdc3f5dccac2e28b969.zip
implement ptr.addr() via transmute
-rw-r--r--library/core/src/ptr/const_ptr.rs4
-rw-r--r--library/core/src/ptr/mut_ptr.rs4
2 files changed, 6 insertions, 2 deletions
diff --git a/library/core/src/ptr/const_ptr.rs b/library/core/src/ptr/const_ptr.rs
index f26fdc74ce1..490d7594bb8 100644
--- a/library/core/src/ptr/const_ptr.rs
+++ b/library/core/src/ptr/const_ptr.rs
@@ -180,7 +180,9 @@ impl<T: ?Sized> *const T {
         T: Sized,
     {
         // FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
-        self as usize
+        // SAFETY: Pointer-to-integer transmutes are valid (if you are okay with losing the
+        // provenance).
+        unsafe { mem::transmute(self) }
     }
 
     /// Gets the "address" portion of the pointer, and 'exposes' the "provenance" part for future
diff --git a/library/core/src/ptr/mut_ptr.rs b/library/core/src/ptr/mut_ptr.rs
index 1fbf592c232..5846c855e8f 100644
--- a/library/core/src/ptr/mut_ptr.rs
+++ b/library/core/src/ptr/mut_ptr.rs
@@ -184,7 +184,9 @@ impl<T: ?Sized> *mut T {
         T: Sized,
     {
         // FIXME(strict_provenance_magic): I am magic and should be a compiler intrinsic.
-        self as usize
+        // SAFETY: Pointer-to-integer transmutes are valid (if you are okay with losing the
+        // provenance).
+        unsafe { mem::transmute(self) }
     }
 
     /// Gets the "address" portion of the pointer, and 'exposes' the "provenance" part for future