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
987
pub fn get_from_table(key: String) -> Option<String> {
10
987
    unsafe {
11
987
        #[allow(clippy::expect_used)]
12
987
        let c_string = CString::new(key).expect("");
13
987
        let key_ptr = c_string.into_raw();
14
987
        let val_ptr: *mut c_char = ffi::TableOut_get(key_ptr);
15
987

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

            
18
987
        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
987
            let res = CStr::from_ptr(val_ptr).to_str().unwrap().to_owned();
26
987
            free(val_ptr as _);
27
987
            Some(res)
28
        }
29
    }
30
987
}