@@ -247,14 +247,7 @@ impl ScalarInt {
247
247
}
248
248
249
249
#[ inline]
250
- pub fn assert_bits ( self , target_size : Size ) -> u128 {
251
- self . to_bits ( target_size) . unwrap_or_else ( |size| {
252
- bug ! ( "expected int of size {}, but got size {}" , target_size. bytes( ) , size. bytes( ) )
253
- } )
254
- }
255
-
256
- #[ inline]
257
- pub fn to_bits ( self , target_size : Size ) -> Result < u128 , Size > {
250
+ pub fn try_to_bits ( self , target_size : Size ) -> Result < u128 , Size > {
258
251
assert_ne ! ( target_size. bytes( ) , 0 , "you should never look at the bits of a ZST" ) ;
259
252
if target_size. bytes ( ) == u64:: from ( self . size . get ( ) ) {
260
253
self . check_data ( ) ;
@@ -264,48 +257,60 @@ impl ScalarInt {
264
257
}
265
258
}
266
259
260
+ #[ inline]
261
+ pub fn assert_bits ( self , target_size : Size ) -> u128 {
262
+ self . try_to_bits ( target_size) . unwrap_or_else ( |size| {
263
+ bug ! ( "expected int of size {}, but got size {}" , target_size. bytes( ) , size. bytes( ) )
264
+ } )
265
+ }
266
+
267
267
/// Tries to convert the `ScalarInt` to an unsigned integer of the given size.
268
268
/// Fails if the size of the `ScalarInt` is not equal to `size` and returns the
269
269
/// `ScalarInt`s size in that case.
270
270
#[ inline]
271
271
pub fn try_to_uint ( self , size : Size ) -> Result < u128 , Size > {
272
- self . to_bits ( size)
272
+ self . try_to_bits ( size)
273
+ }
274
+
275
+ #[ inline]
276
+ pub fn assert_uint ( self , size : Size ) -> u128 {
277
+ self . assert_bits ( size)
273
278
}
274
279
275
280
// Tries to convert the `ScalarInt` to `u8`. Fails if the `size` of the `ScalarInt`
276
- // in not equal to `Size { raw: 1 }` and returns the `size` value of the `ScalarInt` in
281
+ // in not equal to 1 byte and returns the `size` value of the `ScalarInt` in
277
282
// that case.
278
283
#[ inline]
279
284
pub fn try_to_u8 ( self ) -> Result < u8 , Size > {
280
285
self . try_to_uint ( Size :: from_bits ( 8 ) ) . map ( |v| u8:: try_from ( v) . unwrap ( ) )
281
286
}
282
287
283
288
/// Tries to convert the `ScalarInt` to `u16`. Fails if the size of the `ScalarInt`
284
- /// in not equal to `Size { raw: 2 }` and returns the `size` value of the `ScalarInt` in
289
+ /// in not equal to 2 bytes and returns the `size` value of the `ScalarInt` in
285
290
/// that case.
286
291
#[ inline]
287
292
pub fn try_to_u16 ( self ) -> Result < u16 , Size > {
288
293
self . try_to_uint ( Size :: from_bits ( 16 ) ) . map ( |v| u16:: try_from ( v) . unwrap ( ) )
289
294
}
290
295
291
296
/// Tries to convert the `ScalarInt` to `u32`. Fails if the `size` of the `ScalarInt`
292
- /// in not equal to `Size { raw: 4 }` and returns the `size` value of the `ScalarInt` in
297
+ /// in not equal to 4 bytes and returns the `size` value of the `ScalarInt` in
293
298
/// that case.
294
299
#[ inline]
295
300
pub fn try_to_u32 ( self ) -> Result < u32 , Size > {
296
301
self . try_to_uint ( Size :: from_bits ( 32 ) ) . map ( |v| u32:: try_from ( v) . unwrap ( ) )
297
302
}
298
303
299
304
/// Tries to convert the `ScalarInt` to `u64`. Fails if the `size` of the `ScalarInt`
300
- /// in not equal to `Size { raw: 8 }` and returns the `size` value of the `ScalarInt` in
305
+ /// in not equal to 8 bytes and returns the `size` value of the `ScalarInt` in
301
306
/// that case.
302
307
#[ inline]
303
308
pub fn try_to_u64 ( self ) -> Result < u64 , Size > {
304
309
self . try_to_uint ( Size :: from_bits ( 64 ) ) . map ( |v| u64:: try_from ( v) . unwrap ( ) )
305
310
}
306
311
307
312
/// Tries to convert the `ScalarInt` to `u128`. Fails if the `size` of the `ScalarInt`
308
- /// in not equal to `Size { raw: 16 }` and returns the `size` value of the `ScalarInt` in
313
+ /// in not equal to 16 bytes and returns the `size` value of the `ScalarInt` in
309
314
/// that case.
310
315
#[ inline]
311
316
pub fn try_to_u128 ( self ) -> Result < u128 , Size > {
@@ -318,7 +323,7 @@ impl ScalarInt {
318
323
}
319
324
320
325
// Tries to convert the `ScalarInt` to `bool`. Fails if the `size` of the `ScalarInt`
321
- // in not equal to `Size { raw: 1 }` or if the value is not 0 or 1 and returns the `size`
326
+ // in not equal to 1 byte or if the value is not 0 or 1 and returns the `size`
322
327
// value of the `ScalarInt` in that case.
323
328
#[ inline]
324
329
pub fn try_to_bool ( self ) -> Result < bool , Size > {
@@ -334,40 +339,46 @@ impl ScalarInt {
334
339
/// `ScalarInt`s size in that case.
335
340
#[ inline]
336
341
pub fn try_to_int ( self , size : Size ) -> Result < i128 , Size > {
337
- let b = self . to_bits ( size) ?;
342
+ let b = self . try_to_bits ( size) ?;
338
343
Ok ( size. sign_extend ( b) as i128 )
339
344
}
340
345
346
+ #[ inline]
347
+ pub fn assert_int ( self , size : Size ) -> i128 {
348
+ let b = self . assert_bits ( size) ;
349
+ size. sign_extend ( b) as i128
350
+ }
351
+
341
352
/// Tries to convert the `ScalarInt` to i8.
342
- /// Fails if the size of the `ScalarInt` is not equal to `Size { raw: 1 }`
353
+ /// Fails if the size of the `ScalarInt` is not equal to 1 byte
343
354
/// and returns the `ScalarInt`s size in that case.
344
355
pub fn try_to_i8 ( self ) -> Result < i8 , Size > {
345
356
self . try_to_int ( Size :: from_bits ( 8 ) ) . map ( |v| i8:: try_from ( v) . unwrap ( ) )
346
357
}
347
358
348
359
/// Tries to convert the `ScalarInt` to i16.
349
- /// Fails if the size of the `ScalarInt` is not equal to `Size { raw: 2 }`
360
+ /// Fails if the size of the `ScalarInt` is not equal to 2 bytes
350
361
/// and returns the `ScalarInt`s size in that case.
351
362
pub fn try_to_i16 ( self ) -> Result < i16 , Size > {
352
363
self . try_to_int ( Size :: from_bits ( 16 ) ) . map ( |v| i16:: try_from ( v) . unwrap ( ) )
353
364
}
354
365
355
366
/// Tries to convert the `ScalarInt` to i32.
356
- /// Fails if the size of the `ScalarInt` is not equal to `Size { raw: 4 }`
367
+ /// Fails if the size of the `ScalarInt` is not equal to 4 bytes
357
368
/// and returns the `ScalarInt`s size in that case.
358
369
pub fn try_to_i32 ( self ) -> Result < i32 , Size > {
359
370
self . try_to_int ( Size :: from_bits ( 32 ) ) . map ( |v| i32:: try_from ( v) . unwrap ( ) )
360
371
}
361
372
362
373
/// Tries to convert the `ScalarInt` to i64.
363
- /// Fails if the size of the `ScalarInt` is not equal to `Size { raw: 8 }`
374
+ /// Fails if the size of the `ScalarInt` is not equal to 8 bytes
364
375
/// and returns the `ScalarInt`s size in that case.
365
376
pub fn try_to_i64 ( self ) -> Result < i64 , Size > {
366
377
self . try_to_int ( Size :: from_bits ( 64 ) ) . map ( |v| i64:: try_from ( v) . unwrap ( ) )
367
378
}
368
379
369
380
/// Tries to convert the `ScalarInt` to i128.
370
- /// Fails if the size of the `ScalarInt` is not equal to `Size { raw: 16 }`
381
+ /// Fails if the size of the `ScalarInt` is not equal to 16 bytes
371
382
/// and returns the `ScalarInt`s size in that case.
372
383
pub fn try_to_i128 ( self ) -> Result < i128 , Size > {
373
384
self . try_to_int ( Size :: from_bits ( 128 ) )
@@ -381,7 +392,7 @@ impl ScalarInt {
381
392
#[ inline]
382
393
pub fn try_to_float < F : Float > ( self ) -> Result < F , Size > {
383
394
// Going through `to_uint` to check size and truncation.
384
- Ok ( F :: from_bits ( self . to_bits ( Size :: from_bits ( F :: BITS ) ) ?) )
395
+ Ok ( F :: from_bits ( self . try_to_bits ( Size :: from_bits ( F :: BITS ) ) ?) )
385
396
}
386
397
387
398
#[ inline]
@@ -430,7 +441,7 @@ macro_rules! try_from {
430
441
fn try_from( int: ScalarInt ) -> Result <Self , Size > {
431
442
// The `unwrap` cannot fail because to_bits (if it succeeds)
432
443
// is guaranteed to return a value that fits into the size.
433
- int. to_bits ( Size :: from_bytes( std:: mem:: size_of:: <$ty>( ) ) )
444
+ int. try_to_bits ( Size :: from_bytes( std:: mem:: size_of:: <$ty>( ) ) )
434
445
. map( |u| u. try_into( ) . unwrap( ) )
435
446
}
436
447
}
@@ -465,7 +476,7 @@ impl TryFrom<ScalarInt> for char {
465
476
466
477
#[ inline]
467
478
fn try_from ( int : ScalarInt ) -> Result < Self , Self :: Error > {
468
- let Ok ( bits) = int. to_bits ( Size :: from_bytes ( std:: mem:: size_of :: < char > ( ) ) ) else {
479
+ let Ok ( bits) = int. try_to_bits ( Size :: from_bytes ( std:: mem:: size_of :: < char > ( ) ) ) else {
469
480
return Err ( CharTryFromScalarInt ) ;
470
481
} ;
471
482
match char:: from_u32 ( bits. try_into ( ) . unwrap ( ) ) {
@@ -487,7 +498,7 @@ impl TryFrom<ScalarInt> for Half {
487
498
type Error = Size ;
488
499
#[ inline]
489
500
fn try_from ( int : ScalarInt ) -> Result < Self , Size > {
490
- int. to_bits ( Size :: from_bytes ( 2 ) ) . map ( Self :: from_bits)
501
+ int. try_to_bits ( Size :: from_bytes ( 2 ) ) . map ( Self :: from_bits)
491
502
}
492
503
}
493
504
@@ -503,7 +514,7 @@ impl TryFrom<ScalarInt> for Single {
503
514
type Error = Size ;
504
515
#[ inline]
505
516
fn try_from ( int : ScalarInt ) -> Result < Self , Size > {
506
- int. to_bits ( Size :: from_bytes ( 4 ) ) . map ( Self :: from_bits)
517
+ int. try_to_bits ( Size :: from_bytes ( 4 ) ) . map ( Self :: from_bits)
507
518
}
508
519
}
509
520
@@ -519,7 +530,7 @@ impl TryFrom<ScalarInt> for Double {
519
530
type Error = Size ;
520
531
#[ inline]
521
532
fn try_from ( int : ScalarInt ) -> Result < Self , Size > {
522
- int. to_bits ( Size :: from_bytes ( 8 ) ) . map ( Self :: from_bits)
533
+ int. try_to_bits ( Size :: from_bytes ( 8 ) ) . map ( Self :: from_bits)
523
534
}
524
535
}
525
536
@@ -535,7 +546,7 @@ impl TryFrom<ScalarInt> for Quad {
535
546
type Error = Size ;
536
547
#[ inline]
537
548
fn try_from ( int : ScalarInt ) -> Result < Self , Size > {
538
- int. to_bits ( Size :: from_bytes ( 16 ) ) . map ( Self :: from_bits)
549
+ int. try_to_bits ( Size :: from_bytes ( 16 ) ) . map ( Self :: from_bits)
539
550
}
540
551
}
541
552
0 commit comments