Skip to content

Commit 0502d42

Browse files
committed
Add some convenience methods to ScalarInt
1 parent 0907476 commit 0502d42

File tree

1 file changed

+17
-0
lines changed
  • compiler/rustc_middle/src/ty/consts

1 file changed

+17
-0
lines changed

compiler/rustc_middle/src/ty/consts/int.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,23 @@ impl ScalarInt {
202202
Ok(Self::try_from_uint(f_int(u64::try_from(self.data).unwrap())?, self.size()).unwrap())
203203
}
204204

205+
#[inline]
206+
pub fn from_uint(i: impl Into<u128>, size: Size) -> Self {
207+
let i = i.into();
208+
Self::try_from_uint(i, size)
209+
.unwrap_or_else(|| bug!("Unsigned value {:#x} does not fit in {} bits", i, size.bits()))
210+
}
211+
212+
#[inline]
213+
pub fn from_bool(b: bool) -> Self {
214+
Self::from_uint(b as u8, Size::from_bytes(1))
215+
}
216+
217+
#[inline]
218+
pub fn from_char(c: char) -> Self {
219+
Self::from_uint(c as u32, Size::from_bytes(4))
220+
}
221+
205222
#[inline]
206223
pub fn try_from_uint(i: impl Into<u128>, size: Size) -> Option<Self> {
207224
let data = i.into();

0 commit comments

Comments
 (0)