Skip to main content

debug_assert_aligned

Function debug_assert_aligned 

Source
pub fn debug_assert_aligned(ptr: *const u8, align: usize)
Expand description

Debug-only pointer alignment assertion that is safe to export.

Why this style:

  • We need to re-export a symbol other crates can call, but we do not want benches or release builds to pull in debug-only deps or code.
  • Putting #[cfg(...)] on the function itself makes the symbol vanish in release/bench. Callers would then need their own cfg fences, which is brittle across crates.
  • By keeping the function always present and gating only its body, callers can invoke it unconditionally. In debug/test it asserts; in release/bench it compiles to a no-op.

Build behavior:

  • In debug/test, the inner block runs and uses debug_assert!.
  • In release/bench, the else block keeps the args “used” so the function is a true no-op (no codegen warnings, no panic paths).

Cost:

  • Inlining plus the cfg-ed body means zero runtime cost in release and bench profiles.

Usage:

  • Call anywhere you want a cheap alignment check in debug/test, including from other crates that depend on this one.