From 90686ded25ee4f7f6676141a0413c066a2a4d393 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Sat, 14 Dec 2019 12:13:26 -0500 Subject: [PATCH] add ImmTy::try_from_(u)int methods --- src/librustc_mir/interpret/operand.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/librustc_mir/interpret/operand.rs b/src/librustc_mir/interpret/operand.rs index 48e7193ec39d..072152a38877 100644 --- a/src/librustc_mir/interpret/operand.rs +++ b/src/librustc_mir/interpret/operand.rs @@ -218,14 +218,23 @@ impl<'tcx, Tag: Copy> ImmTy<'tcx, Tag> { ImmTy { imm: val.into(), layout } } + #[inline] + pub fn try_from_uint(i: impl Into, layout: TyLayout<'tcx>) -> InterpResult<'tcx, Self> { + Ok(Self::from_scalar(Scalar::try_from_uint(i, layout.size)?, layout)) + } #[inline] pub fn from_uint(i: impl Into, layout: TyLayout<'tcx>) -> Self { - Self::from_scalar(Scalar::from_uint(i, layout.size), layout) + Self::try_from_uint(i, layout).unwrap() + } + + #[inline] + pub fn try_from_int(i: impl Into, layout: TyLayout<'tcx>) -> InterpResult<'tcx, Self> { + Ok(Self::from_scalar(Scalar::try_from_int(i, layout.size)?, layout)) } #[inline] pub fn from_int(i: impl Into, layout: TyLayout<'tcx>) -> Self { - Self::from_scalar(Scalar::from_int(i, layout.size), layout) + Self::try_from_int(i, layout).unwrap() } #[inline]