@@ -5,7 +5,21 @@ macro_rules! int {
55 ( $n: expr) => { {
66 const VALUE : $crate:: Int = match $crate:: Int :: new( $n) {
77 Some ( int) => int,
8- None => panic!( "Number is outside the range of an Int" ) ,
8+ None => {
9+ // Hack to emulate a panic in this case.
10+ // Inspired by the [`static_assertions`](https://github.com/nvzqz/static-assertions) package.
11+ // Improvements to be made with higher MSRVs:
12+ // * 1.48: Replace the number comparison with `Option::is_none`
13+ // * 1.57: Use `panic!()` directly.
14+ // * 1.83: Replace manual `panic!()` with `Option::expect`
15+ const _: [ ( ) ; 0 - !{
16+ const ASSERT : bool = $n >= $crate:: MIN_SAFE_INT && $n <= $crate:: MAX_SAFE_INT ;
17+ ASSERT
18+ } as usize ] = [ ] ;
19+ // This loop should not run, but it produces a never type that keeps the match
20+ // arms having the same type (since never conforms to any type)
21+ loop { }
22+ }
923 } ;
1024 VALUE
1125 } } ;
@@ -18,7 +32,22 @@ macro_rules! uint {
1832 ( $n: expr) => { {
1933 const VALUE : $crate:: UInt = match $crate:: UInt :: new( $n) {
2034 Some ( int) => int,
21- None => panic!( "Number is outside the range of an Int" ) ,
35+ None => {
36+ // Hack to emulate a panic in this case.
37+ // Inspired by the [`static_assertions`](https://github.com/nvzqz/static-assertions) package.
38+ // Improvements to be made with higher MSRVs:
39+ // * 1.48: Replace the number comparison with `Option::is_none`
40+ // * 1.57: Use `panic!()` directly.
41+ // * 1.83: Replace manual `panic!()` with `Option::expect`
42+ #[ allow( unknown_lints, unused_comparisons) ]
43+ const _: [ ( ) ; 0 - !{
44+ const ASSERT : bool = $n <= $crate:: MAX_SAFE_UINT ;
45+ ASSERT
46+ } as usize ] = [ ] ;
47+ // This loop should not run, but it produces a never type that keeps the match
48+ // arms having the same type (since never conforms to any type)
49+ loop { }
50+ }
2251 } ;
2352 VALUE
2453 } } ;
0 commit comments