@@ -47,7 +47,7 @@ fn cmp_u64_to_f64(a: u64, b: f64) -> Ordering {
47
47
if can_represent_as_f64 ( a) {
48
48
// If we can represent as an f64, we can just cast and compare
49
49
( a as f64 ) . partial_cmp ( & b) . unwrap ( )
50
- } else if b <= ( 0x20000000000000u64 as f64 ) {
50
+ } else if b <= ( 0x20000000000000_u64 as f64 ) {
51
51
// If the floating point number is less than all non-representable
52
52
// integers, and our integer is non-representable, then we know
53
53
// the integer is greater.
@@ -64,7 +64,7 @@ fn cmp_u64_to_f64(a: u64, b: f64) -> Ordering {
64
64
65
65
impl Header {
66
66
fn as_i24_unchecked ( & self ) -> i32 {
67
- ( ( self . static_ as i32 ) << 8 ) | ( self . short as i32 )
67
+ ( i32 :: from ( self . static_ ) << 8 ) | i32 :: from ( self . short )
68
68
}
69
69
unsafe fn as_i64_unchecked ( & self ) -> & i64 {
70
70
& * ( self as * const _ as * const i64 ) . add ( 1 )
@@ -88,12 +88,12 @@ impl Header {
88
88
// Safety: We only call methods appropriate for the type
89
89
unsafe {
90
90
match self . type_ {
91
- NumberType :: Static => Some ( self . static_ as i64 ) ,
92
- NumberType :: I24 => Some ( self . as_i24_unchecked ( ) as i64 ) ,
91
+ NumberType :: Static => Some ( i64 :: from ( self . static_ ) ) ,
92
+ NumberType :: I24 => Some ( i64 :: from ( self . as_i24_unchecked ( ) ) ) ,
93
93
NumberType :: I64 => Some ( * self . as_i64_unchecked ( ) ) ,
94
94
NumberType :: U64 => {
95
95
let v = * self . as_u64_unchecked ( ) ;
96
- if v <= i64:: MAX as u64 {
96
+ if i64:: try_from ( v ) . is_ok ( ) {
97
97
Some ( v as i64 )
98
98
} else {
99
99
None
@@ -153,8 +153,8 @@ impl Header {
153
153
// Safety: We only call methods appropriate for the type
154
154
unsafe {
155
155
match self . type_ {
156
- NumberType :: Static => Some ( self . static_ as f64 ) ,
157
- NumberType :: I24 => Some ( self . as_i24_unchecked ( ) as f64 ) ,
156
+ NumberType :: Static => Some ( f64 :: from ( self . static_ ) ) ,
157
+ NumberType :: I24 => Some ( f64 :: from ( self . as_i24_unchecked ( ) ) ) ,
158
158
NumberType :: I64 => {
159
159
let v = * self . as_i64_unchecked ( ) ;
160
160
let can_represent = if v < 0 {
@@ -184,7 +184,7 @@ impl Header {
184
184
// Safety: We only call methods appropriate for the type
185
185
unsafe {
186
186
match self . type_ {
187
- NumberType :: Static => Some ( self . static_ as f32 ) ,
187
+ NumberType :: Static => Some ( f32 :: from ( self . static_ ) ) ,
188
188
NumberType :: I24 => Some ( self . as_i24_unchecked ( ) as f32 ) ,
189
189
NumberType :: I64 => {
190
190
let v = * self . as_i64_unchecked ( ) ;
@@ -210,7 +210,7 @@ impl Header {
210
210
NumberType :: F64 => {
211
211
let v = * self . as_f64_unchecked ( ) ;
212
212
let u = v as f32 ;
213
- if v == ( u as f64 ) {
213
+ if v == f64 :: from ( u ) {
214
214
Some ( u)
215
215
} else {
216
216
None
@@ -228,8 +228,8 @@ impl Header {
228
228
fn to_f64_lossy ( & self ) -> f64 {
229
229
unsafe {
230
230
match self . type_ {
231
- NumberType :: Static => self . static_ as f64 ,
232
- NumberType :: I24 => self . as_i24_unchecked ( ) as f64 ,
231
+ NumberType :: Static => f64 :: from ( self . static_ ) ,
232
+ NumberType :: I24 => f64 :: from ( self . as_i24_unchecked ( ) ) ,
233
233
NumberType :: I64 => * self . as_i64_unchecked ( ) as f64 ,
234
234
NumberType :: U64 => * self . as_u64_unchecked ( ) as f64 ,
235
235
NumberType :: F64 => * self . as_f64_unchecked ( ) ,
@@ -361,7 +361,7 @@ impl INumber {
361
361
362
362
fn alloc ( type_ : NumberType ) -> * mut Header {
363
363
unsafe {
364
- let ptr = alloc ( Self :: layout ( type_) . unwrap ( ) ) as * mut Header ;
364
+ let ptr = alloc ( Self :: layout ( type_) . unwrap ( ) ) . cast :: < Header > ( ) ;
365
365
( * ptr) . type_ = type_;
366
366
( * ptr) . static_ = 0 ;
367
367
( * ptr) . short = 0 ;
@@ -372,16 +372,18 @@ impl INumber {
372
372
fn dealloc ( ptr : * mut Header ) {
373
373
unsafe {
374
374
let layout = Self :: layout ( ( * ptr) . type_ ) . unwrap ( ) ;
375
- dealloc ( ptr as * mut u8 , layout) ;
375
+ dealloc ( ptr. cast :: < u8 > ( ) , layout) ;
376
376
}
377
377
}
378
378
379
379
/// Returns the number zero (without a decimal point). Does not allocate.
380
+ #[ must_use]
380
381
pub fn zero ( ) -> Self {
381
382
// Safety: 0 is in the static range
382
383
unsafe { Self :: new_static ( 0 ) }
383
384
}
384
385
/// Returns the number one (without a decimal point). Does not allocate.
386
+ #[ must_use]
385
387
pub fn one ( ) -> Self {
386
388
// Safety: 1 is in the static range
387
389
unsafe { Self :: new_static ( 1 ) }
@@ -396,7 +398,7 @@ impl INumber {
396
398
fn new_ptr ( type_ : NumberType ) -> Self {
397
399
unsafe {
398
400
INumber ( IValue :: new_ptr (
399
- Self :: alloc ( type_) as * mut u8 ,
401
+ Self :: alloc ( type_) . cast :: < u8 > ( ) ,
400
402
TypeTag :: Number ,
401
403
) )
402
404
}
@@ -415,7 +417,7 @@ impl INumber {
415
417
416
418
// Value must fit in an i24
417
419
fn new_short ( value : i32 ) -> Self {
418
- if value >= STATIC_LOWER as i32 && value < STATIC_UPPER as i32 {
420
+ if value >= i32 :: from ( STATIC_LOWER ) && value < i32:: from ( STATIC_UPPER ) {
419
421
// Safety: We checked the value is in the static range
420
422
unsafe { Self :: new_static ( value as i16 ) }
421
423
} else {
@@ -443,7 +445,7 @@ impl INumber {
443
445
}
444
446
445
447
fn new_u64 ( value : u64 ) -> Self {
446
- if value <= i64:: MAX as u64 {
448
+ if i64:: try_from ( value ) . is_ok ( ) {
447
449
Self :: new_i64 ( value as i64 )
448
450
} else {
449
451
let mut res = Self :: new_ptr ( NumberType :: U64 ) ;
@@ -487,48 +489,59 @@ impl INumber {
487
489
}
488
490
489
491
/// Converts this number to an i64 if it can be represented exactly.
492
+ #[ must_use]
490
493
pub fn to_i64 ( & self ) -> Option < i64 > {
491
494
self . header ( ) . to_i64 ( )
492
495
}
493
496
/// Converts this number to an f64 if it can be represented exactly.
497
+ #[ must_use]
494
498
pub fn to_u64 ( & self ) -> Option < u64 > {
495
499
self . header ( ) . to_u64 ( )
496
500
}
497
501
/// Converts this number to an f64 if it can be represented exactly.
502
+ #[ must_use]
498
503
pub fn to_f64 ( & self ) -> Option < f64 > {
499
504
self . header ( ) . to_f64 ( )
500
505
}
501
506
/// Converts this number to an f32 if it can be represented exactly.
507
+ #[ must_use]
502
508
pub fn to_f32 ( & self ) -> Option < f32 > {
503
509
self . header ( ) . to_f32 ( )
504
510
}
505
511
/// Converts this number to an i32 if it can be represented exactly.
512
+ #[ must_use]
506
513
pub fn to_i32 ( & self ) -> Option < i32 > {
507
514
self . header ( ) . to_i64 ( ) . and_then ( |x| x. try_into ( ) . ok ( ) )
508
515
}
509
516
/// Converts this number to a u32 if it can be represented exactly.
517
+ #[ must_use]
510
518
pub fn to_u32 ( & self ) -> Option < u32 > {
511
519
self . header ( ) . to_u64 ( ) . and_then ( |x| x. try_into ( ) . ok ( ) )
512
520
}
513
521
/// Converts this number to an isize if it can be represented exactly.
522
+ #[ must_use]
514
523
pub fn to_isize ( & self ) -> Option < isize > {
515
524
self . header ( ) . to_i64 ( ) . and_then ( |x| x. try_into ( ) . ok ( ) )
516
525
}
517
526
/// Converts this number to a usize if it can be represented exactly.
527
+ #[ must_use]
518
528
pub fn to_usize ( & self ) -> Option < usize > {
519
529
self . header ( ) . to_u64 ( ) . and_then ( |x| x. try_into ( ) . ok ( ) )
520
530
}
521
531
/// Converts this number to an f64, potentially losing precision in the process.
532
+ #[ must_use]
522
533
pub fn to_f64_lossy ( & self ) -> f64 {
523
534
self . header ( ) . to_f64_lossy ( )
524
535
}
525
536
/// Converts this number to an f32, potentially losing precision in the process.
537
+ #[ must_use]
526
538
pub fn to_f32_lossy ( & self ) -> f32 {
527
539
self . to_f64_lossy ( ) as f32
528
540
}
529
541
530
542
/// This allows distinguishing between `1.0` and `1` in the original JSON.
531
543
/// Numeric operations will otherwise treat these two values as equivalent.
544
+ #[ must_use]
532
545
pub fn has_decimal_point ( & self ) -> bool {
533
546
self . header ( ) . has_decimal_point ( )
534
547
}
@@ -559,18 +572,18 @@ impl From<u64> for INumber {
559
572
}
560
573
impl From < u32 > for INumber {
561
574
fn from ( v : u32 ) -> Self {
562
- Self :: new_u64 ( v as u64 )
575
+ Self :: new_u64 ( u64:: from ( v ) )
563
576
}
564
577
}
565
578
impl From < u16 > for INumber {
566
579
fn from ( v : u16 ) -> Self {
567
- Self :: new_short ( v as i32 )
580
+ Self :: new_short ( i32:: from ( v ) )
568
581
}
569
582
}
570
583
impl From < u8 > for INumber {
571
584
fn from ( v : u8 ) -> Self {
572
585
// Safety: All u8s are in the static range
573
- unsafe { Self :: new_static ( v as i16 ) }
586
+ unsafe { Self :: new_static ( i16:: from ( v ) ) }
574
587
}
575
588
}
576
589
impl From < usize > for INumber {
@@ -586,18 +599,18 @@ impl From<i64> for INumber {
586
599
}
587
600
impl From < i32 > for INumber {
588
601
fn from ( v : i32 ) -> Self {
589
- Self :: new_i64 ( v as i64 )
602
+ Self :: new_i64 ( i64:: from ( v ) )
590
603
}
591
604
}
592
605
impl From < i16 > for INumber {
593
606
fn from ( v : i16 ) -> Self {
594
- Self :: new_short ( v as i32 )
607
+ Self :: new_short ( i32:: from ( v ) )
595
608
}
596
609
}
597
610
impl From < i8 > for INumber {
598
611
fn from ( v : i8 ) -> Self {
599
612
// Safety: All i8s are in the static range
600
- unsafe { Self :: new_static ( v as i16 ) }
613
+ unsafe { Self :: new_static ( i16:: from ( v ) ) }
601
614
}
602
615
}
603
616
impl From < isize > for INumber {
@@ -621,7 +634,7 @@ impl TryFrom<f32> for INumber {
621
634
type Error = ( ) ;
622
635
fn try_from ( v : f32 ) -> Result < Self , ( ) > {
623
636
if v. is_finite ( ) {
624
- Ok ( Self :: new_f64 ( v as f64 ) )
637
+ Ok ( Self :: new_f64 ( f64:: from ( v ) ) )
625
638
} else {
626
639
Err ( ( ) )
627
640
}
@@ -702,12 +715,12 @@ mod tests {
702
715
let x: INumber = 0x1000000 . into ( ) ;
703
716
assert_eq ! ( x. to_i64( ) , Some ( 0x1000000 ) ) ;
704
717
assert_eq ! ( x. to_u64( ) , Some ( 0x1000000 ) ) ;
705
- assert_eq ! ( x. to_f64( ) , Some ( 16777216 .0) ) ;
718
+ assert_eq ! ( x. to_f64( ) , Some ( 16_777_216 .0) ) ;
706
719
707
720
let x: INumber = i64:: MIN . into ( ) ;
708
721
assert_eq ! ( x. to_i64( ) , Some ( i64 :: MIN ) ) ;
709
722
assert_eq ! ( x. to_u64( ) , None ) ;
710
- assert_eq ! ( x. to_f64( ) , Some ( -9223372036854775808 .0) ) ;
723
+ assert_eq ! ( x. to_f64( ) , Some ( -9_223_372_036_854_775_808 .0) ) ;
711
724
712
725
let x: INumber = i64:: MAX . into ( ) ;
713
726
assert_eq ! ( x. to_i64( ) , Some ( i64 :: MAX ) ) ;
@@ -720,9 +733,9 @@ mod tests {
720
733
assert_eq ! ( x. to_f64( ) , None ) ;
721
734
722
735
let x: INumber = 13369629 . into ( ) ;
723
- assert_eq ! ( x. to_i64( ) , Some ( 13369629 ) ) ;
724
- assert_eq ! ( x. to_u64( ) , Some ( 13369629 ) ) ;
725
- assert_eq ! ( x. to_f64( ) , Some ( 13369629 .0) ) ;
736
+ assert_eq ! ( x. to_i64( ) , Some ( 13_369_629 ) ) ;
737
+ assert_eq ! ( x. to_u64( ) , Some ( 13_369_629 ) ) ;
738
+ assert_eq ! ( x. to_f64( ) , Some ( 13_369_629 .0) ) ;
726
739
727
740
let x: INumber = 0x800000 . into ( ) ;
728
741
assert_eq ! ( x. to_i64( ) , Some ( 0x800000 ) ) ;
@@ -753,6 +766,6 @@ mod tests {
753
766
assert ! ( INumber :: try_from( 1e30 ) . unwrap( ) > INumber :: from( i64 :: MAX ) ) ;
754
767
assert ! ( INumber :: try_from( -1e30 ) . unwrap( ) < INumber :: from( i64 :: MIN ) ) ;
755
768
assert ! ( INumber :: try_from( -1e30 ) . unwrap( ) < INumber :: from( i64 :: MIN ) ) ;
756
- assert ! ( INumber :: try_from( 99999999000 .0) . unwrap( ) < INumber :: from( 99999999001u64 ) ) ;
769
+ assert ! ( INumber :: try_from( 99_999_999_000 .0) . unwrap( ) < INumber :: from( 99_999_999_001_u64 ) ) ;
757
770
}
758
771
}
0 commit comments