debug_unreachable/
lib.rs

1#![no_std]
2
3//! `panic!()` in debug builds, optimization hint in release.
4
5#[doc(hidden)]
6pub mod _internal {
7    pub use core::hint::unreachable_unchecked;
8}
9
10#[macro_export]
11/// `panic!()` in debug builds, optimization hint in release.
12///
13/// This is equivalent to [`core::unreachable`] in debug builds,
14/// and [`core::hint::unreachable_unchecked`] in release builds.
15///
16/// Example:
17///
18/// ```
19/// use debug_unreachable::debug_unreachable;
20///
21/// if 0 > 100 {
22///     // Can't happen!
23///     unsafe { debug_unreachable!() }
24/// } else {
25///     println!("Good, 0 <= 100.");
26/// }
27/// ```
28macro_rules! debug_unreachable {
29    () => {
30        debug_unreachable!("entered unreachable code")
31    };
32    ($e:expr) => {
33        if cfg!(debug_assertions) {
34            unreachable!($e)
35        } else {
36            $crate::_internal::unreachable_unchecked()
37        }
38    };
39}