Skip to content

Commit 7b7c6ce

Browse files
committed
add method to obtain the ptr offset of a Scalar
1 parent a0074ca commit 7b7c6ce

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

Diff for: src/librustc/mir/interpret/value.rs

+13
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,19 @@ impl<'tcx, Tag> Scalar<Tag> {
202202
}
203203
}
204204

205+
/// Returns this pointers offset from the allocation base, or from NULL (for
206+
/// integer pointers).
207+
#[inline]
208+
pub fn get_ptr_offset(self, cx: &impl HasDataLayout) -> Size {
209+
match self {
210+
Scalar::Bits { bits, size } => {
211+
assert_eq!(size as u64, cx.pointer_size().bytes());
212+
Size::from_bytes(bits as u64)
213+
}
214+
Scalar::Ptr(ptr) => ptr.offset,
215+
}
216+
}
217+
205218
#[inline]
206219
pub fn is_null_ptr(self, cx: &impl HasDataLayout) -> bool {
207220
match self {

0 commit comments

Comments
 (0)