mirror of
https://github.com/tailix/libkernaux.git
synced 2025-04-21 17:42:26 -04:00
Rust: implement assertions
This commit is contained in:
parent
9490e5a37a
commit
38993ddc26
3 changed files with 41 additions and 0 deletions
|
@ -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
|
||||
|
|
33
pkgs/rust/kernaux/src/assert.rs
Normal file
33
pkgs/rust/kernaux/src/assert.rs
Normal 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()) }
|
||||
}
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
mod assert;
|
||||
mod ntoa;
|
||||
|
||||
pub use ntoa::*;
|
||||
|
|
Loading…
Add table
Reference in a new issue