@@ -379,78 +379,82 @@ impl<'tcx, Tag: Provenance> Scalar<Tag> {
379
379
}
380
380
}
381
381
382
+ /// Converts the scalar to produce an unsigned integer of the given size.
383
+ /// Fails if the scalar is a pointer.
382
384
#[ 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)
386
387
}
387
388
388
389
/// Converts the scalar to produce a `u8`. Fails if the scalar is a pointer.
389
390
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 ( ) )
391
392
}
392
393
393
394
/// Converts the scalar to produce a `u16`. Fails if the scalar is a pointer.
394
395
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 ( ) )
396
397
}
397
398
398
399
/// Converts the scalar to produce a `u32`. Fails if the scalar is a pointer.
399
400
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 ( ) )
401
402
}
402
403
403
404
/// Converts the scalar to produce a `u64`. Fails if the scalar is a pointer.
404
405
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 ( ) )
406
407
}
407
408
408
409
/// Converts the scalar to produce a `u128`. Fails if the scalar is a pointer.
409
410
pub fn to_u128 ( self ) -> InterpResult < ' static , u128 > {
410
- self . to_unsigned_with_bit_width ( 128 )
411
+ self . to_uint ( Size :: from_bits ( 128 ) )
411
412
}
412
413
414
+ /// Converts the scalar to produce a machine-pointer-sized unsigned integer.
415
+ /// Fails if the scalar is a pointer.
413
416
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 ) ?;
415
418
Ok ( u64:: try_from ( b) . unwrap ( ) )
416
419
}
417
420
421
+ /// Converts the scalar to produce a signed integer of the given size.
422
+ /// Fails if the scalar is a pointer.
418
423
#[ 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 )
423
427
}
424
428
425
429
/// Converts the scalar to produce an `i8`. Fails if the scalar is a pointer.
426
430
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 ( ) )
428
432
}
429
433
430
434
/// Converts the scalar to produce an `i16`. Fails if the scalar is a pointer.
431
435
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 ( ) )
433
437
}
434
438
435
439
/// Converts the scalar to produce an `i32`. Fails if the scalar is a pointer.
436
440
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 ( ) )
438
442
}
439
443
440
444
/// Converts the scalar to produce an `i64`. Fails if the scalar is a pointer.
441
445
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 ( ) )
443
447
}
444
448
445
449
/// Converts the scalar to produce an `i128`. Fails if the scalar is a pointer.
446
450
pub fn to_i128 ( self ) -> InterpResult < ' static , i128 > {
447
- self . to_signed_with_bit_width ( 128 )
451
+ self . to_int ( Size :: from_bits ( 128 ) )
448
452
}
449
453
454
+ /// Converts the scalar to produce a machine-pointer-sized signed integer.
455
+ /// Fails if the scalar is a pointer.
450
456
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 ) ?;
454
458
Ok ( i64:: try_from ( b) . unwrap ( ) )
455
459
}
456
460
0 commit comments