Open
Description
My target is wasm32-unknown-unknown
but I think it can be applied to any embedded systems. I just want to define panic handler, but formatting core::panic::Location
costs amlost 3 KiB in binary. This is because inefficient impl Display for u32
. Also see: https://github.com/dtolnay/itoa
[package]
name = "demo"
version = "0.1.0"
edition = "2021"
[lib]
name = "demo"
crate-type = ["cdylib"]
[dependencies]
arrayvec = { version = "0.7.4", default-features = false }
[profile.release]
opt-level = "z"
codegen-units = 1
lto = true
strip = true
#![no_std]
#![feature(panic_info_message)]
#[allow(improper_ctypes)]
extern "C" {
pub fn sc_panic(msg: &str) -> !;
}
#[panic_handler]
fn panic(panic_info: &core::panic::PanicInfo) -> ! {
use core::fmt::Write;
let mut msg = arrayvec::ArrayString::<1024>::new();
// 2_009 bytes binary:
/*let _ = write!(&mut msg, "{}", unsafe {
panic_info.message().unwrap_unchecked()
});*/
// 5_076 bytes binary:
let _ = write!(&mut msg, "{}", unsafe {
panic_info.location().unwrap_unchecked()
});
// ^^^ +3 KiB to print location in format {file}:{line}:{column}???
// exit from smart contract with msg
unsafe { sc_panic(&msg) }
}
#[no_mangle]
extern "C" fn entry_point() {
panic!("msg");
}
This is mostly because this line:
rust/library/core/src/fmt/num.rs
Line 277 in 1aa6aef
Could we have something like specialization for more simpler
impl Display for $num
?