@@ -415,61 +415,62 @@ impl<'tcx, Tag> Scalar<Tag> {
415
415
}
416
416
}
417
417
418
+ #[inline]
419
+ fn to_unsigned_with_bit_width(self, bits: u64) -> InterpResult<'static, u128> {
420
+ let sz = Size::from_bits(bits);
421
+ self.to_bits(sz)
422
+ }
423
+
424
+ /// Converts the scalar to produce an `u8`. Fails if the scalar is a pointer.
418
425
pub fn to_u8(self) -> InterpResult<'static, u8> {
419
- let sz = Size::from_bits(8);
420
- let b = self.to_bits(sz)?;
421
- Ok(b as u8)
426
+ self.to_unsigned_with_bit_width(8).map(|v| v as u8)
422
427
}
423
428
429
+ /// Converts the scalar to produce an `u16`. Fails if the scalar is a pointer.
424
430
pub fn to_u16(self) -> InterpResult<'static, u16> {
425
- let sz = Size::from_bits(16);
426
- let b = self.to_bits(sz)?;
427
- Ok(b as u16)
431
+ self.to_unsigned_with_bit_width(16).map(|v| v as u16)
428
432
}
429
433
434
+ /// Converts the scalar to produce an `u32`. Fails if the scalar is a pointer.
430
435
pub fn to_u32(self) -> InterpResult<'static, u32> {
431
- let sz = Size::from_bits(32);
432
- let b = self.to_bits(sz)?;
433
- Ok(b as u32)
436
+ self.to_unsigned_with_bit_width(32).map(|v| v as u32)
434
437
}
435
438
439
+ /// Converts the scalar to produce an `u64`. Fails if the scalar is a pointer.
436
440
pub fn to_u64(self) -> InterpResult<'static, u64> {
437
- let sz = Size::from_bits(64);
438
- let b = self.to_bits(sz)?;
439
- Ok(b as u64)
441
+ self.to_unsigned_with_bit_width(64).map(|v| v as u64)
440
442
}
441
443
442
444
pub fn to_machine_usize(self, cx: &impl HasDataLayout) -> InterpResult<'static, u64> {
443
445
let b = self.to_bits(cx.data_layout().pointer_size)?;
444
446
Ok(b as u64)
445
447
}
446
448
447
- pub fn to_i8(self) -> InterpResult<'static, i8> {
448
- let sz = Size::from_bits(8);
449
+ #[inline]
450
+ fn to_signed_with_bit_width(self, bits: u64) -> InterpResult<'static, i128> {
451
+ let sz = Size::from_bits(bits);
449
452
let b = self.to_bits(sz)?;
450
- let b = sign_extend(b, sz) as i128;
451
- Ok(b as i8)
453
+ Ok(sign_extend(b, sz) as i128)
452
454
}
453
455
456
+ /// Converts the scalar to produce an `i8`. Fails if the scalar is a pointer.
457
+ pub fn to_i8(self) -> InterpResult<'static, i8> {
458
+ self.to_signed_with_bit_width(8).map(|v| v as i8)
459
+ }
460
+
461
+ /// Converts the scalar to produce an `i16`. Fails if the scalar is a pointer.
454
462
pub fn to_i16(self) -> InterpResult<'static, i16> {
455
- let sz = Size::from_bits(16);
456
- let b = self.to_bits(sz)?;
457
- let b = sign_extend(b, sz) as i128;
458
- Ok(b as i16)
463
+ self.to_signed_with_bit_width(16).map(|v| v as i16)
459
464
}
460
465
466
+ /// Converts the scalar to produce an `i32`. Fails if the scalar is a pointer.
461
467
pub fn to_i32(self) -> InterpResult<'static, i32> {
462
- let sz = Size::from_bits(32);
463
- let b = self.to_bits(sz)?;
464
- let b = sign_extend(b, sz) as i128;
465
- Ok(b as i32)
468
+ self.to_signed_with_bit_width(32).map(|v| v as i32)
466
469
}
467
470
471
+ /// Converts the scalar to produce an `i64`. Fails if the scalar is a pointer.
468
472
pub fn to_i64(self) -> InterpResult<'static, i64> {
469
- let sz = Size::from_bits(64);
470
- let b = self.to_bits(sz)?;
471
- let b = sign_extend(b, sz) as i128;
472
- Ok(b as i64)
473
+ self.to_signed_with_bit_width(64).map(|v| v as i64)
473
474
}
474
475
475
476
pub fn to_machine_isize(self, cx: &impl HasDataLayout) -> InterpResult<'static, i64> {
0 commit comments