1
//! Small wrapper functions over FFI things.
2

            
3
use std::ffi::{c_char, CStr, CString};
4

            
5
use crate::ffi;
6
use libc::free;
7

            
8
/// Gets a given value from Minion's TableOut (where it stores run statistics).
9
pub fn get_from_table(key: String) -> Option<String> {
10
    unsafe {
11
        #[allow(clippy::expect_used)]
12
        let c_string = CString::new(key).expect("");
13
        let key_ptr = c_string.into_raw();
14
        let val_ptr: *mut c_char = ffi::TableOut_get(key_ptr);
15

            
16
        drop(CString::from_raw(key_ptr));
17

            
18
        if val_ptr.is_null() {
19
            free(val_ptr as _);
20
            None
21
        } else {
22
            #[allow(clippy::unwrap_used)]
23
            // CStr borrows the string in the ptr.
24
            // We convert it to &str then clone into a String.
25
            let res = CStr::from_ptr(val_ptr).to_str().unwrap().to_owned();
26
            free(val_ptr as _);
27
            Some(res)
28
        }
29
    }
30
}