Change results to options

This commit is contained in:
Christian Poveda 2019-12-21 10:27:58 -05:00
parent 90686ded25
commit 309f437e1d
No known key found for this signature in database
GPG Key ID: 27525EF5E7420A50
2 changed files with 10 additions and 10 deletions

View File

@ -237,12 +237,12 @@ impl<'tcx, Tag> Scalar<Tag> {
}
#[inline]
pub fn try_from_uint(i: impl Into<u128>, size: Size) -> InterpResult<'tcx, Self> {
pub fn try_from_uint(i: impl Into<u128>, size: Size) -> Option<Self> {
let i = i.into();
if truncate(i, size) == i {
Ok(Scalar::Raw { data: i, size: size.bytes() as u8 })
Some(Scalar::Raw { data: i, size: size.bytes() as u8 })
} else {
throw_unsup_format!("Unsigned value {:#x} does not fit in {} bits", i, size.bits())
None
}
}
@ -272,14 +272,14 @@ impl<'tcx, Tag> Scalar<Tag> {
}
#[inline]
pub fn try_from_int(i: impl Into<i128>, size: Size) -> InterpResult<'tcx, Self> {
pub fn try_from_int(i: impl Into<i128>, size: Size) -> Option<Self> {
let i = i.into();
// `into` performed sign extension, we have to truncate
let truncated = truncate(i as u128, size);
if sign_extend(truncated, size) as i128 == i {
Ok(Scalar::Raw { data: truncated, size: size.bytes() as u8 })
Some(Scalar::Raw { data: truncated, size: size.bytes() as u8 })
} else {
throw_unsup_format!("Signed value {:#x} does not fit in {} bits", i, size.bits())
None
}
}

View File

@ -219,8 +219,8 @@ impl<'tcx, Tag: Copy> ImmTy<'tcx, Tag> {
}
#[inline]
pub fn try_from_uint(i: impl Into<u128>, layout: TyLayout<'tcx>) -> InterpResult<'tcx, Self> {
Ok(Self::from_scalar(Scalar::try_from_uint(i, layout.size)?, layout))
pub fn try_from_uint(i: impl Into<u128>, layout: TyLayout<'tcx>) -> Option<Self> {
Some(Self::from_scalar(Scalar::try_from_uint(i, layout.size)?, layout))
}
#[inline]
pub fn from_uint(i: impl Into<u128>, layout: TyLayout<'tcx>) -> Self {
@ -228,8 +228,8 @@ impl<'tcx, Tag: Copy> ImmTy<'tcx, Tag> {
}
#[inline]
pub fn try_from_int(i: impl Into<i128>, layout: TyLayout<'tcx>) -> InterpResult<'tcx, Self> {
Ok(Self::from_scalar(Scalar::try_from_int(i, layout.size)?, layout))
pub fn try_from_int(i: impl Into<i128>, layout: TyLayout<'tcx>) -> Option<Self> {
Some(Self::from_scalar(Scalar::try_from_int(i, layout.size)?, layout))
}
#[inline]