Rust: implement assertions

This commit is contained in:
Alex Kotov 2022-05-31 22:11:10 +03:00
parent 9490e5a37a
commit 38993ddc26
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
3 changed files with 41 additions and 0 deletions

View File

@ -12,6 +12,13 @@ keywords = ["ffi", "embedded", "bindings"]
categories = ["api-bindings", "embedded", "parsing"]
publish = true
[dependencies]
ctor = "0.1.22"
[dependencies.kernaux-sys]
version = "0.3.0"
path = "../kernaux-sys"
[dependencies.libc]
version = "0.2.113"
default-features = false

View File

@ -0,0 +1,33 @@
use ctor::ctor;
use libc::{c_char, c_int};
use std::ffi::CStr;
#[ctor]
unsafe fn ctor() {
kernaux_sys::assert_cb = Some(assert_cb);
}
unsafe extern "C" fn assert_cb(
file: *const c_char,
line: c_int,
msg: *const c_char,
) {
let file = CStr::from_ptr(file).to_str().unwrap();
let msg = CStr::from_ptr(msg).to_str().unwrap();
panic!("{}:{}:{}", file, line, msg);
}
#[cfg(test)]
mod tests {
use std::ffi::CString;
#[test]
#[should_panic(expected = "foo.rs:123:bar")]
fn default() {
let file = CString::new("foo.rs").unwrap();
let msg = CString::new("bar").unwrap();
unsafe { kernaux_sys::assert_do(file.as_ptr(), 123, msg.as_ptr()) }
}
}

View File

@ -1,3 +1,4 @@
mod assert;
mod ntoa;
pub use ntoa::*;