@@ -249,11 +249,6 @@ impl ScalarInt {
249
249
}
250
250
}
251
251
252
- #[ inline]
253
- pub fn try_to_target_usize ( & self , tcx : TyCtxt < ' _ > ) -> Result < u64 , Size > {
254
- Ok ( self . to_bits ( tcx. data_layout . pointer_size ) ? as u64 )
255
- }
256
-
257
252
/// Tries to convert the `ScalarInt` to an unsigned integer of the given size.
258
253
/// Fails if the size of the `ScalarInt` is not equal to `size` and returns the
259
254
/// `ScalarInt`s size in that case.
@@ -262,56 +257,61 @@ impl ScalarInt {
262
257
self . to_bits ( size)
263
258
}
264
259
265
- // Tries to convert the `ScalarInt` to `bool`. Fails if the `size` of the `ScalarInt`
266
- // in not equal to `Size { raw: 1 }` or if the value is not 0 or 1 and returns the `size`
267
- // value of the `ScalarInt` in that case.
268
- #[ inline]
269
- pub fn try_to_bool ( self ) -> Result < bool , Size > {
270
- match self . try_to_u8 ( ) ? {
271
- 0 => Ok ( false ) ,
272
- 1 => Ok ( true ) ,
273
- _ => Err ( self . size ( ) ) ,
274
- }
275
- }
276
-
277
260
// Tries to convert the `ScalarInt` to `u8`. Fails if the `size` of the `ScalarInt`
278
261
// in not equal to `Size { raw: 1 }` and returns the `size` value of the `ScalarInt` in
279
262
// that case.
280
263
#[ inline]
281
264
pub fn try_to_u8 ( self ) -> Result < u8 , Size > {
282
- self . to_bits ( Size :: from_bits ( 8 ) ) . map ( |v| u8:: try_from ( v) . unwrap ( ) )
265
+ self . try_to_uint ( Size :: from_bits ( 8 ) ) . map ( |v| u8:: try_from ( v) . unwrap ( ) )
283
266
}
284
267
285
268
/// Tries to convert the `ScalarInt` to `u16`. Fails if the size of the `ScalarInt`
286
269
/// in not equal to `Size { raw: 2 }` and returns the `size` value of the `ScalarInt` in
287
270
/// that case.
288
271
#[ inline]
289
272
pub fn try_to_u16 ( self ) -> Result < u16 , Size > {
290
- self . to_bits ( Size :: from_bits ( 16 ) ) . map ( |v| u16:: try_from ( v) . unwrap ( ) )
273
+ self . try_to_uint ( Size :: from_bits ( 16 ) ) . map ( |v| u16:: try_from ( v) . unwrap ( ) )
291
274
}
292
275
293
276
/// Tries to convert the `ScalarInt` to `u32`. Fails if the `size` of the `ScalarInt`
294
277
/// in not equal to `Size { raw: 4 }` and returns the `size` value of the `ScalarInt` in
295
278
/// that case.
296
279
#[ inline]
297
280
pub fn try_to_u32 ( self ) -> Result < u32 , Size > {
298
- self . to_bits ( Size :: from_bits ( 32 ) ) . map ( |v| u32:: try_from ( v) . unwrap ( ) )
281
+ self . try_to_uint ( Size :: from_bits ( 32 ) ) . map ( |v| u32:: try_from ( v) . unwrap ( ) )
299
282
}
300
283
301
284
/// Tries to convert the `ScalarInt` to `u64`. Fails if the `size` of the `ScalarInt`
302
285
/// in not equal to `Size { raw: 8 }` and returns the `size` value of the `ScalarInt` in
303
286
/// that case.
304
287
#[ inline]
305
288
pub fn try_to_u64 ( self ) -> Result < u64 , Size > {
306
- self . to_bits ( Size :: from_bits ( 64 ) ) . map ( |v| u64:: try_from ( v) . unwrap ( ) )
289
+ self . try_to_uint ( Size :: from_bits ( 64 ) ) . map ( |v| u64:: try_from ( v) . unwrap ( ) )
307
290
}
308
291
309
292
/// Tries to convert the `ScalarInt` to `u128`. Fails if the `size` of the `ScalarInt`
310
293
/// in not equal to `Size { raw: 16 }` and returns the `size` value of the `ScalarInt` in
311
294
/// that case.
312
295
#[ inline]
313
296
pub fn try_to_u128 ( self ) -> Result < u128 , Size > {
314
- self . to_bits ( Size :: from_bits ( 128 ) )
297
+ self . try_to_uint ( Size :: from_bits ( 128 ) )
298
+ }
299
+
300
+ #[ inline]
301
+ pub fn try_to_target_usize ( & self , tcx : TyCtxt < ' _ > ) -> Result < u64 , Size > {
302
+ self . try_to_uint ( tcx. data_layout . pointer_size ) . map ( |v| u64:: try_from ( v) . unwrap ( ) )
303
+ }
304
+
305
+ // Tries to convert the `ScalarInt` to `bool`. Fails if the `size` of the `ScalarInt`
306
+ // in not equal to `Size { raw: 1 }` or if the value is not 0 or 1 and returns the `size`
307
+ // value of the `ScalarInt` in that case.
308
+ #[ inline]
309
+ pub fn try_to_bool ( self ) -> Result < bool , Size > {
310
+ match self . try_to_u8 ( ) ? {
311
+ 0 => Ok ( false ) ,
312
+ 1 => Ok ( true ) ,
313
+ _ => Err ( self . size ( ) ) ,
314
+ }
315
315
}
316
316
317
317
/// Tries to convert the `ScalarInt` to a signed integer of the given size.
@@ -357,6 +357,27 @@ impl ScalarInt {
357
357
pub fn try_to_i128 ( self ) -> Result < i128 , Size > {
358
358
self . try_to_int ( Size :: from_bits ( 128 ) )
359
359
}
360
+
361
+ #[ inline]
362
+ pub fn try_to_target_isize ( & self , tcx : TyCtxt < ' _ > ) -> Result < i64 , Size > {
363
+ self . try_to_int ( tcx. data_layout . pointer_size ) . map ( |v| i64:: try_from ( v) . unwrap ( ) )
364
+ }
365
+
366
+ #[ inline]
367
+ pub fn try_to_float < F : Float > ( self ) -> Result < F , Size > {
368
+ // Going through `to_uint` to check size and truncation.
369
+ Ok ( F :: from_bits ( self . to_bits ( Size :: from_bits ( F :: BITS ) ) ?) )
370
+ }
371
+
372
+ #[ inline]
373
+ pub fn try_to_f32 ( self ) -> Result < Single , Size > {
374
+ self . try_to_float ( )
375
+ }
376
+
377
+ #[ inline]
378
+ pub fn try_to_f64 ( self ) -> Result < Double , Size > {
379
+ self . try_to_float ( )
380
+ }
360
381
}
361
382
362
383
macro_rules! from {
@@ -399,11 +420,7 @@ impl TryFrom<ScalarInt> for bool {
399
420
type Error = Size ;
400
421
#[ inline]
401
422
fn try_from ( int : ScalarInt ) -> Result < Self , Size > {
402
- int. to_bits ( Size :: from_bytes ( 1 ) ) . and_then ( |u| match u {
403
- 0 => Ok ( false ) ,
404
- 1 => Ok ( true ) ,
405
- _ => Err ( Size :: from_bytes ( 1 ) ) ,
406
- } )
423
+ int. try_to_bool ( )
407
424
}
408
425
}
409
426
0 commit comments