@@ -379,78 +379,82 @@ impl<'tcx, Tag: Provenance> Scalar<Tag> {
379379 }
380380 }
381381
382+ /// Converts the scalar to produce an unsigned integer of the given size.
383+ /// Fails if the scalar is a pointer.
382384 #[ inline]
383- fn to_unsigned_with_bit_width ( self , bits : u64 ) -> InterpResult < ' static , u128 > {
384- let sz = Size :: from_bits ( bits) ;
385- self . to_bits ( sz)
385+ pub fn to_uint ( self , size : Size ) -> InterpResult < ' static , u128 > {
386+ self . to_bits ( size)
386387 }
387388
388389 /// Converts the scalar to produce a `u8`. Fails if the scalar is a pointer.
389390 pub fn to_u8 ( self ) -> InterpResult < ' static , u8 > {
390- self . to_unsigned_with_bit_width ( 8 ) . map ( |v| u8:: try_from ( v) . unwrap ( ) )
391+ self . to_uint ( Size :: from_bits ( 8 ) ) . map ( |v| u8:: try_from ( v) . unwrap ( ) )
391392 }
392393
393394 /// Converts the scalar to produce a `u16`. Fails if the scalar is a pointer.
394395 pub fn to_u16 ( self ) -> InterpResult < ' static , u16 > {
395- self . to_unsigned_with_bit_width ( 16 ) . map ( |v| u16:: try_from ( v) . unwrap ( ) )
396+ self . to_uint ( Size :: from_bits ( 16 ) ) . map ( |v| u16:: try_from ( v) . unwrap ( ) )
396397 }
397398
398399 /// Converts the scalar to produce a `u32`. Fails if the scalar is a pointer.
399400 pub fn to_u32 ( self ) -> InterpResult < ' static , u32 > {
400- self . to_unsigned_with_bit_width ( 32 ) . map ( |v| u32:: try_from ( v) . unwrap ( ) )
401+ self . to_uint ( Size :: from_bits ( 32 ) ) . map ( |v| u32:: try_from ( v) . unwrap ( ) )
401402 }
402403
403404 /// Converts the scalar to produce a `u64`. Fails if the scalar is a pointer.
404405 pub fn to_u64 ( self ) -> InterpResult < ' static , u64 > {
405- self . to_unsigned_with_bit_width ( 64 ) . map ( |v| u64:: try_from ( v) . unwrap ( ) )
406+ self . to_uint ( Size :: from_bits ( 64 ) ) . map ( |v| u64:: try_from ( v) . unwrap ( ) )
406407 }
407408
408409 /// Converts the scalar to produce a `u128`. Fails if the scalar is a pointer.
409410 pub fn to_u128 ( self ) -> InterpResult < ' static , u128 > {
410- self . to_unsigned_with_bit_width ( 128 )
411+ self . to_uint ( Size :: from_bits ( 128 ) )
411412 }
412413
414+ /// Converts the scalar to produce a machine-pointer-sized unsigned integer.
415+ /// Fails if the scalar is a pointer.
413416 pub fn to_machine_usize ( self , cx : & impl HasDataLayout ) -> InterpResult < ' static , u64 > {
414- let b = self . to_bits ( cx. data_layout ( ) . pointer_size ) ?;
417+ let b = self . to_uint ( cx. data_layout ( ) . pointer_size ) ?;
415418 Ok ( u64:: try_from ( b) . unwrap ( ) )
416419 }
417420
421+ /// Converts the scalar to produce a signed integer of the given size.
422+ /// Fails if the scalar is a pointer.
418423 #[ inline]
419- fn to_signed_with_bit_width ( self , bits : u64 ) -> InterpResult < ' static , i128 > {
420- let sz = Size :: from_bits ( bits) ;
421- let b = self . to_bits ( sz) ?;
422- Ok ( sz. sign_extend ( b) as i128 )
424+ pub fn to_int ( self , size : Size ) -> InterpResult < ' static , i128 > {
425+ let b = self . to_bits ( size) ?;
426+ Ok ( size. sign_extend ( b) as i128 )
423427 }
424428
425429 /// Converts the scalar to produce an `i8`. Fails if the scalar is a pointer.
426430 pub fn to_i8 ( self ) -> InterpResult < ' static , i8 > {
427- self . to_signed_with_bit_width ( 8 ) . map ( |v| i8:: try_from ( v) . unwrap ( ) )
431+ self . to_int ( Size :: from_bits ( 8 ) ) . map ( |v| i8:: try_from ( v) . unwrap ( ) )
428432 }
429433
430434 /// Converts the scalar to produce an `i16`. Fails if the scalar is a pointer.
431435 pub fn to_i16 ( self ) -> InterpResult < ' static , i16 > {
432- self . to_signed_with_bit_width ( 16 ) . map ( |v| i16:: try_from ( v) . unwrap ( ) )
436+ self . to_int ( Size :: from_bits ( 16 ) ) . map ( |v| i16:: try_from ( v) . unwrap ( ) )
433437 }
434438
435439 /// Converts the scalar to produce an `i32`. Fails if the scalar is a pointer.
436440 pub fn to_i32 ( self ) -> InterpResult < ' static , i32 > {
437- self . to_signed_with_bit_width ( 32 ) . map ( |v| i32:: try_from ( v) . unwrap ( ) )
441+ self . to_int ( Size :: from_bits ( 32 ) ) . map ( |v| i32:: try_from ( v) . unwrap ( ) )
438442 }
439443
440444 /// Converts the scalar to produce an `i64`. Fails if the scalar is a pointer.
441445 pub fn to_i64 ( self ) -> InterpResult < ' static , i64 > {
442- self . to_signed_with_bit_width ( 64 ) . map ( |v| i64:: try_from ( v) . unwrap ( ) )
446+ self . to_int ( Size :: from_bits ( 64 ) ) . map ( |v| i64:: try_from ( v) . unwrap ( ) )
443447 }
444448
445449 /// Converts the scalar to produce an `i128`. Fails if the scalar is a pointer.
446450 pub fn to_i128 ( self ) -> InterpResult < ' static , i128 > {
447- self . to_signed_with_bit_width ( 128 )
451+ self . to_int ( Size :: from_bits ( 128 ) )
448452 }
449453
454+ /// Converts the scalar to produce a machine-pointer-sized signed integer.
455+ /// Fails if the scalar is a pointer.
450456 pub fn to_machine_isize ( self , cx : & impl HasDataLayout ) -> InterpResult < ' static , i64 > {
451- let sz = cx. data_layout ( ) . pointer_size ;
452- let b = self . to_bits ( sz) ?;
453- let b = sz. sign_extend ( b) as i128 ;
457+ let b = self . to_int ( cx. data_layout ( ) . pointer_size ) ?;
454458 Ok ( i64:: try_from ( b) . unwrap ( ) )
455459 }
456460
0 commit comments