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