kernaux/
assert.rs

1use ctor::ctor;
2use libc::{c_char, c_int};
3use std::ffi::CStr;
4
5#[ctor]
6unsafe fn ctor() {
7    kernaux_sys::assert_cb = Some(assert_cb);
8}
9
10unsafe extern "C" fn assert_cb(
11    file: *const c_char,
12    line: c_int,
13    msg: *const c_char,
14) {
15    let file = CStr::from_ptr(file).to_str().unwrap();
16    let msg = CStr::from_ptr(msg).to_str().unwrap();
17
18    panic!("{}:{}:{}", file, line, msg);
19}
20
21#[cfg(test)]
22mod tests {
23    use std::ffi::CString;
24
25    #[test]
26    #[should_panic(expected = "foo.rs:123:bar")]
27    fn default() {
28        let file = CString::new("foo.rs").unwrap();
29        let msg = CString::new("bar").unwrap();
30
31        unsafe { kernaux_sys::assert_do(file.as_ptr(), 123, msg.as_ptr()) }
32    }
33}