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

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

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