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