@@ -415,61 +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 )
422427 }
423428
429+ /// Converts the scalar to produce an `u16`. Fails if the scalar is a pointer.
424430 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 )
428432 }
429433
434+ /// Converts the scalar to produce an `u32`. Fails if the scalar is a pointer.
430435 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 )
434437 }
435438
439+ /// Converts the scalar to produce an `u64`. Fails if the scalar is a pointer.
436440 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 )
440442 }
441443
442444 pub fn to_machine_usize ( self , cx : & impl HasDataLayout ) -> InterpResult < ' static , u64 > {
443445 let b = self . to_bits ( cx. data_layout ( ) . pointer_size ) ?;
444446 Ok ( b as u64 )
445447 }
446448
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) ;
449452 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 )
452454 }
453455
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.
454462 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 )
459464 }
460465
466+ /// Converts the scalar to produce an `i32`. Fails if the scalar is a pointer.
461467 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 )
466469 }
467470
471+ /// Converts the scalar to produce an `i64`. Fails if the scalar is a pointer.
468472 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 )
473474 }
474475
475476 pub fn to_machine_isize ( self , cx : & impl HasDataLayout ) -> InterpResult < ' static , i64 > {
0 commit comments