Hello, is there some no_std
way to panic with custom Location
? I want to write an assert function that will be used in C code, but I see no way to overwrite default location of panic, so panic has location of __foo_assert
.
My attempt so far:
#[unsafe(no_mangle)]
extern "C" fn __foo_assert(
value: core::ffi::c_uchar,
func: *const u8,
file: *const u8,
line: core::ffi::c_ulong,
) {
if value == 0 {
if file.is_null() || func.is_null() {
panic!("Assertion failed at unknown location at line {}", line);
}
let file = unsafe { core::ffi::CStr::from_ptr(file) };
let func = unsafe { core::ffi::CStr::from_ptr(func) };
let (Ok(file), Ok(func)) = (file.to_str(), func.to_str()) else {
panic!("Assertion failed at unknown location at line {}", line);
};
panic!("Assertion failed in {func} at {file}:{line}")
}
}
And on C side:
extern void __foo_assert(unsigned char, const char *, const char *, unsigned long);
#define assert(x) __foo_assert(x, __func__, __FILE__, __LINE__)