]> git.lizzy.rs Git - rust.git/commitdiff
add method to obtain the ptr offset of a Scalar
authorRalf Jung <post@ralfj.de>
Mon, 5 Nov 2018 14:11:28 +0000 (15:11 +0100)
committerRalf Jung <post@ralfj.de>
Mon, 5 Nov 2018 14:11:28 +0000 (15:11 +0100)
src/librustc/mir/interpret/value.rs

index 66faebb8c03ed9fb73a8a0ca5ffa75356ec5f318..3f5399396abcf837ea2f3b874427cd197ffae868 100644 (file)
@@ -202,6 +202,19 @@ pub fn ptr_wrapping_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> Self
         }
     }
 
+    /// Returns this pointers offset from the allocation base, or from NULL (for
+    /// integer pointers).
+    #[inline]
+    pub fn get_ptr_offset(self, cx: &impl HasDataLayout) -> Size {
+        match self {
+            Scalar::Bits { bits, size } => {
+                assert_eq!(size as u64, cx.pointer_size().bytes());
+                Size::from_bytes(bits as u64)
+            }
+            Scalar::Ptr(ptr) => ptr.offset,
+        }
+    }
+
     #[inline]
     pub fn is_null_ptr(self, cx: &impl HasDataLayout) -> bool {
         match self {