Add fallible Scalar to ScalarInt conversion method
This commit is contained in:
parent
0fe4f38769
commit
858216cabf
1 changed files with 15 additions and 13 deletions
|
@ -346,16 +346,13 @@ impl<'tcx, Tag> Scalar<Tag> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn to_bits(self, target_size: Size) -> InterpResult<'tcx, u128> {
|
fn to_bits(self, target_size: Size) -> InterpResult<'tcx, u128> {
|
||||||
assert_ne!(target_size.bytes(), 0, "you should never look at the bits of a ZST");
|
assert_ne!(target_size.bytes(), 0, "you should never look at the bits of a ZST");
|
||||||
match self {
|
self.to_int()?.to_bits(target_size).map_err(|size| {
|
||||||
Scalar::Int(int) => int.to_bits(target_size).map_err(|size| {
|
err_ub!(ScalarSizeMismatch {
|
||||||
err_ub!(ScalarSizeMismatch {
|
target_size: target_size.bytes(),
|
||||||
target_size: target_size.bytes(),
|
data_size: size.bytes(),
|
||||||
data_size: size.bytes(),
|
})
|
||||||
})
|
.into()
|
||||||
.into()
|
})
|
||||||
}),
|
|
||||||
Scalar::Ptr(_) => throw_unsup!(ReadPointerAsBytes),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
|
@ -364,13 +361,18 @@ impl<'tcx, Tag> Scalar<Tag> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn assert_int(self) -> ScalarInt {
|
pub fn to_int(self) -> InterpResult<'tcx, ScalarInt> {
|
||||||
match self {
|
match self {
|
||||||
Scalar::Ptr(_) => bug!("expected an int but got an abstract pointer"),
|
Scalar::Ptr(_) => throw_unsup!(ReadPointerAsBytes),
|
||||||
Scalar::Int(int) => int,
|
Scalar::Int(int) => Ok(int),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn assert_int(self) -> ScalarInt {
|
||||||
|
self.to_int().expect("expected an int but got an abstract pointer")
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn assert_ptr(self) -> Pointer<Tag> {
|
pub fn assert_ptr(self) -> Pointer<Tag> {
|
||||||
match self {
|
match self {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue