@@ -415,48 +415,62 @@ impl<'tcx, Tag> Scalar<Tag> {
415415 }
416416 }
417417
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.
418425 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 )
422432 }
423433
434+ /// Converts the scalar to produce an `u32`. Fails if the scalar is a pointer.
424435 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 )
428437 }
429438
439+ /// Converts the scalar to produce an `u64`. Fails if the scalar is a pointer.
430440 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 )
434442 }
435443
436444 pub fn to_machine_usize ( self , cx : & impl HasDataLayout ) -> InterpResult < ' static , u64 > {
437445 let b = self . to_bits ( cx. data_layout ( ) . pointer_size ) ?;
438446 Ok ( b as u64 )
439447 }
440448
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) ;
443452 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 )
446464 }
447465
466+ /// Converts the scalar to produce an `i32`. Fails if the scalar is a pointer.
448467 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 )
453469 }
454470
471+ /// Converts the scalar to produce an `i64`. Fails if the scalar is a pointer.
455472 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 )
460474 }
461475
462476 pub fn to_machine_isize ( self , cx : & impl HasDataLayout ) -> InterpResult < ' static , i64 > {
0 commit comments