Created
March 9, 2023 05:30
-
-
Save a4lg/ced533724a120d5fe6008c29fd773737 to your computer and use it in GitHub Desktop.
`invariant!` macro to declare an invariant
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// If `unsafe` and `nightly` are enabled, enable unstable `core_intrinsics` feature | |
// with #![feature(core_intrinsics)] (Nightly only). | |
macro_rules! invariant { | |
($expr: expr) => { | |
cfg_if::cfg_if! { | |
if #[cfg(all(feature = "unsafe", feature = "nightly"))] { | |
core::intrinsics::assume($expr); | |
} | |
else if #[cfg(feature = "unsafe")] { | |
if !($expr) { | |
core::hint::unreachable_unchecked(); | |
} | |
} | |
else { | |
debug_assert!($expr); | |
} | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment