#[inline(always)]
#[track_caller]
#[allow(unused_variables, reason = "It contains #[cfg(debug_assertions)]")]
pub fn assert_hint(cond: bool, debug_msg: &str) {
if cfg!(debug_assertions) {
assert!(cond, "{debug_msg}");
} else {
unsafe { core::hint::assert_unchecked(cond) };
}
}
#[inline(always)]
#[track_caller]
#[allow(unused_variables, reason = "It contains #[cfg(debug_assertions)]")]
pub fn unreachable_hint() -> ! {
if cfg!(debug_assertions) {
unreachable!();
} else {
unsafe { core::hint::unreachable_unchecked() }
}
}
#[inline(always)]
#[cold]
pub const fn cold_path() {}
#[inline(always)]
pub const fn likely(b: bool) -> bool {
if !b {
cold_path();
}
b
}
#[inline(always)]
pub const fn unlikely(b: bool) -> bool {
if b {
cold_path();
}
b
}
pub trait UnwrapOrPanic<T> {
fn unwrap_or_panic(self, message: &'static str) -> T;
unsafe fn unwrap_unchecked(self) -> T;
}
impl<T> UnwrapOrPanic<T> for Option<T> {
#[inline(always)]
fn unwrap_or_panic(self, message: &'static str) -> T {
self.unwrap_or_else(|| panic!("{message}"))
}
#[inline(always)]
unsafe fn unwrap_unchecked(self) -> T {
unsafe { self.unwrap_unchecked() }
}
}
impl<T, E> UnwrapOrPanic<T> for Result<T, E> {
#[inline(always)]
fn unwrap_or_panic(self, message: &'static str) -> T {
self.unwrap_or_else(|_| panic!("{message}"))
}
#[inline(always)]
unsafe fn unwrap_unchecked(self) -> T {
unsafe { self.unwrap_unchecked() }
}
}
#[track_caller]
pub fn unwrap_or_bug_hint<T>(item: impl UnwrapOrPanic<T>) -> T {
if cfg!(debug_assertions) {
item.unwrap_or_panic("`unwrap_or_bug_hint` has been failed. It is a bug.")
} else {
unsafe { item.unwrap_unchecked() }
}
}
#[allow(unused_variables, reason = "It contains #[cfg(debug_assertions)]")]
#[track_caller]
pub fn unwrap_or_bug_message_hint<T>(item: impl UnwrapOrPanic<T>, message: &'static str) -> T {
if cfg!(debug_assertions) {
item.unwrap_or_panic(message)
} else {
unsafe { item.unwrap_unchecked() }
}
}