Skip to content

Commit 90686de

Browse files
committed
add ImmTy::try_from_(u)int methods
1 parent 0bce91f commit 90686de

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/librustc_mir/interpret/operand.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,23 @@ impl<'tcx, Tag: Copy> ImmTy<'tcx, Tag> {
218218
ImmTy { imm: val.into(), layout }
219219
}
220220

221+
#[inline]
222+
pub fn try_from_uint(i: impl Into<u128>, layout: TyLayout<'tcx>) -> InterpResult<'tcx, Self> {
223+
Ok(Self::from_scalar(Scalar::try_from_uint(i, layout.size)?, layout))
224+
}
221225
#[inline]
222226
pub fn from_uint(i: impl Into<u128>, layout: TyLayout<'tcx>) -> Self {
223-
Self::from_scalar(Scalar::from_uint(i, layout.size), layout)
227+
Self::try_from_uint(i, layout).unwrap()
228+
}
229+
230+
#[inline]
231+
pub fn try_from_int(i: impl Into<i128>, layout: TyLayout<'tcx>) -> InterpResult<'tcx, Self> {
232+
Ok(Self::from_scalar(Scalar::try_from_int(i, layout.size)?, layout))
224233
}
225234

226235
#[inline]
227236
pub fn from_int(i: impl Into<i128>, layout: TyLayout<'tcx>) -> Self {
228-
Self::from_scalar(Scalar::from_int(i, layout.size), layout)
237+
Self::try_from_int(i, layout).unwrap()
229238
}
230239

231240
#[inline]

0 commit comments

Comments
 (0)