@@ -370,78 +370,82 @@ impl<'tcx, Tag: Provenance> Scalar<Tag> {
370370 }
371371 }
372372
373+ /// Converts the scalar to produce an unsigned integer of the given size.
374+ /// Fails if the scalar is a pointer.
373375 #[ inline]
374- fn to_unsigned_with_bit_width ( self , bits : u64 ) -> InterpResult < ' static , u128 > {
375- let sz = Size :: from_bits ( bits) ;
376- self . to_bits ( sz)
376+ pub fn to_uint ( self , size : Size ) -> InterpResult < ' static , u128 > {
377+ self . to_bits ( size)
377378 }
378379
379380 /// Converts the scalar to produce a `u8`. Fails if the scalar is a pointer.
380381 pub fn to_u8 ( self ) -> InterpResult < ' static , u8 > {
381- self . to_unsigned_with_bit_width ( 8 ) . map ( |v| u8:: try_from ( v) . unwrap ( ) )
382+ self . to_uint ( Size :: from_bits ( 8 ) ) . map ( |v| u8:: try_from ( v) . unwrap ( ) )
382383 }
383384
384385 /// Converts the scalar to produce a `u16`. Fails if the scalar is a pointer.
385386 pub fn to_u16 ( self ) -> InterpResult < ' static , u16 > {
386- self . to_unsigned_with_bit_width ( 16 ) . map ( |v| u16:: try_from ( v) . unwrap ( ) )
387+ self . to_uint ( Size :: from_bits ( 16 ) ) . map ( |v| u16:: try_from ( v) . unwrap ( ) )
387388 }
388389
389390 /// Converts the scalar to produce a `u32`. Fails if the scalar is a pointer.
390391 pub fn to_u32 ( self ) -> InterpResult < ' static , u32 > {
391- self . to_unsigned_with_bit_width ( 32 ) . map ( |v| u32:: try_from ( v) . unwrap ( ) )
392+ self . to_uint ( Size :: from_bits ( 32 ) ) . map ( |v| u32:: try_from ( v) . unwrap ( ) )
392393 }
393394
394395 /// Converts the scalar to produce a `u64`. Fails if the scalar is a pointer.
395396 pub fn to_u64 ( self ) -> InterpResult < ' static , u64 > {
396- self . to_unsigned_with_bit_width ( 64 ) . map ( |v| u64:: try_from ( v) . unwrap ( ) )
397+ self . to_uint ( Size :: from_bits ( 64 ) ) . map ( |v| u64:: try_from ( v) . unwrap ( ) )
397398 }
398399
399400 /// Converts the scalar to produce a `u128`. Fails if the scalar is a pointer.
400401 pub fn to_u128 ( self ) -> InterpResult < ' static , u128 > {
401- self . to_unsigned_with_bit_width ( 128 )
402+ self . to_uint ( Size :: from_bits ( 128 ) )
402403 }
403404
405+ /// Converts the scalar to produce a machine-pointer-sized unsigned integer.
406+ /// Fails if the scalar is a pointer.
404407 pub fn to_machine_usize ( self , cx : & impl HasDataLayout ) -> InterpResult < ' static , u64 > {
405- let b = self . to_bits ( cx. data_layout ( ) . pointer_size ) ?;
408+ let b = self . to_uint ( cx. data_layout ( ) . pointer_size ) ?;
406409 Ok ( u64:: try_from ( b) . unwrap ( ) )
407410 }
408411
412+ /// Converts the scalar to produce a signed integer of the given size.
413+ /// Fails if the scalar is a pointer.
409414 #[ inline]
410- fn to_signed_with_bit_width ( self , bits : u64 ) -> InterpResult < ' static , i128 > {
411- let sz = Size :: from_bits ( bits) ;
412- let b = self . to_bits ( sz) ?;
413- Ok ( sz. sign_extend ( b) as i128 )
415+ pub fn to_int ( self , size : Size ) -> InterpResult < ' static , i128 > {
416+ let b = self . to_bits ( size) ?;
417+ Ok ( size. sign_extend ( b) as i128 )
414418 }
415419
416420 /// Converts the scalar to produce an `i8`. Fails if the scalar is a pointer.
417421 pub fn to_i8 ( self ) -> InterpResult < ' static , i8 > {
418- self . to_signed_with_bit_width ( 8 ) . map ( |v| i8:: try_from ( v) . unwrap ( ) )
422+ self . to_int ( Size :: from_bits ( 8 ) ) . map ( |v| i8:: try_from ( v) . unwrap ( ) )
419423 }
420424
421425 /// Converts the scalar to produce an `i16`. Fails if the scalar is a pointer.
422426 pub fn to_i16 ( self ) -> InterpResult < ' static , i16 > {
423- self . to_signed_with_bit_width ( 16 ) . map ( |v| i16:: try_from ( v) . unwrap ( ) )
427+ self . to_int ( Size :: from_bits ( 16 ) ) . map ( |v| i16:: try_from ( v) . unwrap ( ) )
424428 }
425429
426430 /// Converts the scalar to produce an `i32`. Fails if the scalar is a pointer.
427431 pub fn to_i32 ( self ) -> InterpResult < ' static , i32 > {
428- self . to_signed_with_bit_width ( 32 ) . map ( |v| i32:: try_from ( v) . unwrap ( ) )
432+ self . to_int ( Size :: from_bits ( 32 ) ) . map ( |v| i32:: try_from ( v) . unwrap ( ) )
429433 }
430434
431435 /// Converts the scalar to produce an `i64`. Fails if the scalar is a pointer.
432436 pub fn to_i64 ( self ) -> InterpResult < ' static , i64 > {
433- self . to_signed_with_bit_width ( 64 ) . map ( |v| i64:: try_from ( v) . unwrap ( ) )
437+ self . to_int ( Size :: from_bits ( 64 ) ) . map ( |v| i64:: try_from ( v) . unwrap ( ) )
434438 }
435439
436440 /// Converts the scalar to produce an `i128`. Fails if the scalar is a pointer.
437441 pub fn to_i128 ( self ) -> InterpResult < ' static , i128 > {
438- self . to_signed_with_bit_width ( 128 )
442+ self . to_int ( Size :: from_bits ( 128 ) )
439443 }
440444
445+ /// Converts the scalar to produce a machine-pointer-sized signed integer.
446+ /// Fails if the scalar is a pointer.
441447 pub fn to_machine_isize ( self , cx : & impl HasDataLayout ) -> InterpResult < ' static , i64 > {
442- let sz = cx. data_layout ( ) . pointer_size ;
443- let b = self . to_bits ( sz) ?;
444- let b = sz. sign_extend ( b) as i128 ;
448+ let b = self . to_int ( cx. data_layout ( ) . pointer_size ) ?;
445449 Ok ( i64:: try_from ( b) . unwrap ( ) )
446450 }
447451
0 commit comments