Rollup merge of #67299 - christianpoveda:try_immty_from_int, r=RalfJung
Add `ImmTy::try_from_(u)int` methods r? @RalfJung
This commit is contained in:
commit
2b2cc38f5c
@ -236,14 +236,22 @@ pub fn from_char(c: char) -> Self {
|
||||
Scalar::Raw { data: c as u128, size: 4 }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn try_from_uint(i: impl Into<u128>, size: Size) -> Option<Self> {
|
||||
let i = i.into();
|
||||
if truncate(i, size) == i {
|
||||
Some(Scalar::Raw { data: i, size: size.bytes() as u8 })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn from_uint(i: impl Into<u128>, size: Size) -> Self {
|
||||
let i = i.into();
|
||||
assert_eq!(
|
||||
truncate(i, size), i,
|
||||
"Unsigned value {:#x} does not fit in {} bits", i, size.bits()
|
||||
);
|
||||
Scalar::Raw { data: i, size: size.bytes() as u8 }
|
||||
Self::try_from_uint(i, size).unwrap_or_else(|| {
|
||||
bug!("Unsigned value {:#x} does not fit in {} bits", i, size.bits())
|
||||
})
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@ -267,15 +275,23 @@ pub fn from_u64(i: u64) -> Self {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn from_int(i: impl Into<i128>, size: Size) -> 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);
|
||||
assert_eq!(
|
||||
sign_extend(truncated, size) as i128, i,
|
||||
"Signed value {:#x} does not fit in {} bits", i, size.bits()
|
||||
);
|
||||
Scalar::Raw { data: truncated, size: size.bytes() as u8 }
|
||||
if sign_extend(truncated, size) as i128 == i {
|
||||
Some(Scalar::Raw { data: truncated, size: size.bytes() as u8 })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn from_int(i: impl Into<i128>, size: Size) -> Self {
|
||||
let i = i.into();
|
||||
Self::try_from_int(i, size).unwrap_or_else(|| {
|
||||
bug!("Signed value {:#x} does not fit in {} bits", i, size.bits())
|
||||
})
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -218,11 +218,20 @@ pub fn from_scalar(val: Scalar<Tag>, layout: TyLayout<'tcx>) -> Self {
|
||||
ImmTy { imm: val.into(), layout }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
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 {
|
||||
Self::from_scalar(Scalar::from_uint(i, layout.size), layout)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
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]
|
||||
pub fn from_int(i: impl Into<i128>, layout: TyLayout<'tcx>) -> Self {
|
||||
Self::from_scalar(Scalar::from_int(i, layout.size), layout)
|
||||
|
Loading…
Reference in New Issue
Block a user