unreachable_checked

Crates.iounreachable_checked
lib.rsunreachable_checked
version0.2.0
sourcesrc
created_at2023-04-01 21:57:47.33237
updated_at2023-04-01 22:01:34.951594
descriptionunreachable!()-like macro that causes linking error instead of panicking. May be used to statically ensure some code won't panic.
homepagehttps://github.com/DrSloth/unreachable_checked
repositoryhttps://github.com/DrSloth/unreachable_checked
max_upload_size
id827642
size7,913
Hassan Abu-Jabir (DrSloth)

documentation

README

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.
Commit count: 17

cargo fmt