macro_rules! unsafe_precondition_assert {
($cond:expr $(,)?) => { ... };
($cond:expr, $($arg:tt)+) => { ... };
(@inner $cond:expr, $msg:expr) => { ... };
}Expand description
Checks that a precondition of an unsafe function is followed.
The check is enabled at runtime if debug assertions (CONFIG_RUST_DEBUG_ASSERTIONS)
are enabled. Otherwise, this macro is a no-op.
§Examples
use kernel::unsafe_precondition_assert;
struct RawBuffer<T: Copy, const N: usize> {
data: [T; N],
}
impl<T: Copy, const N: usize> RawBuffer<T, N> {
/// # Safety
///
/// The caller must ensure that `index` is less than `N`.
unsafe fn set_unchecked(&mut self, index: usize, value: T) {
unsafe_precondition_assert!(
index < N,
"RawBuffer::set_unchecked() requires index ({index}) < N ({N})"
);
// SAFETY: By the safety requirements of this function, `index` is valid.
unsafe {
*self.data.get_unchecked_mut(index) = value;
}
}
}§Panics
Panics if the expression is evaluated to false at runtime.