From f60719575830cde2f98685c4ac9804e080105887 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 3 Jun 2022 16:45:35 -0400 Subject: [PATCH] implement ptr.addr() via transmute --- core/src/ptr/const_ptr.rs | 4 +++- core/src/ptr/mut_ptr.rs | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/core/src/ptr/const_ptr.rs b/core/src/ptr/const_ptr.rs index f26fdc74c..490d7594b 100644 --- a/core/src/ptr/const_ptr.rs +++ b/core/src/ptr/const_ptr.rs @@ -180,7 +180,9 @@ impl *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/core/src/ptr/mut_ptr.rs b/core/src/ptr/mut_ptr.rs index 1fbf592c2..5846c855e 100644 --- a/core/src/ptr/mut_ptr.rs +++ b/core/src/ptr/mut_ptr.rs @@ -184,7 +184,9 @@ impl *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