use core::{fmt::Write, panic::PanicInfo};
#[inline]
fn panic(m: &str) -> ! {
unsafe { crate::bindings::rt_panic(m.as_ptr().cast()) }
}
struct PanicBuf<const N: usize> {
cursor: usize,
buf: [u8; N],
}
impl<const N: usize> PanicBuf<N> {
const fn new() -> PanicBuf<N> {
PanicBuf {
cursor: 0,
buf: [0; N],
}
}
const fn as_str(&self) -> &str {
unsafe { core::str::from_utf8_unchecked(&self.buf) }
}
}
impl<const N: usize> core::fmt::Write for PanicBuf<N> {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
for &b in s.as_bytes() {
if let Some(e) = self.buf.get_mut(self.cursor) {
*e = b;
self.cursor += 1;
} else {
break;
}
}
Ok(())
}
}
#[panic_handler]
fn panic_handler(panic_info: &PanicInfo) -> ! {
use crate::panic::PanicBuf;
let mut panic_buf: PanicBuf<128> = PanicBuf::new();
let _ = core::write!(panic_buf, "{}", panic_info);
panic(panic_buf.as_str())
}