@@ -416,48 +416,62 @@ impl<'tcx, Tag> Scalar<Tag> {
416416 }
417417 }
418418
419+ #[ inline]
420+ fn to_unsigned_with_bit_width ( self , bits : u64 ) -> InterpResult < ' static , u128 > {
421+ let sz = Size :: from_bits ( bits) ;
422+ self . to_bits ( sz)
423+ }
424+
425+ /// Converts the scalar to produce an `u8`. Fails if the scalar is a pointer.
419426 pub fn to_u8 ( self ) -> InterpResult < ' static , u8 > {
420- let sz = Size :: from_bits ( 8 ) ;
421- let b = self . to_bits ( sz) ?;
422- Ok ( b as u8 )
427+ self . to_unsigned_with_bit_width ( 8 ) . map ( |v| v as u8 )
428+ }
429+
430+ /// Converts the scalar to produce an `u16`. Fails if the scalar is a pointer.
431+ pub fn to_u16 ( self ) -> InterpResult < ' static , u16 > {
432+ self . to_unsigned_with_bit_width ( 16 ) . map ( |v| v as u16 )
423433 }
424434
435+ /// Converts the scalar to produce an `u32`. Fails if the scalar is a pointer.
425436 pub fn to_u32 ( self ) -> InterpResult < ' static , u32 > {
426- let sz = Size :: from_bits ( 32 ) ;
427- let b = self . to_bits ( sz) ?;
428- Ok ( b as u32 )
437+ self . to_unsigned_with_bit_width ( 32 ) . map ( |v| v as u32 )
429438 }
430439
440+ /// Converts the scalar to produce an `u64`. Fails if the scalar is a pointer.
431441 pub fn to_u64 ( self ) -> InterpResult < ' static , u64 > {
432- let sz = Size :: from_bits ( 64 ) ;
433- let b = self . to_bits ( sz) ?;
434- Ok ( b as u64 )
442+ self . to_unsigned_with_bit_width ( 64 ) . map ( |v| v as u64 )
435443 }
436444
437445 pub fn to_machine_usize ( self , cx : & impl HasDataLayout ) -> InterpResult < ' static , u64 > {
438446 let b = self . to_bits ( cx. data_layout ( ) . pointer_size ) ?;
439447 Ok ( b as u64 )
440448 }
441449
442- pub fn to_i8 ( self ) -> InterpResult < ' static , i8 > {
443- let sz = Size :: from_bits ( 8 ) ;
450+ #[ inline]
451+ fn to_signed_with_bit_width ( self , bits : u64 ) -> InterpResult < ' static , i128 > {
452+ let sz = Size :: from_bits ( bits) ;
444453 let b = self . to_bits ( sz) ?;
445- let b = sign_extend ( b, sz) as i128 ;
446- Ok ( b as i8 )
454+ Ok ( sign_extend ( b, sz) as i128 )
455+ }
456+
457+ /// Converts the scalar to produce an `i8`. Fails if the scalar is a pointer.
458+ pub fn to_i8 ( self ) -> InterpResult < ' static , i8 > {
459+ self . to_signed_with_bit_width ( 8 ) . map ( |v| v as i8 )
460+ }
461+
462+ /// Converts the scalar to produce an `i16`. Fails if the scalar is a pointer.
463+ pub fn to_i16 ( self ) -> InterpResult < ' static , i16 > {
464+ self . to_signed_with_bit_width ( 16 ) . map ( |v| v as i16 )
447465 }
448466
467+ /// Converts the scalar to produce an `i32`. Fails if the scalar is a pointer.
449468 pub fn to_i32 ( self ) -> InterpResult < ' static , i32 > {
450- let sz = Size :: from_bits ( 32 ) ;
451- let b = self . to_bits ( sz) ?;
452- let b = sign_extend ( b, sz) as i128 ;
453- Ok ( b as i32 )
469+ self . to_signed_with_bit_width ( 32 ) . map ( |v| v as i32 )
454470 }
455471
472+ /// Converts the scalar to produce an `i64`. Fails if the scalar is a pointer.
456473 pub fn to_i64 ( self ) -> InterpResult < ' static , i64 > {
457- let sz = Size :: from_bits ( 64 ) ;
458- let b = self . to_bits ( sz) ?;
459- let b = sign_extend ( b, sz) as i128 ;
460- Ok ( b as i64 )
474+ self . to_signed_with_bit_width ( 64 ) . map ( |v| v as i64 )
461475 }
462476
463477 pub fn to_machine_isize ( self , cx : & impl HasDataLayout ) -> InterpResult < ' static , i64 > {
0 commit comments