Skip to content

Commit dfd5609

Browse files
bolts: Simplify definition of nonzero! macro (#2624)
* bolts: Simplify definition of `nonzero!` macro * Non-Usize NonZero --------- Co-authored-by: Dominik Maier <domenukk@gmail.com>
1 parent d96d833 commit dfd5609

File tree

1 file changed

+10
-35
lines changed

1 file changed

+10
-35
lines changed

libafl_bolts/src/lib.rs

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,44 +1050,19 @@ pub unsafe fn set_error_print_panic_hook(new_stderr: RawFd) {
10501050
}));
10511051
}
10521052

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.
10821054
#[macro_export]
10831055
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+
}
10891064
}
1090-
}};
1065+
};
10911066
}
10921067

10931068
#[cfg(feature = "python")]

0 commit comments

Comments
 (0)