assert_unchecked

Macro unreachable_unchecked

Source
macro_rules! unreachable_unchecked {
    ($($arg:tt)*) => { ... };
}
Expand description

Equivalent to the unreachable! macro in builds with debug_assertions on, and otherwise calls core::hint::unreachable_unchecked.

§Safety

In release mode, reaching this function is completely undefined behavior (UB). For more details, see the documentation for unreachable_unchecked.

Use this function only when you can prove that the code will never call it. Otherwise, consider using the unreachable! macro, which does not allow optimizations but will panic when executed.

§Example

use assert_unchecked::unreachable_unchecked;
fn div_1(a: u32, b: u32) -> u32 {
    // `b.saturating_add(1)` is always positive (not zero),
    // hence `checked_div` will never return `None`.
    // Therefore, the else branch is unreachable.
    a.checked_div(b.saturating_add(1))
        .unwrap_or_else(|| unsafe { unreachable_unchecked!("division by zero isn't possible") })
}

assert_eq!(div_1(7, 0), 7);
assert_eq!(div_1(9, 1), 4);
assert_eq!(div_1(11, u32::MAX), 0);