@@ -1050,44 +1050,19 @@ pub unsafe fn set_error_print_panic_hook(new_stderr: RawFd) {
1050
1050
} ) ) ;
1051
1051
}
1052
1052
1053
- // Credit goes to https://github.com/thomcc/nonzero_lit
1054
- // We don't want add another dependency and just want to use usize macro of it.
1055
- #[ doc( hidden) ]
1056
- pub mod _private {
1057
- pub use core:: num:: NonZeroUsize ;
1058
-
1059
- macro_rules! define_nz_ctor {
1060
- ( $( pub fn $nz_func: ident( $n: ident : $int: ident) -> $NonZeroInt: ident; ) +) => { $(
1061
- #[ inline]
1062
- #[ must_use]
1063
- pub const fn $nz_func( $n : $int) -> $NonZeroInt {
1064
- // Note: Hacky const fn assert.
1065
- let _ = [ "N must not be zero" ] [ ( $n == 0 ) as usize ] ;
1066
-
1067
- match $NonZeroInt:: new( $n) {
1068
- Some ( x) => x,
1069
- // The assert above makes this branch unreachable
1070
- None => unreachable!( ) ,
1071
- }
1072
- }
1073
- ) +} ;
1074
- }
1075
-
1076
- define_nz_ctor ! {
1077
- pub fn nz_usize( n: usize ) -> NonZeroUsize ;
1078
- }
1079
- }
1080
-
1081
- /// 0 cost way to create check nonzero on compilation.
1053
+ /// Zero-cost way to construct [`core::num::NonZeroUsize`] at compile-time.
1082
1054
#[ macro_export]
1083
1055
macro_rules! nonzero {
1084
- ( $val: expr $( , ) ?) => { {
1085
- const __E: usize = $val;
1086
- {
1087
- const NZ : $crate:: _private:: NonZeroUsize = $crate:: _private:: nz_usize( __E) ;
1088
- NZ
1056
+ // TODO: Further simplify with `unwrap`/`expect` once MSRV includes
1057
+ // https://github.com/rust-lang/rust/issues/67441
1058
+ ( $val: expr) => {
1059
+ const {
1060
+ match core:: num:: NonZero :: new( $val) {
1061
+ Some ( x) => x,
1062
+ None => panic!( "Value passed to `nonzero!` was zero" ) ,
1063
+ }
1089
1064
}
1090
- } } ;
1065
+ } ;
1091
1066
}
1092
1067
1093
1068
#[ cfg( feature = "python" ) ]
0 commit comments