Panic with custom location

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__)

I'm not certain there is, but you may not need it. It's UB to unwind across an FFI boundary like that, so this only works under panic=abort. In which case, you could manually print the error message and abort, using your custom location.

That is, of course, unless you're using a panic hook. Then you'll need to manually call that, which complicates matters.

There is no way to panic with a custom Location. You can't even create a custom Location as it's internal contents are unstable. Currently it contains a regular &str for the file location, but Add nul-terminated filename for #[track_caller] by Darksonn · Pull Request #131828 · rust-lang/rust · GitHub if merged would add a nul terminator.