1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#![cfg(not(feature = "std"))]
extern "C" {
fn panic(payload_ptr: *const u8, payload_len: u32) -> !;
}
#[cfg(not(feature = "panic_with_msg"))]
#[no_mangle]
#[lang = "panic_fmt"]
pub extern fn panic_fmt() -> ! {
unsafe {
panic(::core::ptr::null(), 0u32);
}
}
#[cfg(feature = "panic_with_msg")]
#[no_mangle]
#[lang = "panic_fmt"]
pub extern fn panic_fmt(_fmt: ::core::fmt::Arguments, file: &'static str, line: u32, col: u32) -> ! {
use Vec;
use byteorder::{LittleEndian, ByteOrder};
let msg = format!("{}", _fmt);
let mut sink = Sink::new(
4 + msg.as_bytes().len() +
4 + file.as_bytes().len() +
4 +
4
);
sink.write_str(msg.as_bytes());
sink.write_str(file.as_bytes());
sink.write_u32(line);
sink.write_u32(col);
unsafe {
panic(sink.as_ptr(), sink.len() as u32);
}
struct Sink {
buf: Vec<u8>,
pos: usize
}
impl Sink {
#[inline(always)]
fn new(capacity: usize) -> Sink {
let mut buf = Vec::with_capacity(capacity);
buf.resize(capacity, 0);
Sink {
buf: buf,
pos: 0,
}
}
#[inline(always)]
fn reserve(&mut self, len: usize) -> &mut [u8] {
let dst = &mut self.buf[self.pos..self.pos+len];
self.pos += len;
dst
}
#[inline(always)]
fn write_u32(&mut self, val: u32) {
LittleEndian::write_u32(self.reserve(4), val);
}
#[inline(always)]
fn write_str(&mut self, bytes: &[u8]) {
self.write_u32(bytes.len() as u32);
self.reserve(bytes.len()).copy_from_slice(bytes)
}
}
impl ::core::ops::Deref for Sink {
type Target = [u8];
fn deref(&self) -> &[u8] {
&self.buf
}
}
}
#[lang = "eh_personality"]
extern "C" fn eh_personality() {}