Skip to content

Commit 2bc59c7

Browse files
committed
add some helper methods to ScalarInt
1 parent 5176945 commit 2bc59c7

File tree

1 file changed

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

1 file changed

+92
-0
lines changed

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

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,98 @@ impl ScalarInt {
237237
pub fn try_to_machine_usize<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Result<u64, Size> {
238238
Ok(self.to_bits(tcx.data_layout.pointer_size)? as u64)
239239
}
240+
241+
/// Tries to convert the `ScalarInt` to an unsigned integer of the given size.
242+
/// Fails if the size of the `ScalarInt` is unequal to `size` and returns the
243+
/// `ScalarInt`s size in that case.
244+
#[inline]
245+
pub fn try_to_uint(self, size: Size) -> Result<u128, Size> {
246+
self.to_bits(size)
247+
}
248+
249+
// Tries to convert the `ScalarInt` to `u8`. Fails if the `size` of the `ScalarInt`
250+
// in not equal to `Size { raw: 1 }` and returns the `size` value of the `ScalarInt` in
251+
// that case.
252+
#[inline]
253+
pub fn try_to_u8(self) -> Result<u8, Size> {
254+
self.to_bits(Size::from_bits(8)).map(|v| u8::try_from(v).unwrap())
255+
}
256+
257+
/// Tries to convert the `ScalarInt` to `u16`. Fails if the size of the `ScalarInt`
258+
/// in not equal to `Size { raw: 2 }` and returns the `size` value of the `ScalarInt` in
259+
/// that case.
260+
#[inline]
261+
pub fn try_to_u16(self) -> Result<u16, Size> {
262+
self.to_bits(Size::from_bits(16)).map(|v| u16::try_from(v).unwrap())
263+
}
264+
265+
/// Tries to convert the `ScalarInt` to `u32`. Fails if the `size` of the `ScalarInt`
266+
/// in not equal to `Size { raw: 4 }` and returns the `size` value of the `ScalarInt` in
267+
/// that case.
268+
#[inline]
269+
pub fn try_to_u32(self) -> Result<u32, Size> {
270+
self.to_bits(Size::from_bits(32)).map(|v| u32::try_from(v).unwrap())
271+
}
272+
273+
/// Tries to convert the `ScalarInt` to `u64`. Fails if the `size` of the `ScalarInt`
274+
/// in not equal to `Size { raw: 8 }` and returns the `size` value of the `ScalarInt` in
275+
/// that case.
276+
#[inline]
277+
pub fn try_to_u64(self) -> Result<u64, Size> {
278+
self.to_bits(Size::from_bits(64)).map(|v| u64::try_from(v).unwrap())
279+
}
280+
281+
/// Tries to convert the `ScalarInt` to `u128`. Fails if the `size` of the `ScalarInt`
282+
/// in not equal to `Size { raw: 16 }` and returns the `size` value of the `ScalarInt` in
283+
/// that case.
284+
#[inline]
285+
pub fn try_to_u128(self) -> Result<u128, Size> {
286+
self.to_bits(Size::from_bits(128))
287+
}
288+
289+
/// Tries to convert the `ScalarInt` to a signed integer of the given size.
290+
/// Fails if the size of the `ScalarInt` is unequal to `size` and returns the
291+
/// `ScalarInt`s size in that case.
292+
#[inline]
293+
pub fn try_to_int(self, size: Size) -> Result<i128, Size> {
294+
let b = self.to_bits(size)?;
295+
Ok(size.sign_extend(b) as i128)
296+
}
297+
298+
/// Tries to convert the `ScalarInt` to i8.
299+
/// Fails if the size of the `ScalarInt` is unequal to `Size { raw: 1 }`
300+
/// and returns the `ScalarInt`s size in that case.
301+
pub fn try_to_i8(self) -> Result<i8, Size> {
302+
self.try_to_int(Size::from_bits(8)).map(|v| i8::try_from(v).unwrap())
303+
}
304+
305+
/// Tries to convert the `ScalarInt` to i16.
306+
/// Fails if the size of the `ScalarInt` is unequal to `Size { raw: 2 }`
307+
/// and returns the `ScalarInt`s size in that case.
308+
pub fn try_to_i16(self) -> Result<i16, Size> {
309+
self.try_to_int(Size::from_bits(16)).map(|v| i16::try_from(v).unwrap())
310+
}
311+
312+
/// Tries to convert the `ScalarInt` to i32.
313+
/// Fails if the size of the `ScalarInt` is unequal to `Size { raw: 4 }`
314+
/// and returns the `ScalarInt`s size in that case.
315+
pub fn try_to_i32(self) -> Result<i32, Size> {
316+
self.try_to_int(Size::from_bits(32)).map(|v| i32::try_from(v).unwrap())
317+
}
318+
319+
/// Tries to convert the `ScalarInt` to i64.
320+
/// Fails if the size of the `ScalarInt` is unequal to `Size { raw: 8 }`
321+
/// and returns the `ScalarInt`s size in that case.
322+
pub fn try_to_i64(self) -> Result<i64, Size> {
323+
self.try_to_int(Size::from_bits(64)).map(|v| i64::try_from(v).unwrap())
324+
}
325+
326+
/// Tries to convert the `ScalarInt` to i128.
327+
/// Fails if the size of the `ScalarInt` is unequal to `Size { raw: 16 }`
328+
/// and returns the `ScalarInt`s size in that case.
329+
pub fn try_to_i128(self) -> Result<i128, Size> {
330+
self.try_to_int(Size::from_bits(128)).map(|v| i128::try_from(v).unwrap())
331+
}
240332
}
241333

242334
macro_rules! from {

0 commit comments

Comments
 (0)