mirror of https://github.com/tailix/libkernaux.git
Rust: implement assertions
This commit is contained in:
parent
9490e5a37a
commit
38993ddc26
|
@ -12,6 +12,13 @@ keywords = ["ffi", "embedded", "bindings"]
|
||||||
categories = ["api-bindings", "embedded", "parsing"]
|
categories = ["api-bindings", "embedded", "parsing"]
|
||||||
publish = true
|
publish = true
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
ctor = "0.1.22"
|
||||||
|
|
||||||
[dependencies.kernaux-sys]
|
[dependencies.kernaux-sys]
|
||||||
version = "0.3.0"
|
version = "0.3.0"
|
||||||
path = "../kernaux-sys"
|
path = "../kernaux-sys"
|
||||||
|
|
||||||
|
[dependencies.libc]
|
||||||
|
version = "0.2.113"
|
||||||
|
default-features = false
|
||||||
|
|
|
@ -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()) }
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,4 @@
|
||||||
|
mod assert;
|
||||||
mod ntoa;
|
mod ntoa;
|
||||||
|
|
||||||
pub use ntoa::*;
|
pub use ntoa::*;
|
||||||
|
|
Loading…
Reference in New Issue