use alloc::vec::Vec;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GoPanic {
text: Vec<u8>,
}
impl GoPanic {
pub fn new(text: impl Into<Vec<u8>>) -> Self {
GoPanic { text: text.into() }
}
pub fn text(&self) -> &[u8] {
&self.text
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum RuntimeError {
DivideByZero,
NegativeShift,
}
impl RuntimeError {
pub fn message(self) -> &'static str {
match self {
RuntimeError::DivideByZero => "runtime error: integer divide by zero",
RuntimeError::NegativeShift => "runtime error: negative shift amount",
}
}
}
#[cold]
pub fn go_panic(p: GoPanic) -> ! {
#[cfg(feature = "std")]
{
std::panic::resume_unwind(alloc::boxed::Box::new(p))
}
#[cfg(not(feature = "std"))]
{
panic!("{}", alloc::string::String::from_utf8_lossy(&p.text))
}
}
#[cold]
pub fn runtime_error(e: RuntimeError) -> ! {
go_panic(GoPanic::new(e.message()))
}