Observe the referenced data and prevent the compiler from removing previous writes to it. This function acts like [`core::hint::black_box`] but takes a reference and does not return the passed value. It's implemented using the [`core::arch::asm!`] macro on target arches where `asm!` is stable, i.e. `aarch64`, `arm`, `arm64ec`, `loongarch64`, `riscv32`, `riscv64`, `s390x`, `x86`, and `x86_64`. O
(val: &T)
| 50 | /// Note that erasure of `ThirdPartyType` demonstrated above can be fragile if it contains |
| 51 | /// `MaybeUninit` or `union` data. It also does not perform erasure of types like `Box` or `Vec`. |
| 52 | pub fn optimization_barrier<T: ?Sized>(val: &T) { |
| 53 | #[cfg(all( |
| 54 | not(miri), |
| 55 | any( |
| 56 | target_arch = "aarch64", |
| 57 | target_arch = "arm", |
| 58 | target_arch = "arm64ec", |
| 59 | target_arch = "loongarch64", |
| 60 | target_arch = "riscv32", |
| 61 | target_arch = "riscv64", |
| 62 | target_arch = "s390x", |
| 63 | target_arch = "x86", |
| 64 | target_arch = "x86_64", |
| 65 | ) |
| 66 | ))] |
| 67 | unsafe { |
| 68 | core::arch::asm!( |
| 69 | "# {}", |
| 70 | in(reg) core::ptr::from_ref::<T>(val).cast::<()>(), |
| 71 | options(readonly, preserves_flags, nostack), |
| 72 | ); |
| 73 | } |
| 74 | #[cfg(not(all( |
| 75 | not(miri), |
| 76 | any( |
| 77 | target_arch = "aarch64", |
| 78 | target_arch = "arm", |
| 79 | target_arch = "arm64ec", |
| 80 | target_arch = "loongarch64", |
| 81 | target_arch = "riscv32", |
| 82 | target_arch = "riscv64", |
| 83 | target_arch = "s390x", |
| 84 | target_arch = "x86", |
| 85 | target_arch = "x86_64", |
| 86 | ) |
| 87 | )))] |
| 88 | core::hint::black_box(val); |
| 89 | } |
no outgoing calls