alloy/core/
hint.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//! Hints that can be used to guide the compiler's optimization decisions.

/// A marker intrinsic that tells the compiler to optimize the case where the
/// given boolean is likely to be true.
///
/// # Parameters
///
/// * `b` - The boolean to check
#[inline(always)]
pub const fn likely(b: bool) -> bool {
  if !b {
    unlikely_branch()
  }
  b
}

/// A marker intrinsic that tells the compiler to optimize the case where the
/// given boolean is unlikely to be true.
///
/// # Parameters
///
/// * `b` - The boolean to check
#[inline(always)]
pub const fn unlikely(b: bool) -> bool {
  if b {
    unlikely_branch()
  }
  b
}

/// A marker intrinsic that tells the compiler that the branch this intrinsic
/// is placed on is unlikely to be taken.
#[inline(always)]
#[cold]
pub const fn unlikely_branch() {}

/// A marker intrinsic that tells the compiler that the slice will have a
/// fixed size of `N`.
///
/// # Parameters
///
/// * `slice` - The slice to check.
/// * `length` - The expected length of the slice.
///
/// # Safety
///
/// The size of the slice must be equal to `N`.
pub const unsafe fn fixed_size<T>(slice: &[T], length: usize) {
  if slice.len() != length {
    unsafe { std::hint::unreachable_unchecked() }
  }
}

/// A marker intrinsic that tells the compiler that the given condition is
/// assumed to be true.
///
/// # Parameters
///
/// * `cond` - The condition to assume.
///
/// # Safety
///
/// The condition being assumed must always hold true, otherwise this will lead
/// to undefined behavior.
pub const unsafe fn assume(cond: bool) {
  if !cond {
    unsafe { std::hint::unreachable_unchecked() }
  }
}