2 unstable releases
Uses old Rust 2015
0.2.0 | Apr 1, 2023 |
---|---|
0.1.0 | Apr 1, 2023 |
#477 in No standard library
23 downloads per month
7KB
86 lines
unreachable_checked!
A fork of Kixunil/dont_panic that uses unreachable! instead of panic!
Ensures that code can't panic at compile time.
Example
This code will compile and (not) run just fine:
let should_panic = false;
if should_panic {
unreachable_checked!("This will never execute.");
}
However, this code will cause a linking error:
let should_panic = true;
if should_panic {
unreachable_checked!("This will never execute.");
}
Caveats
- This works only when the appropriate opt_level is specified - it may require release build. You can use the
panic
cargo feature to panic instead - The error message is a weird link error. You don't get line number, etc.
- There may be situations in which you know that the code is unreachable but the compiler can't prove it.
lib.rs
:
This crate provides macros that look just like unreachable!()
but instead of panicking,
they cause a linking error if their calls are not optimized-out.
This can be used to ensure the compiler optimizes away some code.
Example
#[macro_use]
extern crate unreachable_checked;
fn main() {
/*
let x = 6 * 9;
if x == 42 {
unreachable_checked!("6 * 9 == 42");
}
*/
let x = false;
if x {
unreachable_checked!("42");
}
}
Compile with --release
or --features=panic