Skip to content

Commit

Permalink
Expose version() in C API
Browse files Browse the repository at this point in the history
  • Loading branch information
poszu committed Jan 9, 2024
1 parent f98f0ca commit 76eca27
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions ffi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod initialization;
mod log;
mod post_impl;
mod version;

#[repr(C)]
#[derive(Debug, Clone, Copy)]
Expand Down
17 changes: 17 additions & 0 deletions ffi/src/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::ffi::{c_char, CStr};

const VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), "\0");

#[no_mangle]
pub extern "C" fn version() -> *const c_char {
unsafe { CStr::from_bytes_with_nul_unchecked(VERSION.as_bytes()) }.as_ptr()
}

#[cfg(test)]
mod tests {
#[test]
fn test_version() {
let version = unsafe { std::ffi::CStr::from_ptr(super::version()) };
assert_eq!(version.to_str().unwrap(), env!("CARGO_PKG_VERSION"));
}
}

0 comments on commit 76eca27

Please sign in to comment.