Skip to content
Permalink
Browse files

Add extern "C" get/free methods

  • Loading branch information...
buybackoff committed Jul 14, 2019
1 parent 5bddc1f commit 5c8352ebc920c7558addf3bcbf3b631a3ac5a260
Showing with 41 additions and 3 deletions.
  1. +34 −0 src/rust/corelib/src/lib.rs
  2. +7 −3 src/rust/sandbox/src/main.rs
@@ -3,6 +3,29 @@ extern crate lmdb_sys;
extern crate sqlite_sys;

use libc::*;
use std::ffi::CString;

#[no_mangle]
pub extern "C" fn native_versions_get() -> *mut c_char {
let s = format!("LMDB: {}\nBlosc: {}\nSQLite: {}", lmdb_version(), blosc_version(), sqlite_version());
let cs = CString::new(s).unwrap();
cs.into_raw()
}

#[no_mangle]
pub extern "C" fn native_versions_free(c: *mut c_char) {
if c.is_null() {
return;
}
// CString is dropped automatically when does out of scope
unsafe { CString::from_raw(c); }

// Print diagnostics in debug build
if cfg!(debug_assertions) {
println!("Rust string is dropped");
}
}


pub fn lmdb_version() -> String {
unsafe {
@@ -30,6 +53,17 @@ pub fn sqlite_version() -> String {

#[cfg(test)]
mod tests {
#[test]
fn native_versions_get_works() {
unsafe{
let nv = super::native_versions_get();
let cs = std::ffi::CString::from_raw(nv);
assert_eq!(cs.to_str().unwrap(), "LMDB: 0.9.70\nBlosc: 1.15.2.dev\nSQLite: 3.29.0");
std::mem::forget(cs);
super::native_versions_free(nv);
}
}

#[test]
fn lmdb_works() {
assert_eq!(super::lmdb_version(), "0.9.70");
@@ -1,7 +1,11 @@
extern crate corelib;

fn main() -> () {
println!("LMDB: {}", corelib::lmdb_version());
println!("Blosc: {}", corelib::blosc_version());
println!("SQLite: {}", corelib::sqlite_version());
unsafe{
let nv = corelib::native_versions_get();
let cs = std::ffi::CString::from_raw(nv);
println!("{}", cs.to_str().unwrap());
std::mem::forget(cs); // Do not drop, we need return it back to destructor to simulate desired behavior
corelib::native_versions_free(nv);
}
}

0 comments on commit 5c8352e

Please sign in to comment.
You can’t perform that action at this time.