@@ -80,10 +80,9 @@ impl<'de> Deserialize<'de> for bool {
80
80
////////////////////////////////////////////////////////////////////////////////
81
81
82
82
macro_rules! impl_deserialize_num {
83
- ( $primitive: ident, $nonzero: ident $ ( cfg ( $ ( $cfg : tt ) * ) ) * , $deserialize: ident $( $method: ident!( $( $val: ident : $visit: ident) * ) ; ) * ) => {
83
+ ( $primitive: ident, $nonzero: ident, $deserialize: ident $( $method: ident!( $( $val: ident : $visit: ident) * ) ; ) * ) => {
84
84
impl_deserialize_num!( $primitive, $deserialize $( $method!( $( $val : $visit) * ) ; ) * ) ;
85
85
86
- $( #[ cfg( $( $cfg) * ) ] ) *
87
86
impl <' de> Deserialize <' de> for num:: $nonzero {
88
87
fn deserialize<D >( deserializer: D ) -> Result <Self , D :: Error >
89
88
where
@@ -228,12 +227,12 @@ macro_rules! num_as_copysign_self {
228
227
where
229
228
E : Error ,
230
229
{
231
- #[ cfg( any ( no_float_copysign , not( feature = "std" ) ) ) ]
230
+ #[ cfg( not( feature = "std" ) ) ]
232
231
{
233
232
Ok ( v as Self :: Value )
234
233
}
235
234
236
- #[ cfg( all ( not ( no_float_copysign ) , feature = "std" ) ) ]
235
+ #[ cfg( feature = "std" ) ]
237
236
{
238
237
// Preserve sign of NaN. The `as` produces a nondeterministic sign.
239
238
let sign = if v. is_sign_positive( ) { 1.0 } else { -1.0 } ;
@@ -250,13 +249,8 @@ macro_rules! int_to_int {
250
249
where
251
250
E : Error ,
252
251
{
253
- if Self :: Value :: min_value( ) as i64 <= v as i64
254
- && v as i64 <= Self :: Value :: max_value( ) as i64
255
- {
256
- Ok ( v as Self :: Value )
257
- } else {
258
- Err ( Error :: invalid_value( Unexpected :: Signed ( v as i64 ) , & self ) )
259
- }
252
+ Self :: Value :: try_from( v as i64 )
253
+ . map_err( |_| Error :: invalid_value( Unexpected :: Signed ( v as i64 ) , & self ) )
260
254
}
261
255
} ;
262
256
@@ -265,10 +259,8 @@ macro_rules! int_to_int {
265
259
where
266
260
E : Error ,
267
261
{
268
- if $primitive:: min_value( ) as i64 <= v as i64
269
- && v as i64 <= $primitive:: max_value( ) as i64
270
- {
271
- if let Some ( nonzero) = Self :: Value :: new( v as $primitive) {
262
+ if let Ok ( v) = $primitive:: try_from( v as i64 ) {
263
+ if let Some ( nonzero) = Self :: Value :: new( v) {
272
264
return Ok ( nonzero) ;
273
265
}
274
266
}
@@ -299,11 +291,13 @@ macro_rules! int_to_uint {
299
291
where
300
292
E : Error ,
301
293
{
302
- if 0 <= v && v as u64 <= Self :: Value :: max_value( ) as u64 {
303
- Ok ( v as Self :: Value )
304
- } else {
305
- Err ( Error :: invalid_value( Unexpected :: Signed ( v as i64 ) , & self ) )
294
+ if 0 <= v {
295
+ #[ allow( irrefutable_let_patterns) ]
296
+ if let Ok ( v) = Self :: Value :: try_from( v as u64 ) {
297
+ return Ok ( v as Self :: Value ) ;
298
+ }
306
299
}
300
+ Err ( Error :: invalid_value( Unexpected :: Signed ( v as i64 ) , & self ) )
307
301
}
308
302
} ;
309
303
@@ -312,9 +306,12 @@ macro_rules! int_to_uint {
312
306
where
313
307
E : Error ,
314
308
{
315
- if 0 < v && v as u64 <= $primitive:: max_value( ) as u64 {
316
- if let Some ( nonzero) = Self :: Value :: new( v as $primitive) {
317
- return Ok ( nonzero) ;
309
+ if 0 < v {
310
+ #[ allow( irrefutable_let_patterns) ]
311
+ if let Ok ( v) = $primitive:: try_from( v as u64 ) {
312
+ if let Some ( nonzero) = Self :: Value :: new( v) {
313
+ return Ok ( nonzero) ;
314
+ }
318
315
}
319
316
}
320
317
Err ( Error :: invalid_value( Unexpected :: Signed ( v as i64 ) , & self ) )
@@ -344,11 +341,8 @@ macro_rules! uint_to_self {
344
341
where
345
342
E : Error ,
346
343
{
347
- if v as u64 <= Self :: Value :: max_value( ) as u64 {
348
- Ok ( v as Self :: Value )
349
- } else {
350
- Err ( Error :: invalid_value( Unexpected :: Unsigned ( v as u64 ) , & self ) )
351
- }
344
+ Self :: Value :: try_from( v as u64 )
345
+ . map_err( |_| Error :: invalid_value( Unexpected :: Unsigned ( v as u64 ) , & self ) )
352
346
}
353
347
} ;
354
348
@@ -357,8 +351,8 @@ macro_rules! uint_to_self {
357
351
where
358
352
E : Error ,
359
353
{
360
- if v as u64 < = $primitive:: max_value ( ) as u64 {
361
- if let Some ( nonzero) = Self :: Value :: new( v as $primitive ) {
354
+ if let Ok ( v ) = $primitive:: try_from ( v as u64 ) {
355
+ if let Some ( nonzero) = Self :: Value :: new( v) {
362
356
return Ok ( nonzero) ;
363
357
}
364
358
}
@@ -371,7 +365,7 @@ macro_rules! uint_to_self {
371
365
where
372
366
E : Error ,
373
367
{
374
- if v as u64 < = $primitive:: MAX as u64 {
368
+ if let Ok ( v ) = $primitive:: try_from ( v as u64 ) {
375
369
Ok ( Saturating ( v as $primitive) )
376
370
} else {
377
371
Ok ( Saturating ( $primitive:: MAX ) )
@@ -381,37 +375,37 @@ macro_rules! uint_to_self {
381
375
}
382
376
383
377
impl_deserialize_num ! {
384
- i8 , NonZeroI8 cfg ( not ( no_num_nonzero_signed ) ) , deserialize_i8
378
+ i8 , NonZeroI8 , deserialize_i8
385
379
num_self!( i8 : visit_i8) ;
386
380
int_to_int!( i16 : visit_i16 i32 : visit_i32 i64 : visit_i64) ;
387
381
uint_to_self!( u8 : visit_u8 u16 : visit_u16 u32 : visit_u32 u64 : visit_u64) ;
388
382
}
389
383
390
384
impl_deserialize_num ! {
391
- i16 , NonZeroI16 cfg ( not ( no_num_nonzero_signed ) ) , deserialize_i16
385
+ i16 , NonZeroI16 , deserialize_i16
392
386
num_self!( i16 : visit_i16) ;
393
387
num_as_self!( i8 : visit_i8) ;
394
388
int_to_int!( i32 : visit_i32 i64 : visit_i64) ;
395
389
uint_to_self!( u8 : visit_u8 u16 : visit_u16 u32 : visit_u32 u64 : visit_u64) ;
396
390
}
397
391
398
392
impl_deserialize_num ! {
399
- i32 , NonZeroI32 cfg ( not ( no_num_nonzero_signed ) ) , deserialize_i32
393
+ i32 , NonZeroI32 , deserialize_i32
400
394
num_self!( i32 : visit_i32) ;
401
395
num_as_self!( i8 : visit_i8 i16 : visit_i16) ;
402
396
int_to_int!( i64 : visit_i64) ;
403
397
uint_to_self!( u8 : visit_u8 u16 : visit_u16 u32 : visit_u32 u64 : visit_u64) ;
404
398
}
405
399
406
400
impl_deserialize_num ! {
407
- i64 , NonZeroI64 cfg ( not ( no_num_nonzero_signed ) ) , deserialize_i64
401
+ i64 , NonZeroI64 , deserialize_i64
408
402
num_self!( i64 : visit_i64) ;
409
403
num_as_self!( i8 : visit_i8 i16 : visit_i16 i32 : visit_i32) ;
410
404
uint_to_self!( u8 : visit_u8 u16 : visit_u16 u32 : visit_u32 u64 : visit_u64) ;
411
405
}
412
406
413
407
impl_deserialize_num ! {
414
- isize , NonZeroIsize cfg ( not ( no_num_nonzero_signed ) ) , deserialize_i64
408
+ isize , NonZeroIsize , deserialize_i64
415
409
num_as_self!( i8 : visit_i8 i16 : visit_i16) ;
416
410
int_to_int!( i32 : visit_i32 i64 : visit_i64) ;
417
411
uint_to_self!( u8 : visit_u8 u16 : visit_u16 u32 : visit_u32 u64 : visit_u64) ;
@@ -476,9 +470,7 @@ macro_rules! num_128 {
476
470
where
477
471
E : Error ,
478
472
{
479
- if v as i128 >= Self :: Value :: min_value( ) as i128
480
- && v as u128 <= Self :: Value :: max_value( ) as u128
481
- {
473
+ if v as i128 >= Self :: Value :: MIN as i128 && v as u128 <= Self :: Value :: MAX as u128 {
482
474
Ok ( v as Self :: Value )
483
475
} else {
484
476
Err ( Error :: invalid_value(
@@ -494,9 +486,7 @@ macro_rules! num_128 {
494
486
where
495
487
E : Error ,
496
488
{
497
- if v as i128 >= $primitive:: min_value( ) as i128
498
- && v as u128 <= $primitive:: max_value( ) as u128
499
- {
489
+ if v as i128 >= $primitive:: MIN as i128 && v as u128 <= $primitive:: MAX as u128 {
500
490
if let Some ( nonzero) = Self :: Value :: new( v as $primitive) {
501
491
Ok ( nonzero)
502
492
} else {
@@ -528,7 +518,7 @@ macro_rules! num_128 {
528
518
}
529
519
530
520
impl_deserialize_num ! {
531
- i128 , NonZeroI128 cfg ( not ( no_num_nonzero_signed ) ) , deserialize_i128
521
+ i128 , NonZeroI128 , deserialize_i128
532
522
num_self!( i128 : visit_i128) ;
533
523
num_as_self!( i8 : visit_i8 i16 : visit_i16 i32 : visit_i32 i64 : visit_i64) ;
534
524
num_as_self!( u8 : visit_u8 u16 : visit_u16 u32 : visit_u32 u64 : visit_u64) ;
@@ -2415,13 +2405,9 @@ impl<'de> Deserialize<'de> for SystemTime {
2415
2405
2416
2406
const FIELDS : & [ & str ] = & [ "secs_since_epoch" , "nanos_since_epoch" ] ;
2417
2407
let duration = tri ! ( deserializer. deserialize_struct( "SystemTime" , FIELDS , DurationVisitor ) ) ;
2418
- #[ cfg( not( no_systemtime_checked_add) ) ]
2419
- let ret = UNIX_EPOCH
2408
+ UNIX_EPOCH
2420
2409
. checked_add ( duration)
2421
- . ok_or_else ( || D :: Error :: custom ( "overflow deserializing SystemTime" ) ) ;
2422
- #[ cfg( no_systemtime_checked_add) ]
2423
- let ret = Ok ( UNIX_EPOCH + duration) ;
2424
- ret
2410
+ . ok_or_else ( || D :: Error :: custom ( "overflow deserializing SystemTime" ) )
2425
2411
}
2426
2412
}
2427
2413
0 commit comments