time/hint.rs
1//! Hints to the compiler that affects how code should be emitted or optimized.
2
3#![allow(dead_code)] // may be used in the future and has minimal overhead
4
5/// Indicate that a given branch is **not** likely to be taken, relatively speaking.
6#[inline(always)]
7#[cold]
8pub(crate) const fn cold_path() {}
9
10/// Indicate that a given condition is likely to be true.
11#[inline(always)]
12pub(crate) const fn likely(b: bool) -> bool {
13 if !b {
14 cold_path();
15 }
16 b
17}
18
19/// Indicate that a given condition is likely to be false.
20#[inline(always)]
21pub(crate) const fn unlikely(b: bool) -> bool {
22 if b {
23 cold_path();
24 }
25 b
26}
27
28/// Indicate that the provided boolean condition is always true and may be relied upon for safety.
29#[track_caller]
30#[inline(always)]
31pub(crate) const unsafe fn assert_unchecked(b: bool) {
32 debug_assert!(b);
33 if !b {
34 // Safety: The caller has asserted that this condition is always true.
35 unsafe { core::hint::unreachable_unchecked() }
36 }
37}