Skip to content

Instantly share code, notes, and snippets.

@a4lg
Created March 9, 2023 05:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a4lg/ced533724a120d5fe6008c29fd773737 to your computer and use it in GitHub Desktop.
Save a4lg/ced533724a120d5fe6008c29fd773737 to your computer and use it in GitHub Desktop.
`invariant!` macro to declare an invariant
// 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