diff --git a/pkgs/rust/kernaux/Cargo.toml b/pkgs/rust/kernaux/Cargo.toml index 13cb34c..00b11cb 100644 --- a/pkgs/rust/kernaux/Cargo.toml +++ b/pkgs/rust/kernaux/Cargo.toml @@ -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 diff --git a/pkgs/rust/kernaux/src/assert.rs b/pkgs/rust/kernaux/src/assert.rs new file mode 100644 index 0000000..3e7dee2 --- /dev/null +++ b/pkgs/rust/kernaux/src/assert.rs @@ -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()) } + } +} diff --git a/pkgs/rust/kernaux/src/lib.rs b/pkgs/rust/kernaux/src/lib.rs index 350d2e4..9115180 100644 --- a/pkgs/rust/kernaux/src/lib.rs +++ b/pkgs/rust/kernaux/src/lib.rs @@ -1,3 +1,4 @@ +mod assert; mod ntoa; pub use ntoa::*;