@@ -249,11 +249,8 @@ macro_rules! int_to_int {
249
249
where
250
250
E : Error ,
251
251
{
252
- if Self :: Value :: MIN as i64 <= v as i64 && v as i64 <= Self :: Value :: MAX as i64 {
253
- Ok ( v as Self :: Value )
254
- } else {
255
- Err ( Error :: invalid_value( Unexpected :: Signed ( v as i64 ) , & self ) )
256
- }
252
+ Self :: Value :: try_from( v as i64 )
253
+ . map_err( |_| Error :: invalid_value( Unexpected :: Signed ( v as i64 ) , & self ) )
257
254
}
258
255
} ;
259
256
@@ -262,8 +259,8 @@ macro_rules! int_to_int {
262
259
where
263
260
E : Error ,
264
261
{
265
- if $primitive :: MIN as i64 <= v as i64 && v as i64 < = $primitive:: MAX as i64 {
266
- 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) {
267
264
return Ok ( nonzero) ;
268
265
}
269
266
}
@@ -294,11 +291,13 @@ macro_rules! int_to_uint {
294
291
where
295
292
E : Error ,
296
293
{
297
- if 0 <= v && v as u64 <= Self :: Value :: MAX as u64 {
298
- Ok ( v as Self :: Value )
299
- } else {
300
- 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
+ }
301
299
}
300
+ Err ( Error :: invalid_value( Unexpected :: Signed ( v as i64 ) , & self ) )
302
301
}
303
302
} ;
304
303
@@ -307,9 +306,12 @@ macro_rules! int_to_uint {
307
306
where
308
307
E : Error ,
309
308
{
310
- if 0 < v && v as u64 <= $primitive:: MAX as u64 {
311
- if let Some ( nonzero) = Self :: Value :: new( v as $primitive) {
312
- 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
+ }
313
315
}
314
316
}
315
317
Err ( Error :: invalid_value( Unexpected :: Signed ( v as i64 ) , & self ) )
@@ -339,11 +341,8 @@ macro_rules! uint_to_self {
339
341
where
340
342
E : Error ,
341
343
{
342
- if v as u64 <= Self :: Value :: MAX as u64 {
343
- Ok ( v as Self :: Value )
344
- } else {
345
- Err ( Error :: invalid_value( Unexpected :: Unsigned ( v as u64 ) , & self ) )
346
- }
344
+ Self :: Value :: try_from( v as u64 )
345
+ . map_err( |_| Error :: invalid_value( Unexpected :: Unsigned ( v as u64 ) , & self ) )
347
346
}
348
347
} ;
349
348
@@ -352,8 +351,8 @@ macro_rules! uint_to_self {
352
351
where
353
352
E : Error ,
354
353
{
355
- if v as u64 < = $primitive:: MAX as u64 {
356
- 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) {
357
356
return Ok ( nonzero) ;
358
357
}
359
358
}
@@ -366,7 +365,7 @@ macro_rules! uint_to_self {
366
365
where
367
366
E : Error ,
368
367
{
369
- if v as u64 < = $primitive:: MAX as u64 {
368
+ if let Ok ( v ) = $primitive:: try_from ( v as u64 ) {
370
369
Ok ( Saturating ( v as $primitive) )
371
370
} else {
372
371
Ok ( Saturating ( $primitive:: MAX ) )
0 commit comments